假定有一排蜂房,yips症候群,夫君们让我捏一下
#define _crt_secure_no_warnings #include <iostream> #include <string> using namespace std; //岳不群 class yuebuqun { public: yuebuqun( string kongfu) { this->kongfu = kongfu; } virtual void fight() //标识修饰一个成员方法是一个虚函数。 { cout << "岳不群" << "使出了" << kongfu << "打人" << endl; } void print() { } string kongfu; }; //林平之 继承了 岳不群 class linpingzhi :public yuebuqun { public: linpingzhi(string kongfu) :yuebuqun(kongfu) { } //如果说父类中有一个虚函数是fight( ), 子类如果去重写这个虚函数。 void fight() { cout << "林平之" << "使出了" << kongfu << "打人" << endl; } void print() { } }; class linghuchong :public yuebuqun { public: linghuchong(string kongfu) :yuebuqun(kongfu) { } void fight() { cout << "令狐冲 " << "使用了" << kongfu << endl; } }; //在全局提供一个打斗的方法 void fightpeople(yuebuqun *hero)//yuebuqun *hero = xiaopp; yuebuqun *hero = xiaoyy; { cout << "调用打人的方法" << endl; hero->fight();//希望传递进来的如果是子类,调用子类的fight //如果传递进来的是父类, 调用父类的fight //这种行为就是 多态行为。 } int main(void) { yuebuqun *xiaoyy = new yuebuqun("葵花宝典"); //xiaoyy->fight(); linpingzhi *xiaopp = new linpingzhi("僻邪剑谱"); //xiaopp->fight(); linghuchong *xiaoll = new linghuchong("独孤九剑"); fightpeople(xiaoyy); fightpeople(xiaopp); fightpeople(xiaoll); //编译器默认做了一个安全的处理。 编译认为 不管传递时子类对象还是父类对象, //如果统一执行父类d方法 那么是一定可以被成功执行。 delete xiaoyy; delete xiaopp; delete xiaoll; return 0; }
#define _crt_secure_no_warnings #include <iostream> using namespace std; //英雄类 //1999 class hero { public: virtual int getad() { return 10; } }; //1999 class advhero :public hero { public: virtual int getad() { return 1001; } }; //怪兽类 //1999 class monster { public: int getad() { return 1000; } }; //战斗方法 //1999 //写的架构函数,可以调用未来。 void playerfight(hero *hp, monster *mp) { //多态对于编译器来讲的,也是一个动态联编,也是一个迟邦定。 if (hp->getad() > mp->getad()) { //hp->getad 发生了多态 cout << "英雄胜利, 怪兽被打死" << endl; } else { cout << "英雄挂了,怪兽赢了" << endl; } } //2020年 class bughero :public hero { public: virtual int getad() { cout << "调用了bughero的方法" << endl; return 66666; } }; int main(void) { hero h; monster m; playerfight(&h, &m); advhero advh; playerfight(&advh, &m); bughero bh; playerfight(&bh, &m); int a = 10; int b = 20; cout << a << endl; if (a > 10) { //迟邦定 cout << "a>10" << endl; } else { cout << "a < 10" << endl; } return 0; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论