bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_2 / eduincs / pgrm_024.cpp

pgrm_024.cpp (938B)


      1 /* WAP to find max from an array using template class and no-inline template function */
      2 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 template <class T>
      7 class Max {
      8 private:
      9     T *a;
     10     int s;
     11 
     12 public:
     13     Max() {
     14         cout << "Array size: ";
     15         cin >> s;
     16         a = new T[s];
     17     }
     18 
     19     void getData();
     20     T find_max();
     21 
     22     ~Max() {
     23         delete[] a;
     24         cout << "obj deleted.";
     25     }
     26 };
     27 
     28 template <class T>
     29 void Max<T>::getData() {
     30     cout << "Enter elements: ";
     31     for (int i = 0; i < s; ++i) {
     32         cin >> a[i];
     33     }
     34 }
     35 
     36 template <class T>
     37 T Max<T>::find_max() {
     38     T m = a[0];
     39     for (int i = 1; i < s; i++) {
     40         if (m < a[i]) {
     41             m = a[i];
     42         }
     43     }
     44     return m;
     45 }
     46 
     47 int main() {
     48     Max<int> obj;
     49     obj.getData();
     50     cout << "Max element: " << obj.find_max() << endl;
     51 
     52     Max<float> obj2;
     53     obj2.getData();
     54     cout << "Max element: " << obj2.find_max() << endl;
     55 
     56     return 0;
     57 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror