c++-多态的练习
程序员文章站
2022-05-07 09:25:35
多态的几个小练习 练习一 ......
多态的几个小练习
练习一
#include <iostream> #include <string> using namespace std; class fu { public: fu(string name) { this->name = name; } virtual void func() { cout << "调用了fu的函数"<<endl; cout<<"fu " << name <<" func()"<< endl; } string name; }; class zi :public fu { public: zi(string name):fu(name) { } void func() { cout << "调用了zi的函数" << endl; cout << "zi " << name << " func()"<< endl; } }; class zi2 :public fu { public: zi2(string name) : fu(name) { } void func() { cout << "调用了zi2的函数" << endl; cout << "zi2 "<< name << " func()" << endl; } }; class sun :public zi { public: sun(string name) : zi(name) { } void func() { cout << "调用了sun的函数" << endl; cout << "sun " << name << " func()"<<endl; } }; void fun(fu &f) { f.func();//在此处应该发生多态 } int main() { fu f("ffff"); zi z("zzzz"); zi2 z2("tttt"); sun s("ssss"); fun(f); fun(z); fun(z2); fun(s); return 0; }
练习二
#include <iostream> #include <string> using namespace std; class fu { public: fu(string name) { this->name = name; } virtual void func() { cout << "调用了fu的函数"<<endl; cout<<"fu " << name <<" func()"<< endl; } string name; }; class zi :public fu { public: zi(string name):fu(name) { } void func() { cout << "调用了zi的函数" << endl; cout << "zi " << name << " func()"<< endl; } }; class zi2 :public fu { public: zi2(string name) : fu(name) { } void func() { cout << "调用了zi2的函数" << endl; cout << "zi2 "<< name << " func()" << endl; } }; class sun :public zi { public: sun(string name) : zi(name) { } void func() { cout << "调用了sun的函数" << endl; cout << "sun " << name << " func()"<<endl; } }; void fun(fu &f) { f.func();//在此处应该发生多态 } int main() { fu f("ffff"); zi z("zzzz"); zi2 z2("tttt"); sun s("ssss"); fun(f); fun(z); fun(z2); fun(s); return 0; }
练习三
#define _crt_secure_no_warnings #include <iostream> #include <string> using namespace std; //把大象关进冰箱 //冰箱类 class icebox { protected: int size;//冰箱的容积 public: icebox(int size) { this->size = size; } virtual int getsize() { return this->size; } }; class animal { protected: int size; public: animal(int size) { this->size = size; } virtual int getsize() { return this->size; } }; //大象类 class elephent:public animal { private: string name; public: elephent(int size, string name) :animal(size) { this->name = name; } virtual int getesize() { return this->size; } string getname() { return this->name; } }; class geli:public icebox { private: string name; public: geli(int size , string name) :icebox(size) { this->name = name; } virtual int getsize() { return this->size; } string getname() { return this->name; } }; void puteleintobox(icebox *ib, animal *an) { if (ib->getsize() > an->getsize()) { cout << "把动物装进去了" << endl; } else { cout << "动物卡住了" << endl; } } int main() { icebox ib(100); animal an(200); puteleintobox(&ib, &an); elephent *ep = new elephent(200, "非洲象"); geli *dongmz = new geli(300, "geli"); puteleintobox(dongmz, ep); system("pause"); return 0; }
练习四
#define _crt_secure_no_warnings #include"iostream" using namespace std; class programmer { public: virtual int salarypermonth()=0; virtual char * getname()=0; }; class cppprogrammer :public programmer { public: virtual int salarypermonth() { return 20000; } virtual char * getname() { return "cppprogrammer"; } }; class phpprogrammer :public programmer { public: virtual int salarypermonth() { return 10000; } virtual char * getname() { return "phpprogrammer"; } }; class javaprogrammer :public programmer { public: virtual int salarypermonth() { return 15000; } virtual char * getname() { return "javaprogrammer"; } }; class girl { public: virtual int beauty() { } virtual char * getname() { } }; class baifumei : public girl { public: virtual int beauty() { return 19999; } virtual char * getname() { return "baifumei"; } }; class nvdiaosi : public girl { public: virtual int beauty() { return 11000; } virtual char * getname() { return "nvdiaosi"; } }; class fengjie : public girl { public: virtual int beauty() { return 14000; } virtual char * getname() { return "fengjie"; } }; void marry(programmer &pp, girl &gg) { if (pp.salarypermonth() > gg.beauty()) { cout << pp.getname() << "\t"<<"will marry "<<gg.getname() << endl; } else { cout << "hey " << pp.getname() << " don't make a day dream! you want to marry to " << gg.getname() <<"??"<< endl; } } int main() { cppprogrammer cpp; phpprogrammer php; javaprogrammer java; baifumei bfm; nvdiaosi nds; fengjie fj; marry(cpp, bfm); marry(php, bfm); marry(java, bfm); marry(php, nds); marry(java, bfm); marry(java, fj); system("pause"); return 0; }
练习五
#define _crt_secure_no_warnings #include"iostream" using namespace std; class programmer { public: virtual int salarypermonth()=0; virtual char * getname()=0; }; class cppprogrammer :public programmer { public: virtual int salarypermonth() { return 20000; } virtual char * getname() { return "cppprogrammer"; } }; class phpprogrammer :public programmer { public: virtual int salarypermonth() { return 10000; } virtual char * getname() { return "phpprogrammer"; } }; class javaprogrammer :public programmer { public: virtual int salarypermonth() { return 15000; } virtual char * getname() { return "javaprogrammer"; } }; class girl { public: virtual int beauty() { } virtual char * getname() { } }; class baifumei : public girl { public: virtual int beauty() { return 19999; } virtual char * getname() { return "baifumei"; } }; class nvdiaosi : public girl { public: virtual int beauty() { return 11000; } virtual char * getname() { return "nvdiaosi"; } }; class fengjie : public girl { public: virtual int beauty() { return 14000; } virtual char * getname() { return "fengjie"; } }; void marry(programmer &pp, girl &gg) { if (pp.salarypermonth() > gg.beauty()) { cout << pp.getname() << "\t"<<"will marry "<<gg.getname() << endl; } else { cout << "hey " << pp.getname() << " don't make a day dream! you want to marry to " << gg.getname() <<"??"<< endl; } } int main() { cppprogrammer cpp; phpprogrammer php; javaprogrammer java; baifumei bfm; nvdiaosi nds; fengjie fj; marry(cpp, bfm); marry(php, bfm); marry(java, bfm); marry(php, nds); marry(java, bfm); marry(java, fj); system("pause"); return 0; }
练习六
#define _crt_secure_no_warnings #include <iostream> using namespace std; // class girl { public: int fangyu() { return 10; } }; class boy { public: virtual int fight() { return 5; } }; class higboy:public boy { public: virtual int fight() { return 10; } }; class bugboy :public boy { public: virtual int fight() { return 20; } }; //战斗方法 void catchgirl(boy &bp, girl &mp) { if (bp.fight() > mp.fangyu()) { //hp->getad 发生了多态 cout << "女孩被追到了" << endl; } else { cout << "没追到" << endl; } } int main(void) { girl mp; boy b1; higboy b2; bugboy b3; catchgirl(b1, mp); catchgirl(b2, mp); catchgirl(b3, mp); // system("pause"); return 0; }
练习七
#define _crt_secure_no_warnings #include <iostream> #include <string> #include <cstdlib> using namespace std; class person { public: person(char * name,int age) { this->name = name; this->age = age; } virtual void aryyouok() { cout << "name: " << this->name << endl; cout << "age: " << this->age << endl; } string getname() { return name; } int getage() { return age; } private: string name; int age; }; class teacher : public person { public: teacher(char * name, int age, int wage) :person(name, age) { this->wage = wage; } virtual void aryyouok() { person::aryyouok(); cout << "wage:" << this->wage << endl; } private: int wage; }; class student:public person { public: student(char * name, int age, char * work) :person(name, age) { this->work = work; } virtual void aryyouok() { person::aryyouok(); cout << "work:" << this->work << endl; } private: string work; }; void seehello(person & p) { p.aryyouok(); } int main(void) { student stu("íõ¶þ¹·", 18, "ñ§ï°"); teacher tea("°×½à",22, 8000); seehello(stu); cout << endl; seehello(tea); cout << endl; // system("pause"); return 0; }
练习八
#define _crt_secure_no_warnings #include <iostream> #include <string> #include <cstdlib> using namespace std; class person { public: person(char * name,int age) { this->name = name; this->age = age; } virtual void aryyouok() { cout << "name: " << this->name << endl; cout << "age: " << this->age << endl; } string getname() { return name; } int getage() { return age; } private: string name; int age; }; class teacher : public person { public: teacher(char * name, int age, int wage) :person(name, age) { this->wage = wage; } virtual void aryyouok() { person::aryyouok(); cout << "wage:" << this->wage << endl; } private: int wage; }; class student:public person { public: student(char * name, int age, char * work) :person(name, age) { this->work = work; } virtual void aryyouok() { person::aryyouok(); cout << "work:" << this->work << endl; } private: string work; }; void seehello(person & p) { p.aryyouok(); } int main(void) { student stu("íõ¶þ¹·", 18, "ñ§ï°"); teacher tea("°×½à",22, 8000); seehello(stu); cout << endl; seehello(tea); cout << endl; // system("pause"); return 0; }
练习九
#define _crt_secure_no_warnings #include <iostream> #include <string> #include <cstdlib> using namespace std; class person { public: person(char * name,int age) { this->name = name; this->age = age; } virtual void aryyouok() { cout << "name: " << this->name << endl; cout << "age: " << this->age << endl; } string getname() { return name; } int getage() { return age; } private: string name; int age; }; class teacher : public person { public: teacher(char * name, int age, int wage) :person(name, age) { this->wage = wage; } virtual void aryyouok() { person::aryyouok(); cout << "wage:" << this->wage << endl; } private: int wage; }; class student:public person { public: student(char * name, int age, char * work) :person(name, age) { this->work = work; } virtual void aryyouok() { person::aryyouok(); cout << "work:" << this->work << endl; } private: string work; }; void seehello(person & p) { p.aryyouok(); } int main(void) { student stu("íõ¶þ¹·", 18, "ñ§ï°"); teacher tea("°×½à",22, 8000); seehello(stu); cout << endl; seehello(tea); cout << endl; // system("pause"); return 0; }
上一篇: 大话设计模式笔记(二十五)の访问者模式