多态是C++面向对象三大特性之一
多态分为两类
静态多态和动态多态区别:
下面通过案例进行讲解多态
下面为动态绑定,静态绑定则将animal中virtual去掉,则dospeak一直为动物说话
#include
using namespace std;
class animal
{
public:
//Speak函数就是虚函数
//函数前面加上virtual关键字,变成虚函数,那么编译器在编译的时候就不能确定函数调用了
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
class cat : public animal
{
public:
void speak()
{
cout << "cat说话" << endl;
}
};
class dog : public animal
{
public:
void speak()
{
cout << "dog说话" << endl;
}
};
//我们希望传入什么对象,那么就调用什么对象的函数
//如果函数地址在编译阶段就能确定,那么静态联编
//如果函数地址在运行阶段才能确定,就是动态联编
void dospeak(animal & Animal)
{
Animal.speak();
}
//多态满足条件:
//1、有继承关系
//2、子类重写父类中的虚函数
//多态使用:
//父类指针或引用指向子类对象
int main()
{
cat ca;
dospeak(ca);
//ca.speak();
dog dog1;
dospeak(dog1);
//dog1.speak();
system("pause");
return 0;
}
运行结果:
上述代码为父类引用指向子类
下面为父类指针指向子类
animal* ani = new cat;
ani->speak();
delete ani;
多态满足条件
重写:函数返回值类型 函数名 参数列表 完全一致称为重写
案例描述:
分别利用普通写法和多态技术,设计实现两个操作数进行运算的计算器类
多态的优点:
代码组织结构清晰
可读性强
利于前期和后期的扩展以及维护
普通写法:
#include
using namespace std;
class calculator
{
public:
int result(string oper)
{
if (oper == "+")
{
return number1 + number2;
}
else if (oper == "-")
{
return number1 - number2;
}
else if (oper == "*")
{
return number1 * number2;
}
else
{
return 0;
}
}
int number1;
int number2;
};
void testcalculator()
{
calculator cal;
cal.number1 = 100;
cal.number2 = 200;
cout << cal.number1 << "+" << cal.number2 << "=" << cal.result("+") << endl;
cout << cal.number1 << "-" << cal.number2 << "=" << cal.result("-") << endl;
cout << cal.number1 << "*" << cal.number2 << "=" << cal.result("*") << endl;
}
int main()
{
testcalculator();
system("pause");
return 0;
}
#include
using namespace std;
class calculator
{
public:
virtual int result()
{
return 0;
}
int num1;
int num2;
};
class plus1 : public calculator
{
public:
int result()
{
return num1 + num2;
}
};
class sub : public calculator
{
public:
int result()
{
return num1 - num2;
}
};
class cal : public calculator
{
public:
int result()
{
return num1 * num2;
}
};
void testdt()
{
plus1 *plu = new plus1;
plu->num1 = 10;
plu->num2 = 20;
cout << plu->num1 << "+" << plu->num2 << "=" << plu->result() << endl;
delete plu;
sub *su = new sub;
su->num1 = 30;
su->num2 = 40;
cout << su->num1 << "-" << su->num2 << "=" << su->result() << endl;
delete su;
cal *ca = new cal;
ca->num1 = 50;
ca->num2 = 50;
cout << ca->num1 << "*" << ca->num2 << "=" << ca->result() << endl;
delete ca;
}
int main()
{
testdt();
system("pause");
return 0;
}
C++开发提倡利用多态设计程序架构,因为多态优点很多
开发程序原则:开闭
开放扩展,闭合修改
在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容
因此可以将虚函数改为纯虚函数
纯虚函数语法:virtual 返回值类型 函数名 (参数列表)= 0 ;
当类中有了纯虚函数,这个类也称为抽象类
抽象类特点:
#include
using namespace std;
class base
{
public:
virtual void func() = 0;
};
class son : public base
{
public:
virtual void func()
{
cout << "子函数func调用" << endl;
}
};
void test06091()
{
son * s = NULL;
//s = new base; base为抽象类,无法实例化对象
s = new son;
s->func();
delete s;
}
int main()
{
test06091();
system("pause");
return 0;
}
案例描述:
制作饮品的大致流程为:煮水 - 冲泡 - 倒入杯中 - 加入辅料
利用多态技术实现本案例,提供抽象制作饮品基类,提供子类制作咖啡和茶叶
#include
using namespace std;
class makedrink
{
public:
virtual void boil() = 0;
virtual void brew() = 0;
virtual void incup() = 0;
virtual void putsomething() = 0;
void dodringk()
{
boil();
brew();
incup();
putsomething();
}
};
class coffee : public makedrink
{
public:
virtual void boil()
{
cout << "烧农夫山泉" << endl;
}
virtual void brew()
{
cout << "冲泡咖啡" << endl;
}
virtual void incup()
{
cout << "倒入纸杯" << endl;
}
virtual void putsomething()
{
cout << "加糖!" << endl;
}
};
class tea : public makedrink
{
public:
virtual void boil()
{
cout << "烧矿泉水" << endl;
}
virtual void brew()
{
cout << "冲泡茶叶" << endl;
}
virtual void incup()
{
cout << "倒入玻璃杯" << endl;
}
virtual void putsomething()
{
cout << "加枸杞!" << endl;
}
};
//方式一
//void test06092()
//{
// coffee * cof = NULL;
// cof = new coffee;
// cof->boil();
// cof->brew();
// cof->incup();
// cof->putsomething();
// delete cof;
//
// tea * te = new tea;
// te->boil();
// te->brew();
// te->incup();
// te->putsomething();
// delete te;
//}
void dowork(makedrink * drink)
{
drink->dodringk();
delete drink;
}
void test06093()
{
dowork(new coffee);
cout << "---------------" << endl;
dowork(new tea);
}
int main()
{
//test06092();
test06093();
system("pause");
return 0;
}
多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
解决方式:将父类中的析构函数改为虚析构或者纯虚析构
虚析构和纯虚析构共性:
调用顺序:子类析构,父类析构函数。所以,纯虚析构函数要进行定义。
虚析构和纯虚析构区别:
虚析构语法:
virtual ~类名(){}
纯虚析构语法:
virtual ~类名() = 0;
类名::~类名(){}
#include
#include
using namespace std;
class animal
{
public :
animal()
{
cout << "animal构造函数调用" << endl;
}
virtual void speak() = 0;
virtual ~animal() = 0;
//virtual ~animal()
//{
// cout << "animal虚析构函数调用" << endl;
//}
};
animal::~animal()
{
cout << "animal纯析构函数" << endl;
}
//和包含普通纯虚函数的类一样,包含了纯虚析构函数的类也是一个抽象类。不能够被实例化。
class cat : public animal
{
public:
cat(string name)
{
cout << "cat构造函数调用" << endl;
cat_name = new string(name);
}
virtual void speak()
{
cout << *cat_name << "小猫在说话" << endl;
}
~cat()
{
cout << "cat析构函数" << endl;
if (this->cat_name != NULL)
{
delete cat_name;
cat_name = NULL;
}
}
string *cat_name;
};
void test06094()
{
animal * ani = new cat("tom");
ani->speak();
delete ani;
}
int main()
{
test06094();
system("pause");
return 0;
}
案例描述:
电脑主要组成部件为 CPU(用于计算),显卡(用于显示),内存条(用于存储)
将每个零件封装出抽象基类,并且提供不同的厂商生产不同的零件,例如Intel厂商和Lenovo厂商创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
测试时组装三台不同的电脑进行工作
#include
using namespace std;
class cpubase
{
public:
virtual void address() = 0;
};
class nvidiabase
{
public:
virtual void address() = 0;
};
class memorybase
{
public:
virtual void address() = 0;
};
class computerbase
{
public :
computerbase(cpubase * cpubas, nvidiabase * nvidiabas, memorybase * memorybas)
{
m_cpu = cpubas;
m_nvidia = nvidiabas;
m_memory = memorybas;
}
void work()
{
m_cpu->address();
m_nvidia->address();
m_memory->address();
}
~computerbase()
{
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if (m_nvidia != NULL)
{
delete m_nvidia;
m_nvidia = NULL;
}
if (m_memory != NULL)
{
delete m_memory;
m_memory = NULL;
}
}
cpubase * m_cpu;
nvidiabase * m_nvidia;
memorybase * m_memory;
};
class intelcpu : public cpubase
{
public:
virtual void address()
{
cout << "CPU: intelcpu" << endl;
}
};
class chinacpu : public cpubase
{
public:
virtual void address()
{
cout << "CPU: chinacpu" << endl;
}
};
class amdshow : public nvidiabase
{
public:
virtual void address()
{
cout << "Display:AMDshow" << endl;
}
};
class nvidiashow : public nvidiabase
{
public:
virtual void address()
{
cout << "Display:NVIDIAshow" << endl;
}
};
class caihong7 : public memorybase
{
public:
virtual void address()
{
cout << "Memory:caihong7" << endl;
}
};
class shumaxibu : public memorybase
{
public:
virtual void address()
{
cout << "Memory:shumaxibu" << endl;
}
};
void test060969()
{
//第一台电脑零件
cpubase * cpuba = new intelcpu;
nvidiabase * nvidiaba = new amdshow;
memorybase * memoryba = new caihong7;
//第一台电脑
cout << "computer1 start:" << endl;
computerbase * computer1 = new computerbase(cpuba, nvidiaba, memoryba);
computer1->work();
delete computer1;
//第二胎
cout << "computer2 start:" << endl;
computerbase * computer2 = new computerbase(new chinacpu, new nvidiashow, new shumaxibu);
computer2->work();
delete computer2;
//第二胎
cout << "computer3 start:" << endl;
computerbase * computer3 = new computerbase(new intelcpu, new nvidiashow, new caihong7);
computer3->work();
delete computer3;
//第二胎
cout << "computer4 start:" << endl;
computerbase * computer4 = new computerbase(new intelcpu, new amdshow, new caihong7);
computer4->work();
delete computer4;
}
int main()
{
test060969();
system("pause");
return 0;
}