-
June 10th, 2004, 10:15 PM
#1
Whats the problem in the following code?
I am new to C++ and tried the following code on LINUX using g++.
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class mom
{
public:
void display()
{cout<<"MOM is here..."; }
};
class son: private mom
{
public:
void display(){mom:isplay();}
friend void g2(mom*);
};
void g2(mom* s)
{
s->display();
}
int main()
{
son s1;
g2(&s1);
cout<<"\n";
return 0;
}
It gives me an error which says mom is an inaccessible base class of son.
But according to me if the base class is declared private then its public and protected members are accessible to members and friends of the derived class and only members and friends have the premission( or can change) D* to B*.
So dows it means i have a bugged version of g++ but the same message is printed on VC++ and Dev C++ also.
Pls help me.
-
June 10th, 2004, 11:05 PM
#2
First, declare display() as virtual.
Secondly. post the exact error.
Kuphryn
-
June 11th, 2004, 01:32 AM
#3
You cannot convert a derived class to a pointer to a base class (as you try to do in son s1;g2(&s1) when you have declared the inheritance private or protected.
try public inheritance instead
Code:
class son: public mom
-
June 11th, 2004, 07:34 AM
#4
Further, you don't need to create another Display method in Son if all it does is call Mom's Display. If you want Son to do something different, declare Mom's Display as virtual and have Son do something different. As it is, the code is redundant and therefore potentially confusing.
-
June 13th, 2004, 04:29 AM
#5
-
June 13th, 2004, 09:31 AM
#6
GCDEF: son does need a display() function in order to make it visible. All the members of mom are private to son, so virtualising display still wouldn't work, and is unnecessary.
Private inheritance means that you can't pass a son object to a mom* argument.
But according to me if the base class is declared private then its public and protected members are accessible to members and friends of the derived class and only members and friends have the premission( or can change) D* to B*.
Not really. Private inheritance means that public and protected members of the base class have their access specifier changed to private in the derived class. Friends of a derived class have the same access rights to memebrs of a base class as do the members of the derived class. There is no defined conversion between D* and B* for private (or protected) inheritance (which is why you can't call g2() with a son object - only public inheritance gives you the IS-A relationship).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|