Thursday, 21 September 2017

Simple class template having simple function template in C++

/*
 Simple class template having simple function template
*/
#include "iostream"
using namespace std;

template <class T>
class Person{
 T age;
 public:
 void getData(T a){
  age = a;
 }
 T showData();
};

template <class T> T Person<T> :: showData(){
 return age;
}
int main(){
 Person <int> ob;
 ob.getData(32);
 cout<<"\nage: "<<ob.showData()<<endl;
 return 0;
}
 
/*
 OUTPUT

[pavan@Pavan ClassTemp]$ g++ ClassTemp3.cpp
[pavan@Pavan ClassTemp]$ ./a.out 

age: 32
[pavan@Pavan ClassTemp]
*/

No comments: