#include<iostream>
using namespace std;
class A
{
public:
virtual void f()
{
printf("A.f called.\n");
}
};
class X:public A
{
public:
static const int b = 10 ;
int cvalue()
{
return c;
}
private:
int c ;
public:
X()
{
c =20;
}
void f()
{
cout<<"f called."<<endl;
}
};
int main()
{
X x;
cout<<x.b<<endl;
cout<<x.cvalue()<<endl;
x.f();
cout<<"----------------------------------"<<endl;
memset(&x, 0x0, sizeof(X));
cout<<x.b<<endl;
cout<<x.cvalue()<<endl;
x.f();
cout<<x.cvalue()<<endl;
x.f();
cout<<"_______________________"<<endl;
A* a = new X;
a->f();
memset(a, 0x0, sizeof(X));
//vptr is null.
a->f();
delete a;
return 0;
C++类继承与初始化
本文探讨了C++中类的继承与初始化机制,通过具体实例展示了如何在派生类中覆盖父类的方法并实现成员变量初始化。
31万+

被折叠的 条评论
为什么被折叠?



