C++ Class template Deriving a Class Template from a Class

Description

C++ Class template Deriving a Class Template from a Class

#include <iostream>

using namespace std;

class SuperMath/*w w w.  j a  va2s  . c  o  m*/
{
public:
    int IQ;
};

template <typename T>
class MyNumber : public SuperMath
{
public:
    T value;

    T &AddTo(T another)
    {
        value += another;
        return value;
    }

    T &SubtractFrom(T another)
    {
        value -= another;
        return value;
    }
};

void IncreaseIQ(SuperMath &inst)
{
    inst.IQ++;
}

int main()
{
    MyNumber<int> First;
    First.value = 10;
    First.IQ = 206;
    cout << First.AddTo(20) << endl;

    MyNumber<float> Second;
    Second.value = 20.5;
    Second.IQ = 201;
    cout << Second.SubtractFrom(1.3) << endl;

    IncreaseIQ(First);
    IncreaseIQ(Second);
    cout << First.IQ << endl;
    cout << Second.IQ << endl;

    return 0;
}



PreviousNext

Related