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:
a) 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]
b) 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>
#include <string>
using namespace std;
// 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.
template <typename T>
T shape(T base,T height, string shapeName){
T area=0;
if(shapeName=="rectangle"){
area=base*height;
}
if(shapeName=="tirangle"){
area=0.5*base*height;
}
cout<<"Base: "<<base<<"\n";
cout<<"Height: "<<height<<"\n";
cout<<"The area of the shape "<<shapeName<<": "<<area<<"\n";
return area;
}
//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.
template <typename T>
T show_master(int index,T values[]){
return values[index];
}
int main () {
//[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]
shape(5,4,"rectangle");
shape(10,5,"tirangle");
shape(5.5,4.6,"rectangle");
shape(10.4,5.5,"tirangle");
int intValues[] = {10, 10, 12, 445, 1};
double doubleValues[] = {1.5, 23.48, 41.89, 11.85,2.326};
cout<<"\n";
for(int i=0;i<5;i++){
cout<<intValues[i]<<" ";
}
cout<<"\nValue at index 2 = "<<show_master(2,intValues);
cout<<"\n";
for(int i=0;i<5;i++){
cout<<doubleValues[i]<<" ";
}
cout<<"\nValue at index 0 = "<<show_master(0,doubleValues);
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment