多態的幾個小練習
練習一
#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;
}