多态的几个小练习

练习一

#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;
}

最新文章

  1. let和const命令//////////////////////z
  2. js去空格
  3. 调整static变量初始化顺序的一个办法
  4. nginx ssl证书安装配置
  5. js动态获取当前系统时间+js字符串转换为date日期对象
  6. Redis应用场景
  7. mybatis(三)懒加载
  8. python中的Unittest常用方法
  9. protobuf 文件级别优化
  10. 开源 免费 java CMS - FreeCMS2.0 会员我的评论
  11. Android应用程序启动过程源代码分析
  12. matrix67:kmp算法详解
  13. 基于visual Studio2013解决C语言竞赛题之0701排队输出
  14. bzoj2152
  15. Git详细教程---多人协作开发
  16. 数字类型——python3
  17. LeetCode专题-Python实现之第27题:Remove Element
  18. 【原创】大数据基础之ElasticSearch(5)重要配置及调优
  19. sklearn模型的属性与功能-【老鱼学sklearn】
  20. Scala-Unit7-Scala并发编程模型AKKA

热门文章

  1. 关于Java中2.0-1.1!=0.9的问题
  2. 实现自定义的参数解析器——HandlerMethodArgumentResolver
  3. linux hwclock
  4. 不止面试02-JVM内存模型面试题详解
  5. 了解HTTP协议,这一篇就够了
  6. 几种常见设计模式在项目中的应用&lt;Singleton、Factory、Strategy&gt;
  7. centos 7 MysSQL 5.7.23 源码安装
  8. scrapy请求传参-BOSS反爬
  9. OSI-传输层
  10. maven中的setting文件