Inheritance class invoke

I used an object of class D, can I invoke function f()?
or, the keyword private only allow me to invoke h()?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class B
{
public:
 B();
 B(int nn);
 void f();
 void g();
private:
 int n;
};
class D: private B
{
public:
 D(int nn, float dd);
 void h();
private:
 double d;
};
closed account (D80DSL3A)
Not sure. I haven't used private inheritance before.
I wonder if my compiler can answer that question for me?
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
class B
{
public:
 B(){n=0;}
 B(int nn){n=nn;}
 void f(){cout << "f\n";}
 void g(){cout << "g\n";}
private:
 int n;
};

class D: private B
{
public:
 D(int nn, float dd): B(nn), d(dd){}
 void h(){ f(); cout << "h\n";}// h calls f
private:
 double d;
};

int main ()
{
    D d(5,3);
    d.h();// OK. Calling f in a D member function is OK.
    d.f();// Not OK. See below
    ||=== Build: Release in forumProbs (compiler: GNU GCC Compiler) ===|
C:\codeBlocksProjects\forumProbs\main.cpp||In function 'int main()':|
C:\codeBlocksProjects\forumProbs\main.cpp|29|error: 'void B::f()' is inaccessible|
C:\codeBlocksProjects\forumProbs\main.cpp|47|error: within this context|
C:\codeBlocksProjects\forumProbs\main.cpp|47|error: 'B' is not an accessible base of 'D'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|    */

    cout << '\n';
    return 0;
}

It appears that calling f or g in a D class member function is OK, but calling f or g directly in main is not.
Last edited on
Also, theres plenty of youtube videos explaining private inheritance if that tickles your fancy.
Topic archived. No new replies allowed.