rfog

Using same class to instantiate two non-compatible devices

Aug 26th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. // TemplateSpecializationInheritance.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. /*THIS IS THE OLD STRUCTURE*/
  10. class Device
  11. {
  12. public:
  13.     virtual void Print() = 0;
  14. };
  15.  
  16. class InputDevice: public Device
  17. {
  18. public:
  19.     void Print() override { cout << "InputDevicePrint" << endl; }
  20.  
  21.     virtual void InputPrint() = 0;
  22. };
  23.  
  24. class InputOutputDevice: public Device
  25. {
  26. public:
  27.     void Print() override { cout << "InputOutputDevicePrint" << endl; }
  28.  
  29.     virtual void InputPrint() = 0;
  30.     virtual void OutputPrint() = 0;
  31. };
  32.  
  33. class OldInput:public InputDevice
  34. {
  35. public:
  36.     void InputPrint() override { cout << "OldInputPrint" << endl; }
  37. };
  38.  
  39. class OldInputOutput:public InputOutputDevice
  40. {
  41. public:
  42.     void InputPrint() override { cout << "OldInputPrint" << endl; }
  43.     void OutputPrint() override { cout << "OldOutputPrint" << endl; }
  44.  
  45. };
  46.  
  47. /*THE NEW ENVELOPE*/
  48. template <typename T> class NewInputDevice:public T
  49. {
  50. public:
  51.     void Print() override { cout << "newPrint" << endl; }
  52.     void InputPrint() override { cout << "NewInputPrint" << endl; }
  53.  
  54. };
  55.  
  56. class NewInputOutputPrint:public NewInputDevice<InputOutputDevice>
  57. {
  58. public:
  59.     void OutputPrint() override { cout << "NewOutputPrint" << endl; }
  60. };
  61. int main()
  62. {
  63.     OldInput oInput;
  64.     oInput.Print();                 //Shows InputDevicePrint
  65.     oInput.InputPrint();            //Shows OldInputPrint
  66.  
  67.     OldInputOutput oInputOutput;
  68.     oInputOutput.Print();           //Shows InputDevicePrint
  69.     oInputOutput.InputPrint();      //Shows OldInputPrint
  70.     oInputOutput.OutputPrint();     //Shows OldOutputPrint
  71.  
  72.     NewInputDevice<InputDevice> nInput;
  73.     nInput.Print();                 //Shows NewPrint
  74.     nInput.InputPrint();            //Shows NewInputPrint
  75.  
  76.     NewInputOutputPrint nInputOutpur;
  77.     nInputOutpur.Print();           //Shows NewPrint
  78.     nInputOutpur.InputPrint();      //Shows NewInputPrint
  79.     nInputOutpur.OutputPrint();     //NewOutputPrint
  80.    
  81.     return 0;
  82. }
Add Comment
Please, Sign In to add comment