#include <iostream.h>
class A // 父类A
{
public:
A(int x, int y)
: a(x), b(y) { cout<<"Address of object A: "<<this<<endl; }
//private:
int a;
int b;
};
class B:public A // 子类B
{
public:
B(int x, int y, int m, int n)
: A(x,y), a(m), b(n) { cout<<"Address of object B: "<<this<<endl; }
//private:
int a;
int b;
};
void main()
{
A a(10,20);
cout<<a.a<<" "<<a.b<<endl;
B b(1,2,3,4);
cout<<sizeof(a)<<" "<<sizeof(b)<<endl;
cout<<b.a<<" "<<b.b<<endl;
cout<<b.A::a<<" "<<b.A::b<<endl; //access class A's member data overrided in class B
}
class A // 父类A
{
public:
A(int x, int y)
: a(x), b(y) { cout<<"Address of object A: "<<this<<endl; }
//private:
int a;
int b;
};
class B:public A // 子类B
{
public:
B(int x, int y, int m, int n)
: A(x,y), a(m), b(n) { cout<<"Address of object B: "<<this<<endl; }
//private:
int a;
int b;
};
void main()
{
A a(10,20);
cout<<a.a<<" "<<a.b<<endl;
B b(1,2,3,4);
cout<<sizeof(a)<<" "<<sizeof(b)<<endl;
cout<<b.a<<" "<<b.b<<endl;
cout<<b.A::a<<" "<<b.A::b<<endl; //access class A's member data overrided in class B
}
运行结果:
Address of object A: 0x0012FF78
10 20
Address of object A: 0x0012FF68
Address of object B: 0x0012FF68
8 16
3 4
1 2