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() << "	"<<"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() << "	"<<"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;
}
原文地址:https://www.cnblogs.com/ygjzs/p/12079018.html