fix vitual base classes issues: add virtual destruct functions.

master
FengJungle 2021-04-04 21:09:08 +08:00
parent 80c928a497
commit 5b21352028
29 changed files with 63 additions and 17 deletions

View File

@ -12,9 +12,10 @@ public:
AbstractSportProduct(){ AbstractSportProduct(){
} }
virtual ~AbstractSportProduct(){}
//抽象方法: //抽象方法:
void printName(){}; virtual void printName(){};
void play(){}; virtual void play(){};
}; };
//具体产品类Basketball //具体产品类Basketball
@ -24,6 +25,10 @@ public:
Basketball(){ Basketball(){
printName(); printName();
play(); play();
}
~Basketball()
{
} }
//具体实现方法 //具体实现方法
void printName(){ void printName(){
@ -41,6 +46,10 @@ public:
Football(){ Football(){
printName(); printName();
play(); play();
}
~Football()
{
} }
//具体实现方法 //具体实现方法
void printName(){ void printName(){
@ -58,6 +67,10 @@ public:
Volleyball(){ Volleyball(){
printName(); printName();
play(); play();
}
~Volleyball()
{
} }
//具体实现方法 //具体实现方法
void printName(){ void printName(){

View File

@ -12,9 +12,10 @@ public:
AbstractSportProduct(){ AbstractSportProduct(){
} }
virtual ~AbstractSportProduct(){}
//抽象方法: //抽象方法:
void printName(){}; virtual void printName(){};
void play(){}; virtual void play(){};
}; };
//具体产品类Basketball //具体产品类Basketball
@ -72,6 +73,7 @@ public:
class AbstractFactory class AbstractFactory
{ {
public: public:
virtual ~AbstractFactory(){}
virtual AbstractSportProduct *getSportProduct() = 0; virtual AbstractSportProduct *getSportProduct() = 0;
}; };

View File

@ -12,8 +12,9 @@ public:
AbstractBall(){ AbstractBall(){
} }
virtual ~AbstractBall(){}
//抽象方法: //抽象方法:
void play(){}; virtual void play(){};
}; };
//具体产品类Basketball //具体产品类Basketball
@ -47,8 +48,9 @@ class AbstractShirt
{ {
public: public:
AbstractShirt(){} AbstractShirt(){}
virtual ~AbstractShirt(){}
//抽象方法: //抽象方法:
void wearShirt(){}; virtual void wearShirt(){};
}; };
//具体产品类BasketballShirt //具体产品类BasketballShirt
@ -81,6 +83,7 @@ public:
class AbstractFactory class AbstractFactory
{ {
public: public:
virtual ~AbstractFactory() {}
virtual AbstractBall *getBall() = 0; virtual AbstractBall *getBall() = 0;
virtual AbstractShirt *getShirt() = 0; virtual AbstractShirt *getShirt() = 0;
}; };

View File

@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
using namespace std; using namespace std;
//产品类House //<EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD>House
class House class House
{ {
public: public:
@ -19,7 +19,7 @@ public:
void setRoof(string iRoof) { void setRoof(string iRoof) {
this->roof = iRoof; this->roof = iRoof;
} }
//打印House信息 //<EFBFBD><EFBFBD>ӡHouse<EFBFBD><EFBFBD>Ϣ
void printfHouseInfo() { void printfHouseInfo() {
printf("Floor:%s\t\n", this->floor.c_str()); printf("Floor:%s\t\n", this->floor.c_str());
printf("Wall:%s\t\n", this->wall.c_str()); printf("Wall:%s\t\n", this->wall.c_str());
@ -31,7 +31,7 @@ private:
string roof; string roof;
}; };
//抽象建造者AbstractBall //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AbstractBall
class AbstractBuilder class AbstractBuilder
{ {
public: public:
@ -46,7 +46,7 @@ public:
house = nullptr; house = nullptr;
} }
} }
//抽象方法: //<EFBFBD><EFBFBD><EFBFBD>󷽷<EFBFBD><EFBFBD><EFBFBD>
virtual void buildFloor() = 0; virtual void buildFloor() = 0;
virtual void buildWall() = 0; virtual void buildWall() = 0;
virtual void buildRoof() = 0; virtual void buildRoof() = 0;
@ -55,7 +55,7 @@ public:
House *house; House *house;
}; };
//具体建造者ConcreteBuilderA //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ConcreteBuilderA
class ConcreteBuilderA :public AbstractBuilder class ConcreteBuilderA :public AbstractBuilder
{ {
public: public:
@ -70,7 +70,7 @@ public:
house = nullptr; house = nullptr;
} }
} }
//具体实现方法 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
void buildFloor() { void buildFloor() {
this->house->setFloor("Floor_A"); this->house->setFloor("Floor_A");
} }
@ -85,7 +85,7 @@ public:
} }
}; };
//具体建造者ConcreteBuilderB //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ConcreteBuilderB
class ConcreteBuilderB :public AbstractBuilder class ConcreteBuilderB :public AbstractBuilder
{ {
public: public:
@ -100,7 +100,7 @@ public:
house = nullptr; house = nullptr;
} }
} }
//具体实现方法 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
void buildFloor() { void buildFloor() {
this->house->setFloor("Floor_B"); this->house->setFloor("Floor_B");
} }
@ -115,7 +115,7 @@ public:
} }
}; };
//指挥者Director //ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Director
class Director class Director
{ {
public: public:
@ -128,7 +128,7 @@ public:
builder = nullptr; builder = nullptr;
} }
} }
//具体实现方法 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
void setBuilder(AbstractBuilder *iBuilder) { void setBuilder(AbstractBuilder *iBuilder) {
this->builder = iBuilder; this->builder = iBuilder;
} }

View File

@ -20,6 +20,7 @@ class PrototypeWork
{ {
public: public:
PrototypeWork(){} PrototypeWork(){}
virtual ~PrototypeWork(){}
virtual PrototypeWork *clone() = 0; virtual PrototypeWork *clone() = 0;
private: private:

View File

@ -11,6 +11,7 @@ class Controller
{ {
public: public:
Controller(){} Controller(){}
virtual ~Controller(){}
virtual void pathPlanning() = 0; virtual void pathPlanning() = 0;
private: private:
}; };

View File

@ -11,6 +11,7 @@ class Game
{ {
public: public:
Game(){} Game(){}
virtual ~Game(){}
virtual void play() = 0; virtual void play() = 0;
private: private:
}; };
@ -41,6 +42,7 @@ class Phone
public: public:
Phone(){ Phone(){
} }
virtual ~Phone(){}
//°²×°ÓÎÏ· //°²×°ÓÎÏ·
virtual void setupGame(Game *igame) = 0; virtual void setupGame(Game *igame) = 0;
virtual void play() = 0; virtual void play() = 0;

View File

@ -14,6 +14,7 @@ public:
Component(string iName){ Component(string iName){
this->name = iName; this->name = iName;
} }
virtual ~Component(){}
//增加一个部门或办公室 //增加一个部门或办公室
virtual void add(Component*) = 0; virtual void add(Component*) = 0;
//撤销一个部门或办公室 //撤销一个部门或办公室

View File

@ -6,6 +6,7 @@ class Component
{ {
public: public:
Component(){} Component(){}
virtual ~Component(){}
virtual void operation() = 0; virtual void operation() = 0;
}; };

View File

@ -10,6 +10,7 @@ class NetDevice
{ {
public: public:
NetDevice(){} NetDevice(){}
virtual ~NetDevice(){}
virtual string getName() = 0; virtual string getName() = 0;
/*void print(){ /*void print(){

View File

@ -10,6 +10,7 @@ class Subject
{ {
public: public:
Subject(){} Subject(){}
virtual ~Subject(){}
virtual void method() = 0; virtual void method() = 0;
}; };
@ -18,6 +19,7 @@ class RealSubject :public Subject
{ {
public: public:
RealSubject(){} RealSubject(){}
virtual ~RealSubject(){}
void method(){ void method(){
printf("调用业务方法\n"); printf("调用业务方法\n");
} }

View File

@ -36,6 +36,7 @@ public:
Approver(string iName){ Approver(string iName){
setName(iName); setName(iName);
} }
virtual ~Approver(){}
// 添加上级 // 添加上级
void setSuperior(Approver *iSuperior){ void setSuperior(Approver *iSuperior){
this->superior = iSuperior; this->superior = iSuperior;

View File

@ -13,6 +13,7 @@ class Command
{ {
public: public:
Command(){} Command(){}
virtual ~Command(){}
// 声明抽象接口:发送命令 // 声明抽象接口:发送命令
virtual void execute() = 0; virtual void execute() = 0;
private: private:

View File

@ -9,6 +9,7 @@ class AbstractNode
{ {
public: public:
AbstractNode(){} AbstractNode(){}
virtual ~AbstractNode(){}
// ÉùÃ÷³éÏó½Ó¿Ú // ÉùÃ÷³éÏó½Ó¿Ú
virtual char interpret() = 0; virtual char interpret() = 0;
}; };

View File

@ -17,5 +17,5 @@ int Television::getTotalChannelNum(){
} }
void Television::play(int i){ void Television::play(int i){
printf("现在播放:%s……\n", channelList[i].c_str()); printf("<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD>ţ<EFBFBD>%s<><73><EFBFBD><EFBFBD>\n", channelList[i].c_str());
} }

View File

@ -13,6 +13,7 @@ class Aggregate
{ {
public: public:
Aggregate(){} Aggregate(){}
virtual ~Aggregate(){}
virtual Iterator* createIterator() = 0; virtual Iterator* createIterator() = 0;
}; };

View File

@ -13,6 +13,7 @@ class Colleague
{ {
public: public:
Colleague(){} Colleague(){}
virtual ~Colleague(){}
void setMediator(Mediator* iMediator){ void setMediator(Mediator* iMediator){
this->mediator = iMediator; this->mediator = iMediator;
} }

View File

@ -9,6 +9,7 @@ class Mediator
{ {
public: public:
Mediator(){} Mediator(){}
virtual ~Mediator(){}
// 声明抽象方法 // 声明抽象方法
virtual void operation(Colleague*) = 0; virtual void operation(Colleague*) = 0;
// 声明注册方法 // 声明注册方法

View File

@ -13,6 +13,7 @@ class AllyCenter
{ {
public: public:
AllyCenter(); AllyCenter();
virtual ~AllyCenter(){}
// 声明通知方法 // 声明通知方法
virtual void notify(INFO_TYPE infoType, std::string name) = 0; virtual void notify(INFO_TYPE infoType, std::string name) = 0;
// 加入玩家 // 加入玩家

View File

@ -8,6 +8,7 @@ using namespace std;
class Observer class Observer
{ {
public: public:
virtual ~Observer() {}
// 声明响应更新方法 // 声明响应更新方法
virtual void update() = 0; virtual void update() = 0;
}; };
@ -26,6 +27,7 @@ public:
class Subject class Subject
{ {
public: public:
virtual ~Subject() {}
// 添加观察者 // 添加观察者
void attach(Observer* obs){ void attach(Observer* obs){
obsList.push_back(obs); obsList.push_back(obs);

View File

@ -10,6 +10,7 @@ using namespace std;
class Observer class Observer
{ {
public: public:
virtual ~Observer(){}
Observer(){} Observer(){}
// 声明抽象方法 // 声明抽象方法
virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0; virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;

View File

@ -5,6 +5,7 @@
class State class State
{ {
public: public:
virtual ~State(){}
// 声明抽象方法 // 声明抽象方法
virtual void handle() = 0; virtual void handle() = 0;
}; };

View File

@ -7,6 +7,7 @@ class Level
{ {
public : public :
Level(); Level();
virtual ~Level(){}
// 声明方法 // 声明方法
void playCard(); void playCard();
void play(); void play();

View File

@ -8,6 +8,7 @@ class Strategy
{ {
public: public:
Strategy(){} Strategy(){}
virtual ~Strategy(){}
virtual void sort(int arr[], int N) = 0; virtual void sort(int arr[], int N) = 0;
}; };

View File

@ -5,6 +5,7 @@
class AbstractClass class AbstractClass
{ {
public: public:
virtual ~AbstractClass(){}
// 模板方法,定义一个算法的框架流程 // 模板方法,定义一个算法的框架流程
void templateMethod(){ void templateMethod(){
// do something // do something

View File

@ -8,6 +8,7 @@ class FingerprintModule
{ {
public: public:
FingerprintModule(){} FingerprintModule(){}
virtual ~FingerprintModule(){}
void getImage(){ void getImage(){
printf("²ÉÖ¸ÎÆͼÏñ\n"); printf("²ÉÖ¸ÎÆͼÏñ\n");
} }

View File

@ -5,6 +5,7 @@
class Visitor class Visitor
{ {
public: public:
virtual ~Visitor() {}
virtual void visit(ConcreteElementA*) = 0; virtual void visit(ConcreteElementA*) = 0;
virtual void visit(ConcreteElementB*) = 0; virtual void visit(ConcreteElementB*) = 0;
}; };
@ -26,6 +27,7 @@ public:
class Element class Element
{ {
public: public:
virtual ~Element() {}
// 声明抽象方法,以一个抽象访问者的指针作为函数参数 // 声明抽象方法,以一个抽象访问者的指针作为函数参数
virtual void accept(Visitor*) = 0; virtual void accept(Visitor*) = 0;
}; };

View File

@ -10,6 +10,7 @@ class Element
{ {
public: public:
Element(){}; Element(){};
virtual ~Element(){}
virtual void accept(Visitor*) = 0; virtual void accept(Visitor*) = 0;
void setPrice(int iPrice){ void setPrice(int iPrice){
this->price = iPrice; this->price = iPrice;

View File

@ -14,6 +14,7 @@ class Visitor
{ {
public: public:
Visitor(){}; Visitor(){};
virtual ~Visitor(){}
// 声明一组访问方法 // 声明一组访问方法
virtual void visit(Apple*) = 0; virtual void visit(Apple*) = 0;
virtual void visit(Book*) = 0; virtual void visit(Book*) = 0;