Classes - Adding members to members

Discussion in 'C++' started by Daz, May 6, 2006.

  1. Daz

    Daz Guest

    Hi Everyone. Please could someone explain how I can go about adding
    separate member functions to stock.add? For example,
    stock.add.count(int) and stock.add.value(int)?

    This way I can hopefully add to both 'cnt' and 'val'. This class
    doesn't serve much of a purpose other than to help me learn about
    classes (as this is my first).

    Would I need to create another class, and add it as a friend to my
    exisiting class? If so, what about the classes global variables?

    Any help would be greatly appreciated as always. :eek:)

    -----------------------------------CODE
    START-----------------------------------------

    #include <iostream>

    using namespace std;
    using std::string;
    using std::cout;
    using std::endl;

    class stock
    {
    signed long int val;
    long int cnt;
    public:
    stock () { val = 0; count = 0; };
    stock (int a) { val = a; count = 0; }
    void add (int a) { val = val + a; }
    void subtract (int a) { val = val - a; }
    signed int value() { return val; }

    };

    int main ()
    {
    stock pawn;
    pawn.add(5);
    pawn.add(6);
    cout << pawn.value() << endl;
    return 0;
    }


    -----------------------------------CODE
    END-----------------------------------------
     
    Daz, May 6, 2006
    #1
    1. Advertising

  2. * Daz:
    > Hi Everyone. Please could someone explain how I can go about adding
    > separate member functions to stock.add? For example,
    > stock.add.count(int) and stock.add.value(int)?


    Use two member functions, one called add_count, and one called add_value.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 6, 2006
    #2
    1. Advertising

  3. Daz

    Daz Guest

    Alf P. Steinbach wrote:

    > * Daz:
    > > Hi Everyone. Please could someone explain how I can go about adding
    > > separate member functions to stock.add? For example,
    > > stock.add.count(int) and stock.add.value(int)?

    >
    > Use two member functions, one called add_count, and one called add_value.


    I thought I could make the member functions more elaborate, however,
    your suggestion is certainly one I didn't think of.

    Thanks for the prompt response Alf.
     
    Daz, May 6, 2006
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Daz wrote:
    > Hi Everyone. Please could someone explain how I can go about adding
    > separate member functions to stock.add? For example,
    > stock.add.count(int) and stock.add.value(int)?
    >
    > This way I can hopefully add to both 'cnt' and 'val'. This class
    > doesn't serve much of a purpose other than to help me learn about
    > classes (as this is my first).
    >
    > Would I need to create another class, and add it as a friend to my
    > exisiting class? If so, what about the classes global variables?
    >
    > Any help would be greatly appreciated as always. :eek:)
    >
    > -----------------------------------CODE
    > START-----------------------------------------
    >
    > #include <iostream>
    >
    > using namespace std;
    > using std::string;
    > using std::cout;
    > using std::endl;
    >
    > class stock
    > {
    > signed long int val;
    > long int cnt;
    > public:
    > stock () { val = 0; count = 0; };
    > stock (int a) { val = a; count = 0; }
    > void add (int a) { val = val + a; }
    > void subtract (int a) { val = val - a; }
    > signed int value() { return val; }
    >
    > };
    >
    > int main ()
    > {
    > stock pawn;
    > pawn.add(5);
    > pawn.add(6);
    > cout << pawn.value() << endl;
    > return 0;
    > }
    >
    >
    > -----------------------------------CODE
    > END-----------------------------------------
    >


    Well, you could define your add function like this:

    class stock
    {

    /* ... */

    class
    {
    public:

    void operator () (int a)
    {
    val = val + a;
    }

    /* define your member functions here */

    } add;

    /* ... */

    };

    This would keep the syntax you wanted, but you couldn't really call add
    a function anymore. Hope that helps.

    - --
    Regards,
    Jonathan Lamothe

    /*
    * Oops. The kernel tried to access some bad page. We'll have to
    * terminate things with extreme prejudice.
    */

    die_if_kernel("Oops", regs, error_code);
    -- From linux/arch/i386/mm/fault.c
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFEXMuCNrv4JaRC3JsRAiJeAKCGwsm7aZ6f88qYX9chSdQdASiWTwCfQjjb
    RK4JuDRsgPGob7oezq50t4I=
    =ATHs
    -----END PGP SIGNATURE-----
     
    Jonathan Lamothe, May 6, 2006
    #4
  5. Daz wrote:
    > Hi Everyone. Please could someone explain how I can go about adding
    > separate member functions to stock.add? For example,
    > stock.add.count(int) and stock.add.value(int)?


    In C++, a function call is denoted by adding parantheses after a name.
    If a name has no parantheses after it, it is not a function call. In
    your example,

    stock.add.count();

    "add" cannot be a function. However, you could make "add" an object in
    "stock" and put a member function "count()" in "add".

    class adder
    {
    public;
    void count(int i);
    void value(int i);
    };

    class test
    {
    public:
    adder add;
    void f();
    };

    int main()
    {
    test t;
    t.f();
    t.add.count(0);
    }

    > Would I need to create another class, and add it as a friend to my
    > exisiting class?


    No, friends are a different thing.


    Jonathan
     
    Jonathan Mcdougall, May 6, 2006
    #5
  6. Daz

    Daz Guest

    Jonathan Mcdougall wrote:
    {
    > In C++, a function call is denoted by adding parantheses after a

    name.
    > If a name has no parantheses after it, it is not a function call.

    In
    > your example,
    >
    > stock.add.count();
    >
    > "add" cannot be a function. However, you could make "add" an object

    in
    > "stock" and put a member function "count()" in "add".
    >
    > class adder
    > {
    > public;
    > void count(int i);
    > void value(int i);
    > };
    >
    > class test
    > {
    > public:
    > adder add;
    > void f();
    > };
    >
    > int main()
    > {
    > test t;
    > t.f();
    > t.add.count(0);
    > }
    >

    }

    Thanks for that Jonathan. Very useful, and very clever. I appreciate
    the time you took to come up with a good working example, too. It's
    very much appreciated.
     
    Daz, May 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,740
  2. CoolPint
    Replies:
    8
    Views:
    989
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    369
    tom_usenet
    Aug 10, 2004
  4. hdixon
    Replies:
    3
    Views:
    649
    hdixon
    Jul 9, 2006
  5. Rit
    Replies:
    23
    Views:
    946
    Mike Schilling
    Jan 3, 2010
Loading...

Share This Page