Professional Documents
Culture Documents
Task 1:
Create a class Rectangle with data members length, breath and height. Members
functions of class should be Volume and Compare, to calculate volume of the box and
compare two objects’ volume to check the object with greater volume.
(Implementation : using this pointer)
ANSWER:
#include<iostream>
using namespace std;
public :
rectangle(){}
T volume (T a, T b , T c){
length=a ;
breadth=b ;
height =c;
return length*breadth*length;
}
};
template <class T>
T compare (T obj , T obj2){
if (obj> obj2){
cout<<obj<<endl;
}
else
cout<<obj2<<endl;
return 0;
}
Task 2:
Write a simple function template for predicate function isEqualTo that compares its two
arguments of the same type with the equality operator (==) and returns true if they are equal
and false otherwise. Use this function template in a program that calls isEqualTo only with a
variety of fundamental types.
ANSWER:
#include<iostream>
using namespace std;
if (value1 == value2)
return true;
else
return false ;
}
int main(){
int a =67, b=77;
double m=55,n=55;
float c=33 , d=65;
cout<<isEqualto<int>(a,b)<<endl;
cout<<isEqualto<double>(m,n)<<endl;
cout<<isEqualto<float>(c,d)<<endl;
return 0;
}
Task 3:
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.
ANSWER:
#include<iostream>
using namespace std;
int main(){
int arr[5]= {44,55,3,2,1};
float crr[5]= {4.4,5.5,3.2,2.7,1.1};
cout<<isEqualto<int>(arr)<<endl;
cout<<isEqualto<float>(crr)<<endl;
return 0;
}
Total 40 Signature
Note : Attempt all tasks and get them checked by your Lab Instructor.