Advertisement
llsumitll

Factory Design + polymorphs

Jan 29th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | Source Code | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Invoice
  5. {
  6. public:
  7.     virtual void printInvoice(){}
  8. };
  9.  
  10. class clsInvoiceWithHeader : public Invoice
  11. {
  12. public:
  13.     void printInvoice()
  14.     {
  15.         cout << "Hello Client -  I am clsInvoiceWithHeader" << endl;
  16.     }
  17. };
  18.  
  19. class clsInvoiceWithoutHeader : public Invoice
  20. {
  21. public:
  22.     void printInvoice()
  23.     {
  24.         cout << "Hello Client -  I am clsInvoiceWithoutHeader" << endl;
  25.     }
  26. };
  27.  
  28. class clsInvoiceWithoutFooter : public Invoice
  29. {
  30. public:
  31.     void printInvoice()
  32.     {
  33.         cout << "Hello Client -  I am clsInvoiceWithoutFooter" << endl;
  34.     }
  35. };
  36.  
  37. class FactoryInvoice
  38. {
  39. public:
  40.     static Invoice * getInvoiceType( int inttype) ;
  41. };
  42. //static Invoice * invoice = nullptr;
  43.  
  44.  
  45.  
  46. Invoice * FactoryInvoice::getInvoiceType( int inttype)
  47. {
  48.        
  49.         if( inttype == 1)
  50.         {
  51.             return (new clsInvoiceWithHeader());
  52.         }
  53.         if ( inttype == 2)
  54.         {
  55.             return (new clsInvoiceWithoutHeader());
  56.         }
  57.         if ( inttype == 3)
  58.         {
  59.             return (new clsInvoiceWithoutFooter());
  60.         }
  61.         else
  62.             return nullptr;
  63. }
  64.  
  65. int main()
  66. {
  67.     Invoice* iv = nullptr;
  68.    
  69.         iv = FactoryInvoice::getInvoiceType(1);
  70.     if (iv != nullptr){
  71.             iv -> printInvoice();
  72.         }
  73.    
  74.         iv = FactoryInvoice::getInvoiceType(2);
  75.         if (iv != nullptr){
  76.             iv -> printInvoice();
  77.         }
  78.  
  79.         iv = FactoryInvoice::getInvoiceType(3);
  80.     if (iv != nullptr){
  81.         iv -> printInvoice();
  82.     }
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement