Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Classes - Adding members to members (http://www.velocityreviews.com/forums/t453819-classes-adding-members-to-members.html)

Daz 05-06-2006 10:46 AM

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


Alf P. Steinbach 05-06-2006 10:54 AM

Re: Classes - Adding members to members
 
* 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?

Daz 05-06-2006 10:58 AM

Re: Classes - Adding members to members
 

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.


Jonathan Lamothe 05-06-2006 04:14 PM

Re: Classes - Adding members to members
 
-----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

iD8DBQFEXMuCNrv4JaRC3JsRAiJeAKCGwsm7aZ6f88qYX9chSd QdASiWTwCfQjjb
RK4JuDRsgPGob7oezq50t4I=
=ATHs
-----END PGP SIGNATURE-----

Jonathan Mcdougall 05-06-2006 07:16 PM

Re: Classes - Adding members to members
 

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


Daz 05-07-2006 09:25 AM

Re: Classes - Adding members to members
 

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.



All times are GMT. The time now is 03:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.