Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/CCircle.cpp -o obj/Debug/CCircle.o
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/CFactory.cpp -o obj/Debug/CFactory.o
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/CHexagon.cpp -o obj/Debug/CHexagon.o
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/CPentagon.cpp -o obj/Debug/CPentagon.o
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/CRectangle.cpp -o obj/Debug/CRectangle.o
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/CSquare.cpp -o obj/Debug/CSquare.o
- g++ -Wall -fexceptions -g -std=c++11 -c /mnt/home/Data_MaOt/Short_C_and_Cpp_progs/DesignPatterns/03_Factory_Pattern/main.cpp -o obj/Debug/main.o
- g++ -o bin/Debug/03_Factory_Pattern obj/Debug/CCircle.o obj/Debug/CFactory.o obj/Debug/CHexagon.o obj/Debug/CPentagon.o obj/Debug/CRectangle.o obj/Debug/CSquare.o obj/Debug/main.o
- #include <iostream>
- #include "CCircle.h"
- CCircle::CCircle()
- {
- std::cout << "CCircle::CCircle(): Started and finished." << std::endl;
- }
- void CCircle::Identify()
- {
- std::cout << "CCircle::CCircle(): I am a CCircle object." << std::endl;
- }
- #ifndef CCIRCLE_H
- #define CCIRCLE_H
- #include "CShapeBase.h"
- class CCircle : public CShapeBase
- {
- public:
- CCircle();
- void Identify();
- };
- #endif // CCIRCLE_H
- #include <iostream>
- #include <memory>
- #include "CFactory.h"
- #include "CCircle.h"
- #include "CSquare.h"
- #include "CRectangle.h"
- #include "CPentagon.h"
- #include "CHexagon.h"
- std::shared_ptr<CShapeBase> CFactory::Produce()
- {
- std::shared_ptr<CShapeBase> myBasePtr = nullptr;
- while(myBasePtr == nullptr)
- {
- std::cout << "Please select your shape:" << std::endl;
- std::cout << "1) Circle" << std::endl;
- std::cout << "2) Square" << std::endl;
- std::cout << "3) Rectangle" << std::endl;
- std::cout << "4) Pentagon" << std::endl;
- std::cout << "5) Hexagon" << std::endl;
- int input = 0;
- std::cin >> input;
- switch(input)
- {
- case 1:
- myBasePtr = std::make_shared<CCircle>();
- break;
- case 2:
- myBasePtr = std::make_shared<CSquare>();
- break;
- case 3:
- myBasePtr = std::make_shared<CRectangle>();
- break;
- case 4:
- myBasePtr = std::make_shared<CPentagon>();
- break;
- case 5:
- myBasePtr = std::make_shared<CHexagon>();
- break;
- default:
- std::cout << "CFactory::Produce(): Did not produce a shape." << std::endl;
- break;
- }
- }
- return myBasePtr;
- }
- #ifndef CFACTORY_H
- #define CFACTORY_H
- #include <memory>
- #include "CShapeBase.h"
- // Include derives classes here?
- class CFactory
- {
- public:
- std::shared_ptr<CShapeBase> Produce();
- };
- #endif // CFACTORY_H
- #include <iostream>
- #include "CHexagon.h"
- CHexagon::CHexagon()
- {
- std::cout << "CHexagon::CHexagon(): Started and finished." << std::endl;
- }
- void CHexagon::Identify()
- {
- std::cout << "CHexagon::CHexagon(): I am a CHexagon object." << std::endl;
- }
- #ifndef CHEXAGON_H
- #define CHEXAGON_H
- #include "CShapeBase.h"
- class CHexagon : public CShapeBase
- {
- public:
- CHexagon();
- void Identify();
- };
- #endif // CHEXAGON_H
- #include <iostream>
- #include "CPentagon.h"
- CPentagon::CPentagon()
- {
- std::cout << "CPentagon::CPentagon(): Started and finished." << std::endl;
- }
- void CPentagon::Identify()
- {
- std::cout << "CPentagon::CPentagon(): I am a CPentagon object." << std::endl;
- }
- #ifndef CPENTAGON_H
- #define CPENTAGON_H
- #include "CShapeBase.h"
- class CPentagon : public CShapeBase
- {
- public:
- CPentagon();
- void Identify();
- };
- #endif // CPENTAGON_H
- #include <iostream>
- #include "CRectangle.h"
- CRectangle::CRectangle()
- {
- std::cout << "CRectangle::CRectangle(): Started and finished." << std::endl;
- }
- void CRectangle::Identify()
- {
- std::cout << "CRectangle::CRectangle(): I am a CRectangle object." << std::endl;
- }
- #ifndef CRECTANGLE_H
- #define CRECTANGLE_H
- #include "CShapeBase.h"
- class CRectangle : public CShapeBase
- {
- public:
- CRectangle();
- void Identify();
- };
- #endif // CRECTANGLE_H
- #ifndef CSHAPEBASE_H
- #define CSHAPEBASE_H
- class CShapeBase
- {
- public:
- virtual void Identify() = 0;
- };
- #endif // CSHAPEBASE_H
- #include <iostream>
- #include "CSquare.h"
- CSquare::CSquare()
- {
- std::cout << "CSquare::CSquare(): Started and finished." << std::endl;
- }
- void CSquare::Identify()
- {
- std::cout << "CSquare::CSquare(): I am a CSquare object." << std::endl;
- }
- #ifndef CSHAPE_H
- #define CSHAPE_H
- #include "CShapeBase.h"
- class CSquare : public CShapeBase
- {
- public:
- CSquare();
- void Identify();
- };
- #endif // CSHAPE_H
- #include <iostream>
- #include "CFactory.h"
- int main()
- {
- std::cout << "main(): Started." << std::endl;
- CFactory myFactory;
- std::shared_ptr<CShapeBase> myBasePtr = myFactory.Produce();
- myBasePtr->Identify();
- std::cout << "main(): Finished." << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement