64,683
社区成员
发帖
与我相关
我的任务
分享
#include "iostream"
using namespace std;
class Parent
{
public:
void printP()
{
a = 1;
cout << "a=" << a << endl;
printf("我是爹。。。\n");
}
protected:
int a;
int b;
};
class Child : public Parent
{
public:
Child()
{
a = 0; b = 0; c = 0;
}
void printC()
{
c = 2;
cout << "c=" << c << endl;
printf("我是儿子。。。\n");
}
protected:
private:
int c;
};
void main()
{
Parent a;//定义一个父类对象
Child *q = (Child*)(&a);//派生类指针强制指向父类对象
q->printC();//派生类指针调用派生类函数(涉及内部数据),
system("pause");
}