How come the following code
Code:
#include <iostream>
using namespace std;
class A {
int Num;
char Word[12];
public:
A(int num, char* word) : Num(num) {
strncpy(Word, word, 12);
cout << "Building A!\n";
}
void Print()
{
cout << "Num = " << Num << " Word = " << Word << endl;
}
};
class B: private A {
float fNum;
public:
B(int num, char* word, float f) : A(num, word), fNum(f) {
cout << "Building B! \n";
}
void Print() {
cout << "I'm B!\n";
cout << "fNum = " << fNum << endl;
A::Print();
}
};
class C: public B {
A myA;
public:
C(int num, char* word) : B(num, word, 5.3), myA(num, word) {
cout << "Building C!\n";
}
void Print() {
cout << "I'm C!\n"; myA.Print(); B::Print();
}
};
void main() {
C FunAndAmusement(3, "Seven");
FunAndAmusement.Print();
}
Gives me the following error:
Code:
Error 2 error C2247: 'A' not accessible because 'B' uses 'private' to inherit from 'A'
It is true that B uses private to inherit from A, but how does this have to do with the fact that i'm using an A data member inside C and using C's constructor init line to initialize it's A datamember?
EDIT: Is this because B inherits private from A and that includes A's constructor as well, and that's why C cannot use A's constructor since it doesnt have access to it? if so, is there a way to override it and use A as it's datamember? like putting A's constructor in the "protected" or "public" section in class C?
Thanks.