Meine Fragen sind, warum keinen Kompilierungsfehler verursacht?Warum Sub-Klasse auf C++ private Vererbung Mitglied zugreifen?
Klasse B
ist privat, vererben Klasse A
und x
und y
sind Mitglieder der Klasse A
.
class A {
public:
int x;
int y;
void set(int a, int b) { x = a; y =b;}
};
class B : private A{
public:
int getmultiply (void){
return x*y;}
};
int main(void)
{
B b;
//b.set(3,4); // this will cause compilation error
cout << b.getmultiply(); // why this will not??
return 0;
}
Danke und ich verstehe es endlich. – jmmom