Hi all
I was thinking on the behavior of inheritance. So I have declared protected constructor in base class. In derived class, I inherited base class as private, I have created a function which return a base class object.
Below is the code.
Code:
#include<iostream>
using namespace std;
class A
{
int a;
void getData()
{
cout << "\n Inside A getData()";
}
protected:
A(){}
};
class B : private A
{
int b;
public:
void getData()
{
cout << "\n Inside B getData()";
}
A* getBase()
{
A *a = new A(); // error line
return a;
}
B() {}
};
int main()
{
B b;
b.getData();
getchar();
return 0;
}
At error line, I am getting below error:
cannot access protected member declared in class 'A'
I am not able to understand that when I create B b; it can access the A constructor ( as constructor is protected), then why not when I am creating A object?
Can any body help?
thanks
Bargi