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

Class Inheritance

P: 5
I have two classes: A and B.

Class B is a child class of A.

In class A I have the friend operator>> overloaded, and want to access a public function from the child class B.

However, when I build, I get the error "class A has no member named function".

Why can't I access the public function in the child class?

code snippets:

Expand|Select|Wrap|Line Numbers
  1. //Parent Class A: 
  2. istream& operator>>(istream& ins, A& c)
  3. {
  4.     getline(ins, c.title);
  5.     ins >> c.size;
  6.  
  7.     for (int i = 0; i < c.size; i++)
  8.         ins >> c.people[i];
  9.     c.function(c.people, c.size); //this line produces error
  10.  
  11.     return ins;
  12. }
  13.  
  14. //Child Class B:
  15. void B::function(A people[], int& size)
  16. {
  17.     //....
  18. }
  19.  
Mar 14 '07 #1
Share this Question
Share on Google+
7 Replies


Ganon11
Expert 2.5K+
P: 3,652
In general, a superclass (A) knows nothing about its subclasses (B). So how can A call a function defined in B?

B, however, can call a function in A. Subclasses do know about their superclasses.
Mar 14 '07 #2

P: 5
In general, a superclass (A) knows nothing about its subclasses (B). So how can A call a function defined in B?

B, however, can call a function in A. Subclasses do know about their superclasses.
How could I call the function from within the parent class?

The function in the child class is public.
Mar 14 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
How could I call the function from within the parent class?

The function in the child class is public.
How do you plan to determine if the object in question is of type B or A or some other subclass of A?
Mar 15 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
You could include the same function in A, and either provide a unique definition or make it virtual. That way, any subclass of A can (or, if virtual, must) include a definition of that method, and you can call the method on any A object. At run-time, though, it would be determined that the object is actually a B, and the B function would be used rather than the A.
Mar 15 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
(or, if virtual, must)
Could be pure virtual.
Mar 15 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
In which case any subclass must define it or still declare it as pure virtual and be abstract. Since the OP ncluded a definition in B of function, I assumed he would want to define it and, thus, either pure virtual or virtual could work in A.
Mar 15 '07 #7

Banfa
Expert Mod 5K+
P: 8,916
In which case any subclass must define it or still declare it as pure virtual and be abstract. Since the OP ncluded a definition in B of function, I assumed he would want to define it and, thus, either pure virtual or virtual could work in A.
Sorry I misread your original post and thought you were refering to class A not a sub-class of class A
Mar 15 '07 #8

Post your reply

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