有一大段代码,很多类都需要用到
难道你要每个类都写一遍吗?
想想就很繁琐
所以今天我们说一下继承
模板就
class 子类:(权限) 父类
大家可以看下面的代码了解一下
#include<iostream>
using namespace std;
class father
{
public:
void head()
{
cout << "首页" << endl;
}
void back()
{
cout << "尾页" << endl;
}
};
class son1 :public father
{
public:
void content()
{
cout << "大儿子的页面" << endl;
}
};
class son3 :public father
{
public:
void content()
{
cout << "三儿子的页面" << endl;
}
};
class son2 :public father
{
public:
void content()
{
cout << "二儿子的页面" << endl;
}
};
void test1()
{
cout << "大儿子展示" << endl;
son1 s1;
s1.head();
s1.back();
s1.content();
cout << "-----------------" << endl;
cout << "二儿子展示" << endl;
son2 s2;
s2.head();
s2.back();
s2.content();
cout << "-----------------" << endl;
cout << "三儿子展示" << endl;
son3 s3;
s3.head();
s3.back();
s3.content();
cout << "-----------------" << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
显然对于其中重复的
void head()
{
cout << "首页" << endl;
}
void back()
{
cout << "尾页" << endl;
}
我们就只写了一次?
?
|