Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9.  
  10. #include <iostream>
  11. #include "CCircle.h"
  12.  
  13. CCircle::CCircle()
  14. {
  15. std::cout << "CCircle::CCircle(): Started and finished." << std::endl;
  16. }
  17.  
  18. void CCircle::Identify()
  19. {
  20. std::cout << "CCircle::CCircle(): I am a CCircle object." << std::endl;
  21. }
  22.  
  23. #ifndef CCIRCLE_H
  24. #define CCIRCLE_H
  25.  
  26. #include "CShapeBase.h"
  27.  
  28. class CCircle : public CShapeBase
  29. {
  30. public:
  31. CCircle();
  32. void Identify();
  33. };
  34.  
  35. #endif // CCIRCLE_H
  36.  
  37. #include <iostream>
  38. #include <memory>
  39.  
  40. #include "CFactory.h"
  41.  
  42. #include "CCircle.h"
  43. #include "CSquare.h"
  44. #include "CRectangle.h"
  45. #include "CPentagon.h"
  46. #include "CHexagon.h"
  47.  
  48. std::shared_ptr<CShapeBase> CFactory::Produce()
  49. {
  50. std::shared_ptr<CShapeBase> myBasePtr = nullptr;
  51.  
  52. while(myBasePtr == nullptr)
  53. {
  54. std::cout << "Please select your shape:" << std::endl;
  55. std::cout << "1) Circle" << std::endl;
  56. std::cout << "2) Square" << std::endl;
  57. std::cout << "3) Rectangle" << std::endl;
  58. std::cout << "4) Pentagon" << std::endl;
  59. std::cout << "5) Hexagon" << std::endl;
  60.  
  61. int input = 0;
  62. std::cin >> input;
  63.  
  64. switch(input)
  65. {
  66. case 1:
  67. myBasePtr = std::make_shared<CCircle>();
  68. break;
  69. case 2:
  70. myBasePtr = std::make_shared<CSquare>();
  71. break;
  72. case 3:
  73. myBasePtr = std::make_shared<CRectangle>();
  74. break;
  75. case 4:
  76. myBasePtr = std::make_shared<CPentagon>();
  77. break;
  78. case 5:
  79. myBasePtr = std::make_shared<CHexagon>();
  80. break;
  81. default:
  82. std::cout << "CFactory::Produce(): Did not produce a shape." << std::endl;
  83. break;
  84. }
  85. }
  86.  
  87. return myBasePtr;
  88. }
  89.  
  90. #ifndef CFACTORY_H
  91. #define CFACTORY_H
  92.  
  93. #include <memory>
  94.  
  95. #include "CShapeBase.h"
  96.  
  97. // Include derives classes here?
  98.  
  99. class CFactory
  100. {
  101. public:
  102. std::shared_ptr<CShapeBase> Produce();
  103. };
  104.  
  105. #endif // CFACTORY_H
  106.  
  107. #include <iostream>
  108. #include "CHexagon.h"
  109.  
  110. CHexagon::CHexagon()
  111. {
  112. std::cout << "CHexagon::CHexagon(): Started and finished." << std::endl;
  113. }
  114.  
  115. void CHexagon::Identify()
  116. {
  117. std::cout << "CHexagon::CHexagon(): I am a CHexagon object." << std::endl;
  118. }
  119.  
  120. #ifndef CHEXAGON_H
  121. #define CHEXAGON_H
  122.  
  123. #include "CShapeBase.h"
  124.  
  125. class CHexagon : public CShapeBase
  126. {
  127. public:
  128. CHexagon();
  129. void Identify();
  130. };
  131.  
  132. #endif // CHEXAGON_H
  133.  
  134. #include <iostream>
  135. #include "CPentagon.h"
  136.  
  137. CPentagon::CPentagon()
  138. {
  139. std::cout << "CPentagon::CPentagon(): Started and finished." << std::endl;
  140. }
  141.  
  142. void CPentagon::Identify()
  143. {
  144. std::cout << "CPentagon::CPentagon(): I am a CPentagon object." << std::endl;
  145. }
  146.  
  147. #ifndef CPENTAGON_H
  148. #define CPENTAGON_H
  149.  
  150. #include "CShapeBase.h"
  151.  
  152. class CPentagon : public CShapeBase
  153. {
  154. public:
  155. CPentagon();
  156. void Identify();
  157. };
  158.  
  159. #endif // CPENTAGON_H
  160.  
  161. #include <iostream>
  162. #include "CRectangle.h"
  163.  
  164. CRectangle::CRectangle()
  165. {
  166. std::cout << "CRectangle::CRectangle(): Started and finished." << std::endl;
  167. }
  168.  
  169. void CRectangle::Identify()
  170. {
  171. std::cout << "CRectangle::CRectangle(): I am a CRectangle object." << std::endl;
  172. }
  173.  
  174. #ifndef CRECTANGLE_H
  175. #define CRECTANGLE_H
  176.  
  177. #include "CShapeBase.h"
  178.  
  179. class CRectangle : public CShapeBase
  180. {
  181. public:
  182. CRectangle();
  183. void Identify();
  184. };
  185.  
  186. #endif // CRECTANGLE_H
  187.  
  188. #ifndef CSHAPEBASE_H
  189. #define CSHAPEBASE_H
  190.  
  191. class CShapeBase
  192. {
  193. public:
  194. virtual void Identify() = 0;
  195. };
  196.  
  197. #endif // CSHAPEBASE_H
  198.  
  199. #include <iostream>
  200. #include "CSquare.h"
  201.  
  202. CSquare::CSquare()
  203. {
  204. std::cout << "CSquare::CSquare(): Started and finished." << std::endl;
  205. }
  206.  
  207. void CSquare::Identify()
  208. {
  209. std::cout << "CSquare::CSquare(): I am a CSquare object." << std::endl;
  210. }
  211.  
  212. #ifndef CSHAPE_H
  213. #define CSHAPE_H
  214.  
  215. #include "CShapeBase.h"
  216.  
  217. class CSquare : public CShapeBase
  218. {
  219. public:
  220. CSquare();
  221. void Identify();
  222. };
  223.  
  224. #endif // CSHAPE_H
  225.  
  226. #include <iostream>
  227. #include "CFactory.h"
  228.  
  229. int main()
  230. {
  231. std::cout << "main(): Started." << std::endl;
  232.  
  233. CFactory myFactory;
  234.  
  235. std::shared_ptr<CShapeBase> myBasePtr = myFactory.Produce();
  236.  
  237. myBasePtr->Identify();
  238.  
  239. std::cout << "main(): Finished." << std::endl;
  240. return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement