Introduction to Generic Classes






This video tutorial explains about the generic classes in c++ programming.
You are gonna learn how to define a generic class, how to pass generic types to generic class, how to create objects from a generic class, how to specify the datatypes for the objects created from a generic class, how to use the generic classes or template classes in detail with example.

source code for this tutorial

#include <iostream>

using namespace std;

template <class MYTYPE>class MyClass{
MYTYPE p1;
MYTYPE p2;

public:
    MyClass(MYTYPE x,MYTYPE y){
    p1 = x;
    p2 = y;
    }

    void whatYouGot(){
    cout << "i got p1 = "<<p1<<" and p2 = "<<p2<<endl;
    }


};


int main()
{
    MyClass<int> intObject(22,55);

    MyClass <string> stringObj("anil","anjali");

    intObject.whatYouGot();
    stringObj.whatYouGot();

    return 0;
}

 
Back To Top