#include
"
iostream.h
"
class A {
public :
int num;
A() {
num = 1 ;
}
virtual void Display() {
cout << " A " ;
}
void print() {
cout << num;
}
static void show() {
Aa;
a.Display();
a.print();
}
} ;
class B: public A {
public :
int num;
B() {
num = 2 ;
}
void Display() {
cout << " B " ;
}
void print() {
cout << num;
}
virtual void show() {
Display();
print();
}
} ;
class C: public B {
public :
int num;
C() {
num = 3 ;
}
void Display() {
cout << " C " ;
}
virtual void print() {
cout << num;
}
void show() {
Display();
A::print();
}
} ;
void main()
{
B * b = new C;
cout << " b->num= " << b -> num << endl;
}
class A {
public :
int num;
A() {
num = 1 ;
}
virtual void Display() {
cout << " A " ;
}
void print() {
cout << num;
}
static void show() {
Aa;
a.Display();
a.print();
}
} ;
class B: public A {
public :
int num;
B() {
num = 2 ;
}
void Display() {
cout << " B " ;
}
void print() {
cout << num;
}
virtual void show() {
Display();
print();
}
} ;
class C: public B {
public :
int num;
C() {
num = 3 ;
}
void Display() {
cout << " C " ;
}
virtual void print() {
cout << num;
}
void show() {
Display();
A::print();
}
} ;
void main()
{
B * b = new C;
cout << " b->num= " << b -> num << endl;
}
继承之后
类B中的num不会覆盖A中的num,继承之后B中A中的num不可见,除非A::num才可以
类C中的num也不会覆盖B和A中的num,也是同样的道理
当一个c对象地址转成B地址,可见的就是B类中num,所以打印出来的就是2