473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

May 6 '06 #1
5 1704
* 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?
May 6 '06 #2
Daz

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.

May 6 '06 #3
-----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-----
May 6 '06 #4

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

May 6 '06 #5
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.

May 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Ward | last post by:
Hi All, Sorry if this is off topic, but I could not seem to find a suitable OO Design newsgroup. If there is one feel free to let me know. Here is a simplification of a general design problem I...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
5
by: pauldepstein | last post by:
I am working with a class which has a constructor which takes several parameters: class MyClass {public: MyClass(SomeType A, SomeOtherType B, YetAnotherType C) {} // ....} I want to amend...
6
by: ManicQin | last post by:
Let's ignore for a minute design decisions , readability and any other of those parameters , In the most raw form of c++ (purely efficiency if you want) what is better, bloating up a classes or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.