多态的几个小练习

练习一

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

c++-多态的练习的更多相关文章

  1. Java中的多态

    1.多态:具有表现多种形态的能力的特征 父类: public abstract class Animal {public abstract void Say();} 子类: public class ...

  2. C# 工厂模式+虚方法(接口、抽象方法)实现多态

    面向对象语言的三大特征之一就是多态,听起来多态比较抽象,简而言之就是同一行为针对不同对象得到不同的结果,同一对象,在不同的环境下得到不同的状态. 实例说明: 业务需求:实现一个打开文件的控制台程序的d ...

  3. C#非常重要基础之多态

    前几天看了一位同志的博客,写的是关于他自己去支付宝面试的经历.过程大体是这样的:问答的时候,前面部分,作者都应答如流,说起自己经验如何之丰富,最后面试官问了作者一个问题:请简述多态的概念和作用.结果这 ...

  4. C++多态详解

    多态是面向对象的程序设计的关键技术.多态:调用同一个函数名,可以根据需要但实现不同的功能.多态体现在两个方面,我们以前学过的编译时的多态性(函数重载)和现在我们这一章将要学习的运行时的多态性(虚函数) ...

  5. 【那些年关于java多态应用】

    1.多态:具有表现多种形态的能力的特征 父类: public abstract class Animal { public abstract void Say();} 子类: public class ...

  6. JAVA多态

    多态是指当系统A访问系统B的服务时,系统B可以通过多种方式来提供服务,而这一切对系统A是透明的.比如动物园的饲养员能够给各种各样的动物喂食.下图显示了饲养员Feeder,食物Food和动物Animal ...

  7. C#多态“说来也说”——逻辑层BLL中的多态使用

    本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址 http://www.cnblogs.com/tdws/p/5861842.html 昨天晚上,有个朋友说学了好久,依然没搞 ...

  8. java多态的理解

    面向对象语言中的类有三个特征,封装.继承.多态.封装与继承很好理解,那什么是多态呢? 1.什么是多态? 多态的定义:指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同 ...

  9. java中如何实现多态

    复习基础知识 多态,就是重载和重写.重载发生在一个类中.重写发生在子类,意思就是子类重写父类相同名称的方法.刚学语言有的东西,不必搞得那么清楚,只有知道怎么用就行了,有的问题你要想真正把它搞得很懂,短 ...

  10. OC多态

    要点: 1.多种形态,引用的多种形态对于一个引用变量,可以指向任何类的对象.对于一个父类的引用(类与类之间有一种继承关系),可以指向子类,也可以指向本类,指向的类型不同.当通过此引用向对象发送消息,调 ...

随机推荐

  1. 【Luogu P3834】可持久化线段树(主席树)

    Luogu P3834 可持久化数据结构就是支持在历史版本上进行查询和修改操作的数据结构. 主席树就是对线段树的改进,使之可持久化. 前置知识:动态开点线段树 我们利用权值(值域)线段树统计区间内的数 ...

  2. Selenium+Java(三)Selenium元素定位

    前言 使用Selenium做元素定位的时候,需要用到HTML的知识,所以最好是能懂得HTML的基本知识. 一.页面元素的查看(以百度为例) 打开IE浏览器,点击F12进入开发者模式,点击图中红圈圈中的 ...

  3. AVLTree的Python实现

    AVLTree 自己最近在学习数据结构,花了几天理解了下AVLTree的实现,简单一句话概括就是先理解什么是旋转,然后弄明白平衡因子在各种旋转后是如何变化的.最后整理了下学习的过程,并尽量用图片解释, ...

  4. Nginx+MySQL+PHP+Redis多机部署(测试发布discuz论坛)

    链接:LNMP+Redis单机部署 1.实战多机部署环境 nginx服务器: 192.168.1.3 php服务器:    192.168.1.4 mysql服务器: 192.168.1.10 red ...

  5. LVS+Keepalived-DR模式(Apache)

    Environment:4台CentOS机器 两台LVS 两台web服务器 LVS主备的操作,都需要安装ipvsadm和keepalived LVS主机操作 : 1.更改Keepalived的配置文件 ...

  6. 引用公共页面的js函数报错

    对于网站来说很多页面之间都有着大量的共享模块,如页头,页脚和用户栏等.很多时候为了方便.省事,我们在公共模块写函数,然后在别的页面里调用.但我们在引用公共的js函数时,有些可以引用,有些却报错:这是因 ...

  7. 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程(下)

    [摘要] cluster模块详解 示例代码托管在:http://www.github.com/dashnowords/blogs 阅读本章需要先阅读本系列前两章内容预热一下. 一. 引言 前两篇博文中 ...

  8. MyBatis系列(一) MyBatis入门

    前言 MyBatis官方文档:https://mybatis.org/mybatis-3/zh/index.html MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由 ...

  9. iOS开发-KVO的奥秘

    转自:http://www.jianshu.com/p/742b4b248da9 序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣 ...

  10. 爬虫学习(二)--爬取360应用市场app信息

    欢迎加入python学习交流群 667279387 爬虫学习 爬虫学习(一)-爬取电影天堂下载链接 爬虫学习(二)–爬取360应用市场app信息 代码环境:windows10, python 3.5 ...