Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Classes - Adding members to members

Reply
Thread Tools

Classes - Adding members to members

 
 
Daz
Guest
Posts: n/a
 
      05-06-2006
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. )

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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-06-2006
* 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?
 
Reply With Quote
 
 
 
 
Daz
Guest
Posts: n/a
 
      05-06-2006

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.

 
Reply With Quote
 
Jonathan Lamothe
Guest
Posts: n/a
 
      05-06-2006
-----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. )
>
> -----------------------------------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

iD8DBQFEXMuCNrv4JaRC3JsRAiJeAKCGwsm7aZ6f88qYX9chSd QdASiWTwCfQjjb
RK4JuDRsgPGob7oezq50t4I=
=ATHs
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-06-2006

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

 
Reply With Quote
 
Daz
Guest
Posts: n/a
 
      05-07-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can nsmc, local classes or anonymous classes have static members? Rit Java 23 01-03-2010 05:54 PM
class members vs instance members hdixon Python 3 07-09-2006 06:56 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Templates: Members Vs. non-members Dave C++ 3 08-10-2004 11:23 AM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments