| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Daz" |
| Date: |
06 May 2006 05:46:40 AM |
| Object: |
Classes - Adding members to members |
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. :o)
-----------------------------------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-----------------------------------------
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Classes - Adding members to members |
06 May 2006 05:54:32 AM |
|
|
* 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?
.
|
|
|
| User: "Daz" |
|
| Title: Re: Classes - Adding members to members |
06 May 2006 05:58:11 AM |
|
|
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.
.
|
|
|
|
|
| User: "Jonathan Lamothe" |
|
| Title: Re: Classes - Adding members to members |
06 May 2006 11:14:58 AM |
|
|
-----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. :o)
-----------------------------------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-----
.
|
|
|
|
| User: "Jonathan Mcdougall" |
|
| Title: Re: Classes - Adding members to members |
06 May 2006 02:16:32 PM |
|
|
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
.
|
|
|
| User: "Daz" |
|
| Title: Re: Classes - Adding members to members |
07 May 2006 04:25:54 AM |
|
|
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.
.
|
|
|
|
|

|
Related Articles |
|
|