Thursday, 21 September 2017

Class templates having single level inheritance in C++

/*
 Class templates having single level inheritance 
*/
#include "iostream"
using namespace std;

template <class T>
class First{
 T x;
 public:
  void getFirst(T a){
   x = a;
  }
  void showFirst(){
   cout<<"\nx: "<<x<<endl;
  }
};

template <class T2>
class Second: public First<T2>{
 T2 y, z;
 public:
  Second(T2 m, T2 n){
   y = m;
   z = n;
  }
  void showAll(){
//   display();
   cout<<"\ny: "<<y<<"\tz: "<<z<<endl;
  }
};

int main(){
 First<int> ob1;
 ob1.getFirst(10);
 ob1.showFirst();
 Second <int> ob2(20,30);
 ob2.showAll();
 return 0;

}

/*
 OUTPUT

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

x: 10

y: 20 z: 30
[pavan@Pavan ClassTemp]$ 

*/

No comments: