thespeedracer38

InheritanceOfTemplate

Mar 25th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. class Item{
  8.     protected:
  9.         T *contents;
  10.         int capacity, size;
  11.     public:
  12.         Item(int = 10);
  13.         ~Item();
  14.         int getSize();
  15.         int getCapacity();
  16.         bool isPresent(T);
  17.         bool isFull();
  18. };
  19.  
  20. template <class T>
  21. Item<T>::Item(int capacity){
  22.     contents = new T[Item<T>::capacity = capacity];
  23.     size = 0;
  24. }
  25.  
  26. template <class T>
  27. Item<T>::~Item(){
  28.     delete[] contents;
  29. }
  30.  
  31. template <class T>
  32. bool Item<T>::isPresent(T item){
  33.     int pos = -1;
  34.     for(int i = 0; i < size; i++){
  35.         if(contents[i] == item){
  36.             pos = i;
  37.             break;
  38.         }
  39.     }
  40.     return pos != -1;
  41. }
  42.  
  43. template <class T>
  44. bool Item<T>::isFull(){
  45.     return size == capacity;
  46. }
  47.  
  48. template <class S>
  49. class Set:public Item<S>{
  50.     public:
  51.         Set(int = 1);
  52.         Set(int , int);
  53.         Set(const Set<S>&);
  54.         void read();
  55.         void add(S);
  56.         void display();
  57.         Set<S>& operator =(const Set<S>&);
  58.         Set<S> operator +(const Set<S>&);
  59. };
  60.  
  61. template <class S>
  62. void Set<S>::display(){
  63.     for(int i = 0; i < Item<S>::size; i++){
  64.         cout << Item<S>::contents[i] << " ";
  65.     }
  66.     cout << endl;
  67. }
  68.  
  69. template <class S>
  70. Set<S>::Set(int capacity):Item<S>(capacity){ }
  71.  
  72. template <class S>
  73. Set<S>::Set(int capacity, int size):Item<S>(capacity){
  74.     Item<S>::size = size;
  75. }
  76.  
  77. template <class S>
  78. Set<S>::Set(const Set<S> &obj){
  79.     Item<S>::contents = new S[obj.capacity];
  80.     Item<S>::capacity = obj.capacity;
  81.     Item<S>::size = obj.size;
  82.     for(int i = 0; i < obj.size; i++){
  83.         Item<S>::contents[i] = obj.contents[i];
  84.     }
  85. }
  86.  
  87. template <class S>
  88. Set<S>& Set<S>::operator =(const Set<S> &obj){
  89.     Item<S>::size = obj.size;
  90.     Item<S>::capacity = obj.capacity;
  91.     for(int i = 0; i < obj.size; i++){
  92.         Item<S>::contents[i] = obj.contents[i];
  93.     }
  94.     return *this;
  95. }
  96.  
  97. template <class S>
  98. Set<S> Set<S>::operator +(const Set<S> &obj){
  99.     Set<S> temp(obj.capacity + Item<S>::capacity);
  100.     // Copying from the current object
  101.     for(int i = 0; i < Item<S>::size; i++){
  102.         temp.add(Item<S>::contents[i]);
  103.     }
  104.     // Copying from the obj object
  105.     for(int i = 0; i < obj.size; i++){
  106.         temp.add(obj.contents[i]);
  107.     }
  108.     return temp;
  109. }
  110.  
  111. template <class S>
  112. void Set<S>::read(){
  113.     cout << "Input values to set\n";
  114.     cout << "Enter -1 to stop reading\n";
  115.     int input;
  116.     while(!Item<S>::isFull() ){
  117.         cout << "Enter value: ";
  118.         cin >> input;
  119.         if(input == -1){
  120.             break;
  121.         }
  122.         else{
  123.             add(input);
  124.         }
  125.     }
  126. }
  127.  
  128. template <class S>
  129. void Set<S>::add(S item){
  130.     if(!Item<S>::isFull() && !Item<S>::isPresent(item)){
  131.         Item<S>::contents[Item<S>::size] = item;
  132.         Item<S>::size = Item<S>::size + 1;
  133.     }
  134. }
  135.  
  136.  
  137. int main() {
  138.     Set<int> s1(5);
  139.     Set<int> s2(7);
  140.     Set<int> s3;
  141.    
  142.     s1.read();
  143.     s2.read();
  144.    
  145.     // Union operation
  146.     s3 = s1 + s2;
  147.    
  148.     cout << "s1\n";
  149.     s1.display();
  150.    
  151.     cout << "s2\n";
  152.     s2.display();
  153.    
  154.     cout << "s3\n";
  155.     s3.display();
  156.    
  157.     return 0;
  158. }
Add Comment
Please, Sign In to add comment