C++ Class template Using Different Types as Template Parameter

Description

C++ Class template Using Different Types as Template Parameter

#include <iostream>

using namespace std;

template <typename T>
class MyClass/*from   w  w w.j a va  2  s. co  m*/
{
public:
    T member;
};

template <int MaxPrice>
class PriceController
{
public:
    int Price;
    void TestPrice()
    {
        if (Price > MaxPrice)
        {
            cout << "Too expensive" << endl;
        }
    }
};

int main()
{
    MyClass<int> myClass;
    myClass.member = 2;
    cout << myClass.member << endl;

    const int c = 30;
    PriceController<c> my2;
    my2.Price = 15;
    my2.TestPrice();

    PriceController<c> my3;
    my3.Price = 45;
    my3.TestPrice();

    const int JulieMaxPrice = 60;
    PriceController<JulieMaxPrice> JuliesCar;
    JuliesCar.Price = 80;
    JuliesCar.TestPrice();

    return 0;
}



PreviousNext

Related