面向对象编程自学笔记整理
十一_四、类与对象
6、多态
6.1、多态的基本概念
#include<iostream>
using namespace std;
class animal
{
public:
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
class cat :public animal
{
public:
void speak()
{
cout << "小猫在说话" << endl;
}
};
class dog :public animal
{
public:
void speak()
{
cout << "小狗在说话" << endl;
}
};
void dospeak(animal &ani)
{
ani.speak();
}
void test01()
{
cat CA;
dospeak(CA);
dog DG;
dospeak(DG);
}
void test02()
{
cout << "sizeof(animal)=" << sizeof(animal) << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
6.2、多态案例1-计算器类
#include<iostream>
#include<string>
using namespace std;
class calculator
{
public:
double getresult(string oper)
{
if (oper == "+")
{
return m_num1 + m_num2;
}
else if (oper == "-")
{
return m_num1 - m_num2;
}
else if (oper == "*")
{
return m_num1 * m_num2;
}
else if (oper == "/")
{
return m_num1 / m_num2;
}
}
int m_num1;
int m_num2;
};
void test01()
{
calculator c;
c.m_num1 = 10;
c.m_num2 = 10;
cout << c.m_num1 << "+" << c.m_num2 << "=" << c.getresult("+") << endl;
cout << c.m_num1 << "-" << c.m_num2 << "=" << c.getresult("-") << endl;
cout << c.m_num1 << "*" << c.m_num2 << "=" << c.getresult("*") << endl;
cout << c.m_num1 << "/" << c.m_num2 << "=" << c.getresult("/") << endl;
}
class abstractcalculator
{
public:
virtual int getresult()
{
return 0;
}
int m_num1;
int m_num2;
};
class addcalculator :public abstractcalculator
{
public:
int getresult()
{
return m_num1 + m_num2;
}
};
class subcalculator :public abstractcalculator
{
public:
int getresult()
{
return m_num1 - m_num2;
}
};
class mulcalculator :public abstractcalculator
{
public:
int getresult()
{
return m_num1 * m_num2;
}
};
void test02()
{
abstractcalculator* abc;
abc=new addcalculator;
abc->m_num1 = 20;
abc->m_num2 = 10;
cout << abc->m_num1 << "+" << abc->m_num2 << "=" << abc->getresult()<<endl;
delete abc;
abc = new subcalculator;
abc->m_num1 = 250;
abc->m_num2 = 10;
cout << abc->m_num1 << "-" << abc->m_num2 << "=" << abc->getresult() << endl;;
delete abc;
abc = new mulcalculator;
abc->m_num1 = 200;
abc->m_num2 = 30;
cout << abc->m_num1 << "*" << abc->m_num2 << "=" << abc->getresult() << endl;;
delete abc;
}
int main()
{
test02();
system("pause");
return 0;
}
6.3、纯虚函数和抽象类
#include<iostream>
using namespace std;
class base
{
public:
virtual void func() = 0;
};
class son1 :public base
{
public:
void func()
{
cout << "子类1的func函数的调用" << endl;
}
};
class son2 :public base
{
public:
void func()
{
cout << "子类2的func函数的调用" << endl;
}
};
void test01()
{
base* b=new son1;
b->func();
base* c = new son2;
c->func();
}
int main()
{
test01();
system("pause");
return 0;
}
6.4、多态案例2-制作饮品
#include<iostream>
using namespace std;
class abstractdrinking
{
public:
virtual void boil() = 0;
virtual void brew() = 0;
virtual void pourincup() = 0;
virtual void putsomething() = 0;
virtual void makedrink()
{
boil();
brew();
pourincup();
putsomething();
}
};
class coffee: public abstractdrinking
{
public:
virtual void boil()
{
cout << "煮农夫山泉水"<<endl;
}
virtual void brew()
{
cout << "冲泡咖啡" << endl;
}
virtual void pourincup()
{
cout << "倒入杯中" << endl;
}
virtual void putsomething()
{
cout << "加入牛奶和糖" << endl;
}
};
class tea : public abstractdrinking
{
public:
virtual void boil()
{
cout << "煮百岁山水" << endl;
}
virtual void brew()
{
cout << "冲泡茶叶" << endl;
}
virtual void pourincup()
{
cout << "倒入杯中" << endl;
}
virtual void putsomething()
{
cout << "加入枸杞" << endl;
}
};
void dowork(abstractdrinking *abs)
{
abs->makedrink();
delete abs;
}
void test01()
{
cout << "咖啡制作:" << endl;
dowork(new coffee);
cout << "--------------------------------" << endl;
cout << "茶的制作:" << endl;
dowork(new tea);
}
int main()
{
test01();
system("pause");
return 0;
}
6.5、虚析构和纯虚析构
#include<iostream>
using namespace std;
#include<string>
class animal
{
public:
animal()
{
cout << "animal构造函数调用" << endl;
}
virtual ~animal() = 0;
virtual void speak() = 0;
};
animal::~animal()
{
cout << "animal纯虚析构函数调用" << endl;
}
class cat :public animal
{
public:
cat(string name)
{
cout << "cat构造函数调用" << endl;
m_name=new string(name);
}
virtual void speak()
{
cout <<*m_name <<"小猫喵喵喵" << endl;
}
~cat()
{
if (m_name != NULL)
{
cout << "cat析构函数调用" << endl;
delete m_name;
m_name = NULL;
}
}
string* m_name;
};
void test01()
{
animal* an=new cat("汤姆");
an->speak();
delete an;
}
int main()
{
test01();
system("pause");
return 0;
}
6.6、多态案例3-电脑组装
#include<iostream>
using namespace std;
class CPU
{
public:
virtual void calculator() = 0;
};
class videocard
{
public:
virtual void display() = 0;
};
class memory
{
public:
virtual void storage() = 0;
};
class computer
{
public:
computer(CPU* cpu, videocard* vcd, memory* stor)
{
m_cpu=cpu;
m_vcd=vcd;
m_stor=stor;
}
void dowork()
{
m_cpu->calculator();
m_vcd->display();
m_stor->storage();
}
~computer()
{
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if (m_vcd != NULL)
{
delete m_vcd;
m_vcd= NULL;
}
if (m_stor != NULL)
{
delete m_stor;
m_stor = NULL;
}
}
private:
CPU* m_cpu;
videocard* m_vcd;
memory* m_stor;
};
class InterCPU:public CPU
{
public:
virtual void calculator()
{
cout << "Inter的CPU开始计算了" << endl;
}
};
class InterVideocard :public videocard
{
public:
virtual void display()
{
cout << "Inter的显卡开始显示了" << endl;
}
};
class Intermemory :public memory
{
public:
virtual void storage()
{
cout << "Inter的内存条开始存储了" << endl;
}
};
class LenovoCPU :public CPU
{
public:
virtual void calculator()
{
cout << "Lenovo的CPU开始计算了" << endl;
}
};
class LenovoVideocard :public videocard
{
public:
virtual void display()
{
cout << "Lenovo的显卡开始显示了" << endl;
}
};
class Lenovomemory :public memory
{
public:
virtual void storage()
{
cout << "Lenovo的内存条开始存储了" << endl;
}
};
void test01()
{
CPU* interCPU = new InterCPU;
videocard* intercard = new InterVideocard;
memory* interMemory = new Intermemory;
cout << "第一台电脑开始工作。。。" << endl;
computer* computer1 = new computer(interCPU, intercard, interMemory);
computer1->dowork();
delete computer1;
cout << "---------------------------------" << endl;
cout << "第二台电脑开始工作。。。" << endl;
computer* computer2 = new computer(new LenovoCPU, new LenovoVideocard, new Lenovomemory);
computer2->dowork();
delete computer2;
cout << "---------------------------------" << endl;
cout << "第三台电脑开始工作。。。" << endl;
computer* computer3 = new computer(new InterCPU, new LenovoVideocard, new Intermemory);
computer3->dowork();
delete computer3;
}
int main()
{
test01();
system("pause");
return 0;
}