偷偷拿来记录一下萌新的cs路——day 40 计算器类、制作饮品、电脑组装。
目录
计算器类
用普通类写法实现计算器
用多态实现计算器类
纯虚函数和抽象类实现制作饮品
组装电脑
组装电脑的框架
实现一台电脑
实现多台电脑
计算器类
用普通类写法实现计算器
#include
#include
using namespace std;
class Calculator {
public:
int 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; // 操作数1
c.m_Num2 = 20; // 操作数2
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;
}
int main() {
test01();
return 0;
}
#include#include using namespace std; class Calculator { public: int 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; // 操作数1 c.m_Num2 = 20; // 操作数2 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; } int main() { test01(); return 0; }
运行结果:
可见若要增加功能,需要修改Calculator 中的源码,不符合开发中的开闭原则。
多态的优点:代码组织结构清晰,可读性强,利于后期拓展及维护。
用多态实现计算器类
#include
#include
using namespace std;
// 计算器抽象类
class AbstractCalculator {
public:
virtual int getResult() {
return 0;
}
int m_Num1 = 0, m_Num2 = 0;
};
// 加法计算器类
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 = new AddCalculator; // 父类指针指向子类对象
abc->m_Num1 = 10;
abc->m_Num2 = 20;
cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl;
delete abc; // 释放堆区数据
abc = new SubCalculator;
abc->m_Num1 = 10;
abc->m_Num2 = 20;
cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl;
delete abc;
abc = new MulCalculator;
abc->m_Num1 = 10;
abc->m_Num2 = 20;
cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl;
delete abc;
}
int main() {
test02();
return 0;
}
堆区数据:由程序员控制,程序运行结束由编译器回收。
运行结果
纯虚函数和抽象类实现制作饮品
#include
using namespace std;
class AbstractDrinking {
public:
virtual void Boil() = 0;
virtual void Brew() = 0; // 冲泡
virtual void PourInCup() = 0;
virtual void PutSth() = 0;
void makeDrink() {
Boil();
Brew();
PourInCup();
PutSth();
}
};
class Coffee :public AbstractDrinking {
public:
void Boil() {
cout << "Boil the water. " << endl;
}
void Brew() {
cout << "Brew the coffee. " << endl;
}
void PourInCup() {
cout << "Pour coffee into cups. " << endl;
}
void PutSth() {
cout << "Pour in milk. " << endl;
}
};
class Tea :public AbstractDrinking {
virtual void Boil() {
cout << "Boil the water. " << endl;
}
virtual void Brew() {
cout << "Brew the tea leaves. " << endl;
}
virtual void PourInCup() {
cout << "Pour tea into cups. " << endl;
}
virtual void PutSth() {
cout << "Pour in wolfberries and lemon. " << endl;
}
};
// 制作过程
void doWork(AbstractDrinking *abs) {
abs->makeDrink();
delete abs; // 释放堆区数据,防止泄露
}
void test01() {
doWork(new Coffee);
cout << endl; // 分割
doWork(new Tea);
}
int main() {
test01();
return 0;
}
运行结果
组装电脑
组装电脑的框架
最终目的:实现三台不同cpu、显卡、内存条组合的电脑。
#includeusing namespace std; class CPU { virtual void calculate() = 0; }; // 显卡 class GraphicsCard { virtual void display() = 0; }; // 内存条 class RAM { virtual void storage() = 0; }; class Computer { Computer() { } }; class IntelCPU : public CPU { void calculate() { cout << "CPU of Intel. " << endl; } }; class AMDCPU : public CPU { void calculate() { cout << "CPU of AMD. " << endl; } }; class IntelGraphicsCard : public GraphicsCard { void display() { cout << "Graphics card of Intel. " << endl; } }; class SamsungGraphicsCard : public GraphicsCard { void display() { cout << "Graphics card of Samsung. " << endl; } }; class KinstonRAM : public RAM { void storage() { cout << "RAM of Kingston. " << endl; } }; class SamsungRAM : public RAM { void storage() { cout << "RAM of Samsung. " << endl; } }; void test01() { } int main() { test01(); return 0; }
实现一台电脑
#include
using namespace std;
class CPU {
public:
virtual void calculate() = 0;
};
// 显卡
class GraphicsCard {
public:
virtual void display() = 0;
};
// 内存条
class RAM {
public:
virtual void storage() = 0;
};
class Computer {
public:
// 接收指针
Computer(CPU* cpu, GraphicsCard* gc, RAM* ram) {
m_cpu = cpu;
m_gc = gc;
m_ram = ram;
}
// 工作函数
void work() {
m_cpu->calculate();
m_gc->display();
m_ram->storage();
}
private:
CPU* m_cpu;
GraphicsCard* m_gc;
RAM* m_ram;
};
class IntelCPU : public CPU {
public:
void calculate() {
cout << "CPU of Intel. " << endl;
}
};
class AMDCPU : public CPU {
public:
void calculate() {
cout << "CPU of AMD. " << endl;
}
};
class IntelGraphicsCard : public GraphicsCard {
public:
void display() {
cout << "Graphics card of Intel. " << endl;
}
};
class SamsungGraphicsCard : public GraphicsCard {
public:
void display() {
cout << "Graphics card of Samsung. " << endl;
}
};
class KingstonRAM : public RAM {
public:
void storage() {
cout << "RAM of Kingston. " << endl;
}
};
class SamsungRAM : public RAM {
public:
void storage() {
cout << "RAM of Samsung. " << endl;
}
};
void test01() {
// 第一台电脑部件
CPU* intelCPU = new IntelCPU;
GraphicsCard* intelGc = new IntelGraphicsCard;
RAM* kingstonRAM = new KingstonRAM;
// 创建第一台电脑
Computer* computer1 = new Computer(intelCPU, intelGc, kingstonRAM);
computer1->work();
delete computer1;
}
int main() {
test01();
return 0;
}
运行结果:
实现多台电脑
由于部件的指针未被释放,创建第二台电脑需要先释放部件指针,可以直接在delete computer1 后释放也可以在Computer 类中定义析构函数。
class Computer { public: Computer(CPU* cpu, GraphicsCard* gc, RAM* ram) { m_cpu = cpu; m_gc = gc; m_ram = ram; } void work() { m_cpu->calculate(); m_gc->display(); m_ram->storage(); } // 析构函数释放电脑部件 ~Computer() { if (m_cpu != NULL) { delete m_cpu; m_cpu = NULL; } if (m_gc != NULL) { delete m_gc; m_gc = NULL; } if (m_ram != NULL) { delete m_ram; m_ram = NULL; } } private: CPU* m_cpu; GraphicsCard* m_gc; RAM* m_ram; };
完整代码:
#includeusing namespace std; class CPU { public: virtual void calculate() = 0; }; // 显卡 class GraphicsCard { public: virtual void display() = 0; }; // 内存条 class RAM { public: virtual void storage() = 0; }; class Computer { public: // 接收指针 Computer(CPU* cpu, GraphicsCard* gc, RAM* ram) { m_cpu = cpu; m_gc = gc; m_ram = ram; } // 工作函数 void work() { m_cpu->calculate(); m_gc->display(); m_ram->storage(); } // 析构函数释放电脑部件 ~Computer() { if (m_cpu != NULL) { delete m_cpu; m_cpu = NULL; } if (m_gc != NULL) { delete m_gc; m_gc = NULL; } if (m_ram != NULL) { delete m_ram; m_ram = NULL; } } private: CPU* m_cpu; GraphicsCard* m_gc; RAM* m_ram; }; class IntelCPU : public CPU { public: void calculate() { cout << "CPU of Intel. " << endl; } }; class AMDCPU : public CPU { public: void calculate() { cout << "CPU of AMD. " << endl; } }; class IntelGraphicsCard : public GraphicsCard { public: void display() { cout << "Graphics card of Intel. " << endl; } }; class SamsungGraphicsCard : public GraphicsCard { public: void display() { cout << "Graphics card of Samsung. " << endl; } }; class KingstonRAM : public RAM { public: void storage() { cout << "RAM of Kingston. " << endl; } }; class SamsungRAM : public RAM { public: void storage() { cout << "RAM of Samsung. " << endl; } }; void test01() { // 第一台电脑部件 CPU* intelCPU = new IntelCPU; GraphicsCard* intelGc = new IntelGraphicsCard; RAM* kingstonRAM = new KingstonRAM; // 创建第一台电脑 Computer* computer1 = new Computer(intelCPU, intelGc, kingstonRAM); computer1->work(); delete computer1; cout << endl; Computer* computer2 = new Computer(new IntelCPU, new SamsungGraphicsCard, new KingstonRAM); computer2->work(); delete computer2; cout << endl; Computer* computer3 = new Computer(new AMDCPU, new IntelGraphicsCard, new SamsungRAM); computer3->work(); delete computer3; } int main() { test01(); return 0; }
运行结果:
学了更多知识还会回来更新的!有误之处请大佬指正,非常感谢!