06 继承的引出_
非继承的写法
#include <iostream>
using namespace std;
class News
{
public:
void header()
{
cout << "公共头部" << endl;
}
void footer()
{
cout << "公共底部" << endl;
}
void left()
{
cout << "左侧列表" << endl;
}
void content()
{
cout << "新闻播报" << endl;
}
};
class YULE
{
public:
void header()
{
cout << "公共头部" << endl;
}
void footer()
{
cout << "公共底部" << endl;
}
void left()
{
cout << "左侧列表" << endl;
}
void content()
{
cout << "白百何-------" << endl;
}
};
void Test()
{
//新闻页
News news;
news.content();
news.footer();
news.header();
news.left();
//娱乐页
YULE yule;
yule.content();
yule.footer();
yule.header();
yule.left();
}
int main()
{
Test();
// std::cout << "Hello World!\n";
}
继承写法
//继承写法
//抽象一个 基类的网页,重复的代码都写在这个网页上
class BasePage
{
public:
void header()
{
cout << "公共头部" << endl;
}
void footer()
{
cout << "公共底部" << endl;
}
void left()
{
cout << "左侧列表" << endl;
}
};
class NewsPage :public BasePage
{
public:
void content()
{
cout << "新闻播报" << endl;
}
};
class YULEPage :public BasePage
{
public:
void content()
{
cout << "白百何-------" << endl;
}
};
class GamePage :public BasePage
{
public:
void content()
{
cout << "游戏新闻" << endl;
}
};
void Test02()
{
cout << "新闻网页-------" << endl;
NewsPage news;
news.content();
news.footer();
news.header();
news.left();
cout << "娱乐网页-------" << endl;
YULEPage yule;
yule.content();
yule.footer();
yule.header();
yule.left();
cout << "游戏网页-------" << endl;
GamePage game;
game.content();
game.footer();
game.header();
game.left();
}
//继承,减少代码重复性
//BasePage 基类 子类
//NewsPage 派生类 父类
//YULEPage 派生类 父类
//GamePage 派生类 父类
int main()
{
Test02();
// std::cout << "Hello World!\n";
}

浙公网安备 33010602011771号