赞
踩
- #include <iostream>
-
- using namespace std;
-
- class RemoteCon{
- public:
- virtual void openUtils(){
- cout << "遥控器的开被按下" << endl;
- }
- void closeUtils()
- {
- cout<<"遥控器的关被按下"<<endl;
- }
- };
- class TvRemoteCon : public RemoteCon{
- public:
- void openUtils() override{
- cout << "电视遥控器的开被按下" << endl;
- }
- };
-
- void testFunc(){
- }
-
- class RoundspeakerCon : public RemoteCon{
- public:
- void openUtils() override{
- cout << "音响遥控器的开被按下" << endl;
- }
- };
-
- class LightCon : public RemoteCon{
- public:
- void openUtils() override{
- cout << "灯光遥控器的开被按下" << endl;
- }
- };
-
- void test(RemoteCon& r)//引用的方式
- {
- r.openUtils();
- }
- int main()
- {
- RemoteCon *remoteCon = new TvRemoteCon; //多态
- remoteCon->openUtils();
- RemoteCon *remoteCon2 = new RoundspeakerCon; //多态
- remoteCon2->openUtils();
- RemoteCon *remoteCon3 = new LightCon; //多态
- remoteCon3->openUtils();
-
- TvRemoteCon tvRmoteCon;
- test(tvRmoteCon);
-
- }
- #include <iostream>
- using namespace std;
- class Teacher{
- public:
- string name;
- string shool;
- string major;
- virtual void goInClass() = 0;
- virtual void startTeaching() = 0;
- virtual void afterTeaching() = 0;
- void chat();
- };
- class EnglishTeacher : public Teacher{
- public:
- void goInClass() override{
- cout << "英语老师开始进入教室" << endl;
- }
-
- void startTeaching() override{
- cout << "英语老师开始教学" << endl;
- }
-
- void afterTeaching() override{
- };
- };
- class ProTeacher : public Teacher{
- public:
- void goInClass() override{
- cout << "编程老师开始进入教室" << endl;
- }
- void startTeaching() override{
- cout << "编程老师开始撸代码了,拒绝读PPT" << endl;
- }
- void afterTeaching() override{
- cout << "编程老师下课后手把手教x学员写代码" << endl;
- };
- };
- int main()
- {
- // Teacher t;//抽象类,不支持被实例化
- EnglishTeacher e;
- e.goInClass();
- ProTeacher t;
- t.startTeaching();
- t.afterTeaching();
- //抽象类,多态
- Teacher *teacher = new ProTeacher;
- teacher->startTeaching();
- return 0;
- }
- #include <iostream>
- using namespace std;
- class BasketBallMove{
- public:
- virtual void passTheBall() = 0;
- };
- class LiveMove{
- public:
- virtual void eat() = 0;
- virtual void bite() = 0;
- virtual void drink() = 0;
- virtual void la() = 0;
- };
- class Human : public LiveMove,BasketBallMove{
- public:
- void eat() override{};
- void bite() override{};
- void drink() override{};
- void la() override{};
- void passTheBall() override{};
- };
- class Dog : public LiveMove{
- public:
- void eat() override{};
- void bite() override{};
- void drink() override{};
- void la() override{};
- };
- int main()
- {
- Human h;
- h.eat();
- Dog g;
- g.eat();
- // LiveMove *l = new LiveMove;
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。