Concept of Class inheritance

#include <iostream>
using namespace std;


class A
{
protected:

void functionA()
{
cout << "\nI am in Function A of Class A" << endl;
}
};

class B : private A
{
public:
void functionB()
{
cout << "\nI am in Function B of Class B" << endl;
}
};

class C : public A
{
public:
void functionC()
{
cout << "\nI am in Function C of Class C" << endl;
}
};

int main()
{
A a;
B b;
C c;


//a.functionA();
b.functionB();
b.functionA(); // error generated here
return 0;
}


I am trying to keep the member functions of Class A secret.
But Class B and Class C can access them and use them
However, member functions of Class A should not be accessed directly.
Anybody who wish to access member functions of Class A must access via Class B or C
Class A is like a base class
Class B and C and derived from Class A.

Please help me in understand this concept of public protected and private
And how to resolve this situation.


thanks in advance
You say:

I am trying to keep the member functions of Class A secret.


But then you say:

Anybody who wish to access member functions of Class A must access via Class B or C


Which means they aren't secret, since the user knows about them.
Mate,

No one should be able to modify the functions of Class A

I think I figured out the solution.


#include <iostream>
using namespace std;


class A
{
protected:

void functionA()
{
cout << "\nI am in Function A of Class A" << endl;
}
};

class B : private A
{
public:
void functionB()
{
cout << "\nI am in Function B of Class B" << endl;
functionA();

}
};

class C : public A
{
public:
void functionC()
{
cout << "\nI am in Function C of Class C" << endl;
functionA();

}
};

int main()
{
A a;
B b;
C c;


//a.functionA();
//b.functionB();
b.functionB();
c.functionC();
return 0;
}



Ahh, I get it. Yeah, you basically just create wrapper functions for the functions in class A.
Topic archived. No new replies allowed.