473,396 Members | 1,966 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,396 software developers and data experts.

Class Inheritance

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
7 2713
Ganon11
3,652 Expert 2GB
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
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
9,065 Expert Mod 8TB
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
3,652 Expert 2GB
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
9,065 Expert Mod 8TB
(or, if virtual, must)
Could be pure virtual.
Mar 15 '07 #6
Ganon11
3,652 Expert 2GB
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
9,065 Expert Mod 8TB
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

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

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
22
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
11
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
3
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.