Templates and its types in oop(object oriented programming)

Templates

Task 1:

Write a template function that returns the average of all the elements of an array. The arguments to the function should be the array name and the size of the array (type int). In main(), exercise the function with arrays of type int, long, double, and char.

#include<iostream>
using namespace std;
template <class T>
class Average{
            T *arr;
            int index;
public:
            Average()
            {
                        index = 0;
            }
            Average(int a,T *b)
            {
                        index = a;
                        arr = new T[index];
                        arr = b;
            }
            void ave()
            {
                        T sum = 0,average;
                        for (int i = 0; i < index; i++)
                        {
                                    sum = sum + arr[i];
                        }
                        average = sum / index;
                        cout << "Average is:" << average << endl;
            }
            void show()
            {
                        for (int i = 0; i < index; i++)
                        {
                                    cout << "Values of array is:" << arr[i] << endl;
                        }
            }
            ~Average()
            {
                        cout << "It is deleted" << endl;
                        cin.get();
                        delete[]arr;
            }
};
int main()
{
            const int size=5;
            int arr[size];
            for (int i = 0; i < size; i++)
            {
                        cout << "Enter the values for array:"; cin >> arr[i];
            }
            Average<int>a1(size,arr);
            a1.ave();
            getchar();
            getchar();
            return 0;
}      

          

       
 

Task 2:

Createa class Rectangle with data members length, breath and height. Members functions of class should be Cal_Volume and Compare, to calculate volume of the box and compare two objects’ volume to check which object have greater volume. (Implementation: usethispointer).

#include<iostream>
using namespace std;
class Rectangle{
            int length;
            int breath;
            int height;
public:
            int vol;
            Rectangle()
            {
                        length = breath = height = 0;
            }
            Rectangle(int a, int b, int c)
            {
                        length = a; breath = b; height = c;
            }
            void call_volume(){
                        cout << "Volume is calculating" << endl;
                        vol = length*breath*height;
                        cout << "Volume is calculated:" << vol << endl;
                        cout << endl << endl;
            }
            Rectangle compare(Rectangle &r, Rectangle &rr)
            {
                        if (r.vol > rr.vol)
                                    cout << "1st Rectangle is greater than other" << endl;
                        else
                                    cout << "2nd Rectangle is greater than 1st" << endl;
                        return *this;
            }
};
int main()
{
            Rectangle r1(11, 12, 13), r2(16, 18, 12),r3;
            r1.call_volume();
            r2.call_volume();
            r3.compare(r1, r2);
            getchar();
            getchar();
            return 0;
}

          

       
 

Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........