c++-多态的练习
2019-12-22 16:01:24来源:博客园 阅读 ()
c++-多态的练习
多态的几个小练习
练习一
#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("í??t1·", 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("í??t1·", 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("í??t1·", 18, "?§?°");
Teacher tea("°×?à",22, 8000);
seeHello(stu);
cout << endl;
seeHello(tea);
cout << endl;
// system("pause");
return 0;
}
原文链接:https://www.cnblogs.com/ygjzs/p/12079018.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:c++-虚析构函数
- 虚函数实现多态性 代码参考 2020-04-28
- C++值多态:传统多态与类型擦除之间 2020-04-15
- c++中的多态机制 2020-04-04
- 虚函数表与多态的认知 2020-03-28
- C++ 多态 2020-03-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash