rfog icon

Using same class to instantiate two non-compatible devices

rfog | PRO | 08/26/16 11:21:20 AM UTC | 0 ⭐ | 441 👁️ | Never ⏰ | []
C++ |

1.89 KB

|

None

|

0 👍

/

0 👎

// TemplateSpecializationInheritance.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
 
using namespace std;
 
/*THIS IS THE OLD STRUCTURE*/
class Device
{
public:
    virtual void Print() = 0;
};
 
class InputDevice: public Device
{
public:
    void Print() override { cout << "InputDevicePrint" << endl; }
 
    virtual void InputPrint() = 0;
};
 
class InputOutputDevice: public Device
{
public:
    void Print() override { cout << "InputOutputDevicePrint" << endl; }
 
    virtual void InputPrint() = 0;
    virtual void OutputPrint() = 0;
};
 
class OldInput:public InputDevice
{
public:
    void InputPrint() override { cout << "OldInputPrint" << endl; }
};
 
class OldInputOutput:public InputOutputDevice
{
public:
    void InputPrint() override { cout << "OldInputPrint" << endl; }
    void OutputPrint() override { cout << "OldOutputPrint" << endl; }
 
};
 
/*THE NEW ENVELOPE*/
template <typename T> class NewInputDevice:public T
{
public:
    void Print() override { cout << "newPrint" << endl; }
    void InputPrint() override { cout << "NewInputPrint" << endl; }
 
};
 
class NewInputOutputPrint:public NewInputDevice<InputOutputDevice>
{
public:
    void OutputPrint() override { cout << "NewOutputPrint" << endl; }
};
int main()
{
    OldInput oInput;
    oInput.Print();                 //Shows InputDevicePrint
    oInput.InputPrint();            //Shows OldInputPrint
 
    OldInputOutput oInputOutput;
    oInputOutput.Print();           //Shows InputDevicePrint
    oInputOutput.InputPrint();      //Shows OldInputPrint
    oInputOutput.OutputPrint();     //Shows OldOutputPrint
 
    NewInputDevice<InputDevice> nInput;
    nInput.Print();                 //Shows NewPrint
    nInput.InputPrint();            //Shows NewInputPrint
 
    NewInputOutputPrint nInputOutpur;
    nInputOutpur.Print();           //Shows NewPrint
    nInputOutpur.InputPrint();      //Shows NewInputPrint
    nInputOutpur.OutputPrint();     //NewOutputPrint
    
    return 0;
}

Comments