Classes - Adding members to members

D

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)?

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-----------------------------------------
 
A

Alf P. Steinbach

* 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.
 
D

Daz

Alf said:
* Daz:

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.
 
J

Jonathan Lamothe

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
J

Jonathan Mcdougall

Daz said:
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
 
D

Daz

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top