本文章部分内容源于《黑马程序员C++ 核心篇》
1. 前言
突发奇想来回顾一下多态,顺便把继承和智能指针一起练习一下。多态的出现就是为了在封装和继承类以后,使调用类的时候更宽泛,把类里面的东西再向下一个类里面细分,最后再调用大类的时候,就可以将子类实现的功能一起搞出来。
众所周知多态的实现条件有两个:
- 有继承关系
- 子类重写父类中的虚函数
注意在子类重写父类成员函数(虚函数/纯虚函数)时,virtual可以省略,写了也没啥问题,我下面的代码里都没写。
多态使用时:父类指针或引用指向子类对象。因此这里涉及到new,就需要用delete,由于防止出问题,我会把其中两个例子直接使用智能指针解决shared_ptr。每一个例子都很好,建议挨个刷。
2. TEST1
动物说话,其实父类虚函数没有任何作用,因为最后都要在子类重写,所以后面被纯虚函数替代了。
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
class Dog : public Animal
{
public:
void speak()
{
cout << "狗狗在说话" << endl;
}
};
class Cat : public Animal
{
public:
void speak()
{
cout << "猫咪在说话" << endl;
}
};
void DoSpeak(Animal& animal)
{
animal.speak();
}
int main()
{
Dog dog;
DoSpeak(dog);
}
3. TEST2
注意:相同类型的指针重复使用的时候,可以省略掉类型。 传统指针实现计算器
#include <iostream>
using namespace std;
class AbstractCalculate
{
public:
virtual int getResult()
{
return 0;
}
int m_Num1;
int m_Num2;
};
class Add : public AbstractCalculate
{
public:
int getResult()
{
return m_Num1+m_Num2;
}
};
class Cut : public AbstractCalculate
{
public:
int getResult()
{
return m_Num1-m_Num2;
}
};
class Mu : public AbstractCalculate
{
public:
int getResult()
{
return m_Num1*m_Num2;
}
};
void test()
{
AbstractCalculate * t = new Add;
t->m_Num1 = 10;
t->m_Num2 = 10;
cout << t->getResult() << endl;
delete t;
t = new Cut;
t->m_Num1 = 10;
t->m_Num2 = 10;
cout << t->getResult() << endl;
delete t;
t = new Mu;
t->m_Num1 = 10;
t->m_Num2 = 10;
cout << t->getResult() << endl;
delete t;
}
int main()
{
test();
}
4. TEST3
注意:声明智能指针,可以用boost::shared_ptr或者std::shared_ptr,都可以 传统指针or智能指针 实现计算器
#include <iostream>
using namespace std;
#include <boost/smart_ptr.hpp>
class Calculate
{
public:
virtual int getResult()
{
return 0;
}
int m_Num1;
int m_Num2;
};
class Add : public Calculate
{
public:
int getResult()
{
return m_Num1+m_Num2;
}
};
void test()
{
shared_ptr <Calculate> t (new Add);
t->m_Num1 =10;
t->m_Num2 = 10;
cout << t->getResult() << endl;
cout <<"----" <<endl;
}
int main()
{
test();
}
5. TEST4
建议还是要先捋一下逻辑再开始写代码,代码写到这需要对应逻辑了。还有就是到目前为止,咱们都没有涉及到构造函数的调用,从下个例子开始进入构造函多态的篇章,一定要好好看。
#include <iostream>
using namespace std;
class Abstract
{
public:
virtual void shaoshui() = 0;
virtual void chongpao() = 0;
virtual void daoru() = 0;
virtual void fuliao() = 0;
void guiding()
{
shaoshui();
chongpao();
daoru();
fuliao();
}
};
class orige : public Abstract
{
public:
void shaoshui()
{
cout << "烧水" << endl;
}
void chongpao()
{
cout << "冲泡果汁" << endl;
}
void daoru()
{
cout << "果汁倒入杯里" << endl;
}
void fuliao()
{
cout << "加入果汁粉" << endl;
}
};
class coffee : public Abstract
{
public:
void shaoshui()
{
cout << "烧水" << endl;
}
void chongpao()
{
cout << "冲泡咖啡" << endl;
}
void daoru()
{
cout << "咖啡倒入杯里" << endl;
}
void fuliao()
{
cout << "加入咖啡粉" << endl;
}
};
void test()
{
Abstract* t = new coffee;
t->guiding();
delete t;
t = new orige;
t->guiding();
delete t;
}
int main()
{
test();
}
6. TEST5
class Animal {
public:
Animal()
{
cout << "Animal 构造函数调用!" << endl;
}
virtual void Speak() = 0;
virtual ~Animal() = 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()
{
cout << "Cat析构函数调用!" << endl;
if (this->m_Name != NULL) {
delete m_Name;
m_Name = NULL;
}
}
public:
string *m_Name;
};
void test01()
{
Animal *animal = new Cat("Tom");
animal->Speak();
delete animal;
}
int main() {
test01();
system("pause");
return 0;
}
7. TEST6(终极)
建议:先看一遍把逻辑画个图,不然写着写着晕了
#include <iostream>
using namespace std;
#include <boost/smart_ptr.hpp>
class CPU
{
public:
virtual void cpu()=0;
};
class Show
{
public:
virtual void show()=0;
};
class Memory
{
public:
virtual void memory()=0;
};
class Computer
{
public:
Computer(CPU *_cpu, Show* _show, Memory* _memory):m_cpu(_cpu), m_show(_show), m_memory(_memory)
{
}
void work()
{
m_cpu->cpu();
m_show->show();
m_memory->memory();
}
~Computer()
{
if(m_cpu !=NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if(m_show !=NULL)
{
delete m_show
m_show = NULL;
}
if(m_memory !=NULL)
{
delete m_memory;
m_memory = NULL;
}
}
private:
CPU * m_cpu;
Show * m_show;
Memory* m_memory;
};
class IntelCpu : public CPU
{
public:
void cpu()
{
cout << "Intel牌Cpu开始运行" << endl;
}
};
class IntelShow: public Show
{
public:
void show()
{
cout << "Intel牌Show开始运行" << endl;
}
};
class IntelMemory: public Memory
{
public:
void memory()
{
cout << "Intel牌Memory开始运行" << endl;
}
};
class LenvoCpu : public CPU
{
public:
void cpu()
{
cout << "Lenvo牌Cpu开始运行" << endl;
}
};
class LenvoShow: public Show
{
public:
void show()
{
cout << "Lenvo牌Show开始运行" << endl;
}
};
class LenvoMemory: public Memory
{
public:
void memory()
{
cout << "Lenvo牌Memory开始运行" << endl;
}
};
void test()
{
CPU * cpu1 = new IntelCpu;
Show * show1 = new LenvoShow;
Memory * mem1 = new IntelMemory;
cout << "第一个电脑:" << endl;
boost::shared_ptr<Computer> test ( new Computer(cpu1, show1, mem1));
test->work();
}
int main()
{
test();
cout << "--3-" << endl;
}
|