一、案例1:计算器类
1.1 普通方式实现
cpp
#include <iostream>
#include <string>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
int result(string oper)
{
if (oper == "+")
return num1 + num2;
else if (oper == "-")
return num1 - num2;
else if (oper == "*")
return num1 * num2;
}
};
void test1()
{
Calculator c;
c.num1 = 10;
c.num2 = 10;
//加法
cout << c.num1 << "+" << c.num2 << "=" << c.result("+") << endl;
//减法
cout << c.num1 << "-" << c.num2 << "=" << c.result("-") << endl;
//乘法
cout << c.num1 << "*" << c.num2 << "=" << c.result("*") << endl;
}
1.2 多态实现
1.多态实现相对与普通方法代码量更大;
2.但若要增加程序功能(如除法)或出现异常时,普通实现方法需要对result()进行修改;
3.而多态实现方法则将加减分成不同的类,增加新功能不会影响到加减函数,并且某一功能 出现异常时可快速定位,增强代码维护性;
cpp
//多态实现
class Abstract_Calculator
{
public:
int num1;
int num2;
virtual int result()
{
return 0;
}
};
class Add_Calculator : public Abstract_Calculator
{
public:
int result()
{
return num1 + num2;
}
};
class Sub_Calculator : public Abstract_Calculator
{
public:
int result()
{
return num1 - num2;
}
};
void test2()
{
cout << "***************多态实现*****************" << endl;
Abstract_Calculator* abc = new Add_Calculator;
abc->num1 = 10;
abc->num2 = 10;
cout << abc->num1 << "+" << abc->num2 << "=" << abc->result() << endl;
delete abc;
abc = new Sub_Calculator;
abc->num1 = 10;
abc->num2 = 10;
cout << abc->num1 << "-" << abc->num2 << "=" << abc->result() << endl;
delete abc;
}
二、案例2:制作饮品
cpp
#include <iostream>
#include <string>
using namespace std;
class Abstract_Drinking
{
public:
//1.煮水
virtual void zhushui() = 0;
//2.冲泡
virtual void chongpao() = 0;
//3.倒入杯中
virtual void daoru() = 0;
//4.加...
virtual void dao() = 0;
//5.总体制作流程
void made_progress()
{
zhushui();
chongpao();
daoru();
dao();
}
};
class Coffee : public Abstract_Drinking
{
public:
void zhushui()
{
cout << "煮水" << endl;
}
void chongpao()
{
cout << "冲泡咖啡" << endl;
}
void daoru()
{
cout << "倒入杯中" << endl;
}
void dao()
{
cout << "加糖和牛奶" << endl;
}
};
class Tea : public Abstract_Drinking
{
public:
void zhushui()
{
cout << "煮水" << endl;
}
void chongpao()
{
cout << "冲泡茶叶" << endl;
}
void daoru()
{
cout << "倒入杯中" << endl;
}
void dao()
{
cout << "加柠檬" << endl;
}
};
void doWork(Abstract_Drinking* abc)
{
abc->made_progress();
delete abc;
}
void test1()
{
doWork(new Coffee);
cout << "---------------------" << endl;
doWork(new Tea);
}
int main()
{
test1();
return 0;
}
三、案例3:电脑组装
cpp
#include <iostream>
#include <string>
using namespace std;
//CPU抽象类
class CPU
{
public:
virtual void calculate() = 0;
};
//显卡抽象类
class VideoCard
{
public:
virtual void display() = 0;
};
//内存条抽象类
class Memory
{
public:
virtual void storage() = 0;
};
//电脑类
class Computer
{
public:
Computer(CPU* cpu, VideoCard* vc, Memory* mem)
{
_cpu = cpu;
_vc = vc;
_mem = mem;
}
void DoWork()
{
_cpu->calculate();
_vc->display();
_mem->storage();
}
~Computer()
{
if (_cpu != NULL)
{
delete _cpu;
_cpu = NULL;
}
if (_vc != NULL)
{
delete _vc;
_vc = NULL;
}
if (_mem != NULL)
{
delete _mem;
_mem = NULL;
}
}
private:
CPU* _cpu;
VideoCard* _vc;
Memory* _mem;
};
//Intel厂商
class IntelCPU: public CPU
{
public:
void calculate()
{
cout << "IntelCPU开始计算了" << endl;
}
};
class IntelVideoCard : public VideoCard
{
public:
void display()
{
cout << "IntelVideoCard开始显示了" << endl;
}
};
class IntelMemory : public Memory
{
public:
void storage()
{
cout << "IntelMemory开始存储了" << endl;
}
};
//Lenovo厂商
class LenovoCPU : public CPU
{
public:
void calculate()
{
cout << "LenovoCPU开始计算了" << endl;
}
};
class LenovoVideoCard : public VideoCard
{
public:
void display()
{
cout << "LenovoVideoCard开始显示了" << endl;
}
};
class LenovoMemory : public Memory
{
public:
void storage()
{
cout << "LenovoMemory开始存储了" << endl;
}
};
//测试
void test1()
{
cout << "创建第一台电脑--------------------" << endl;
CPU* intel_CPU = new IntelCPU;
VideoCard* intel_VideoCard = new IntelVideoCard;
Memory* intel_Memory = new IntelMemory;
Computer* computer1 = new Computer(intel_CPU, intel_VideoCard, intel_Memory);
computer1->DoWork();
delete computer1;
cout << "创建第二台电脑--------------------" << endl;
Computer* computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);
computer2->DoWork();
delete computer2;
}
int main()
{
test1();
return 0;
}