By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

help about class and member function

P: 4
Hi All;
I have two classes;
first class,
Expand|Select|Wrap|Line Numbers
  1. class  A
  2. {
  3. //
  4. Public:
  5. void read_data1(fstream&);
  6. void write_data1(fstream&);
  7. };
  8.  
  9. A::read_data1(fstream & in)
  10. {
  11. //reading data "here i use vector to store data"
  12. }
  13.  
  14. B::write_data(fstream& out)
  15. {
  16. //writing data
  17. }
  18.  
second class,
Expand|Select|Wrap|Line Numbers
  1. class  B
  2. {
  3. //
  4. void get_result(fstream &);
  5. void out_result(fstream &);
  6. }
  7.  
Now my problem is, I want use the first class member function into second class member function as following,
Expand|Select|Wrap|Line Numbers
  1. B::get_result(fstream & in)
  2. {
  3. //.......reading data[COLOR="Red"](directly use class A member funtion here)[/COLOR]
  4. }
  5.  
  6. B::out_result(fstream & out)
  7. {
  8. //.......writing data [COLOR="red"](directly use class A member function here)[/COLOR]
  9. }
  10.  
Please tell me how can I do this?

Thanks in advance!

Rgds,
Sitha
Jun 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: 33
Well, derive B from A, so that it inherits its methods. Inheritance is a basic topic when constructing classes.
Jun 5 '07 #2

P: 4
Well, derive B from A, so that it inherits its methods. Inheritance is a basic topic when constructing classes.
The problem is that, if I want to make use seveal classes' member function into B, then it would be multiple inheritance.
multiple inheritance is not desired option in programing..........
So I expect some different idea .....

Please give me an idea.
Jun 5 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
The onjly way to call this method:

Expand|Select|Wrap|Line Numbers
  1. A::read_data1(fstream & in);
  2.  
is by using a class A object.

Can you add a class A object reference to yout argument list???

Expand|Select|Wrap|Line Numbers
  1.  
  2. B::get_result(fstream & in, A& arg);
  3.  
Or can class B have a class A member?

Failing that you will fall back to inheritance of some sort.
Jun 5 '07 #4

P: 4
The onjly way to call this method:

Expand|Select|Wrap|Line Numbers
  1. A::read_data1(fstream & in);
  2.  
is by using a class A object.

Can you add a class A object reference to yout argument list???

Expand|Select|Wrap|Line Numbers
  1.  
  2. B::get_result(fstream & in, A& arg);
  3.  
Or can class B have a class A member?

Failing that you will fall back to inheritance of some sort.
From your suggestion I tried the following way,



Expand|Select|Wrap|Line Numbers
  1. class A;
  2.  
  3. class B
  4. {
  5. private:
  6. A* pa;
  7. public:
  8. void get_result(fstream&, pa&);
  9.  
  10. //
  11. };
  12.  
  13. void B::get_result(fstream & in, A& arg)
  14. {
  15. arg->read_data1(in);
  16. }
But it didn't work.....I know that I am making a silly mistake here.. Please tell me what is wrong with my code?
Jun 6 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
Expand|Select|Wrap|Line Numbers
  1. void B::get_result(fstream & in, A& arg)
  2. {
  3. arg->read_data1(in);
  4. }
  5.  
won't compile. arg is a reference that you are using as a pointer. You have to pick one of these:
Expand|Select|Wrap|Line Numbers
  1. void B::get_result(fstream & in, A& arg)
  2. {
  3. arg.read_data1(in);
  4. }
  5. //OR
  6. void B::get_result(fstream & in, A* arg)
  7. {
  8. arg->read_data1(in);
  9. }
  10.  
Jun 6 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.