You have been developing a small commercial Software for numeric operations. You are working on the calculation of areas and volume of different shapes. Based on your software complete these tasks:
Create a function template named as shape() that accepts three arguments in such a way that first and second will be the dimension like base and height and the third will be the name of the shape. The shape() function displays the dimension values and the area of the shape. The function returns the area to the calling program; the area is the same data type as the parameter. [Note: Design a main program such that, it input the values of only those shapes whose area is computable on the bases of base and height only]
Create another function template named as show_master()that accepts two arguments. The first is index and the second argument is an array of 10 values. The show_master() function will returns the specified index value.
#include<iostream>
using namespace std;
template<typename P>
P shape(P first_dimension, P second_dimension, string name_of_shape ){
P area ;
if(name_of_shape == "Triangle"){
area = 0.5 * first_dimension * second_dimension;
cout<<"The base of the Triangle is\t"<<first_dimension<<endl;
cout<<"The height of the Triangle is\t"<<second_dimension<<endl;
cout<<"The area of the Triangle is\t"<<area<<endl;
}
else if(name_of_shape == "Rectangle"){
area = first_dimension * second_dimension;
cout<<"The base of the Rectangle is\t"<<first_dimension<<endl;
cout<<"The height of the Rectangle is\t"<<second_dimension<<endl;
cout<<"The area of the Rectangle is\t"<<area<<endl;
}
}
template<typename S>
 S show_master(S index, S arr[10]){
return arr[index];
}
//Testing function
int main(){
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
cout<<"The value at 1 is \t"<<show_master(1,arr)<<endl;
shape(10, 20, "Rectangle" );
shape(10, 20, "Triangle" );
}
Comments
Leave a comment