why I got error in private inheritance when use virtual function

please shed a light ,thanks in advance




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
class Sea
{
      public:
	    virtual void get();
	    virtual void print();
      private:
	    int a;
	    string b;
	    char c;
};
void Sea::get()
{
      cin>>a>>b>>c;
}
void Sea::print()
{
      cout<<a<<' '<<b<<' '<<c<<' ';
}
class Sea1:private Sea
{
      public:
	    virtual void get();
	    virtual void print();
      private:
	    int a;
	    string b;
};
void Sea1::get()
{
      Sea::get();
      cin>>a>>b;
}
void Sea1::print()
{
      Sea::print();
      cout<<a<<' '<<b;
}
int main()
{
      Sea1 a;
      Sea *r;
      r=&a;
      r->get();
      r->print();
      return 0;
}
Last edited on
If you want to do this you should use public inheritance.
1
2
3
Sea1 a;
Sea *r;
r=&a;
Right, you can't get a Sea pointer from a Sea1 object because it inherits privately. Only Sea1 and its friends can access the Sea portion of it.

Peter is right, for this kind of thing you likely want public inheritance.
thanks
Topic archived. No new replies allowed.