#include <iostream>
using namespace std;//运算符的重载
class Base//
{
public:
int a;
Base()
{
cout << "父类的不带参的构造函数调用" << endl;
}
Base(int a)
{
cout << "父类的带参数的构造函数调用" << endl;
}
~Base()
{
cout << "父类的析造函数调用" << endl;
}
void go()//父类
{
cout << "父类的go()" << endl;
}
void go(int a)//父类
{
this->a = a;
cout << "父类的有参数的go()" << endl;
}
};
//父类 = 基类 子类 = 派生类
class Derive :public Base//学生,公有的继承
{
public:
Derive() :Base(10)//构造函数,继承父类
{
cout << "子类的构造函数调用" << endl;
}
~Derive()
{
cout << "子类的析造函数调用" << endl;
}
void go()//重写这个go(),继承父类
{
cout << "子类的无参数的go()" << endl;
}
void go(int a)//重写这个go(),继承父类
{
cout << "子类的带参的go()" << endl;
}
};
void main()
{
{
Derive child;
child.go();//子类的go()
child.Base::go();//父类的go(),这个是子类调用父类的方法
child.go(10);
}
system("pause");
}
子类父类 构造函数析构函数
最新推荐文章于 2024-07-12 14:22:12 发布