網頁

2016年11月14日 星期一

Template part5

#include <iostream>
using namespace std;

template <class Type1 = string, class Type2 = int >class MyClass{

Type1 p1;
Type2 p2;

public:
    MyClass(Type1 x, Type2 y){
    p1 = x;
    p2 = y;
    }
    void whatYouGot(){
    cout << "i got "<<p1<<" and "<<p2<<endl;
    }
};

int main()
{
    MyClass <> ob1("anil",24);
    MyClass <float> ob2(22.36,55);
    MyClass <int,string> ob3(21,"anjali");
    ob1.whatYouGot();
    ob2.whatYouGot();
    ob3.whatYouGot();
    return 0;
}


#include <iostream>
using namespace std;

template <class Type1 , class Type2 = int >class MyClass{

Type1 p1;
Type2 p2;

public:
    MyClass(Type1 x, Type2 y){
    p1 = x;
    p2 = y;
    }
    void whatYouGot(){
    cout << "i got "<<p1<<" and "<<p2<<endl;
    }
};

int main()
{
    MyClass <string> ob1("anil",24);
    MyClass <float> ob2(22.36,55);
    MyClass <int,string> ob3(21,"anjali");
    ob1.whatYouGot();
    ob2.whatYouGot();
    ob3.whatYouGot();
    return 0;
}

沒有留言:

張貼留言