@913094331
2019-08-02T03:46:11.000000Z
字数 2103
阅读 738
C++ DesignPattern OOP
Source Code
#include <iostream>#include <string>using namespace std;class Shape{public:virtual void Draw() {};};class Circle : public Shape{void Draw(){ cout<<"Draw a circle"<<endl; }};class Square : public Shape{void Draw(){ cout<<"Draw a square"<<endl; }};class Rectangle : public Shape{void Draw(){ cout<<"Draw a rectangle"<<endl; }};class Triangle : public Shape{void Draw(){ cout<<"Draw a triangle"<<endl; }};class Casual : public Shape{void Draw(){ cout<<"Draw casually"<<endl; }};class ShapeFactory{public:static Shape* Relshape(string type) {if(!type.compare("CIRCLE")) {return new Circle();} else if(!type.compare("SQUARE")) {return new Square();} else if(!type.compare("RECTANGLE")) {return new Rectangle();} else if(!type.compare("TRIANGLE")) {return new Triangle();} else {return new Casual();}}};class FactoryPattern{public:FactoryPattern(){cout<<"************MENU*************"<<endl;cout<<"*** 1.CirCle ***"<<endl;cout<<"*** 2.Square ***"<<endl;cout<<"*** 3.Rectangle ***"<<endl;cout<<"*** 4.Triangle ***"<<endl;cout<<"*** 5.Casually ***"<<endl;cout<<"*****************************"<<endl;cout<<"FactoryPattern------What do you want to draw?"<<endl;cout<<"Answer----- ";cin>>tag;switch(tag){case 1:shape= ShapeFactory::Relshape("CIRCLE"); break;case 2:shape= ShapeFactory::Relshape("SQUARE"); break;case 3:shape= ShapeFactory::Relshape("RECTANGLE"); break;case 4:shape= ShapeFactory::Relshape("TRIANGLE"); break;default:shape= ShapeFactory::Relshape(""); break;}shape->Draw();}~FactoryPattern(){}private:int tag;Shape* shape;};int main(){FactoryPattern factory;return 0;}
Debug
- error : expected '}' at end of input
reason : lose matching '}'
solution : check all {} one by one- error : could not convert '……' from 'Circle*' to 'Shape'
reason : type matching failed
solution : check the return-value and return-type of the function and assignment variables' type- error : "……" is private within this context
reason : lose the tag "public:" in class- error : undefined reference to `vtable for Shape'
reason : undefined reference stands failure in process of link, 'vtable' is created by virtual interface class, this error stands the definition has not been implemented
solution : "virtual void Draw();" -> "virtual void Draw() {};"
tips:
new class should be defined with () , e.g. new Cirle()