In: Computer Science
Write a C++ program that takes input from the keyboard of the 3 dimensions of a room (W,L,H in feet) and calculates the area (square footage) needed to paint the walls only. Use function overloading in your program to pass variables of type int and double. You should have two functions: one that accepts int datatypes and one that accepts double datatypes. Also assume a default value of 8 if the height parameter is omitted when calling the functions.
Function overloading is the technique of calling same function name with different parameter
----------------------------------------------------------------------------------
a room has 1 floor, 4 walls, 1 roof. suppose we have to paint 4 walls and 1 roof. then
total area = 4 walls area+ 1roof area
= 2 opposit walls( length * height) + 2 opposit walls (breadth* height)
+ 1 roof( length * breadth)
=2*(L*H)+2*(B*H)+L*B;
user will input height but we will omitt it and use default value 8.
#include<iostream>
using namespace std;
int function(int x,int y,int z) // SAME FUNCTION NAME
{
int L=x;
int B=y;
int H=z;
int area=2*(L*H)+2*(B*H)+L*B;
cout<<"\n passing int data type \n ";
return area;
}
double function(double x,double y,double z) // SAME FUNCTION NAME
{
double L=x;
double B=y;
double H=z;
double area=2*(L*H) + 2* (B*H) + L*B;
cout<<"\n passing double data type \n ";
return area;
}
int main()
{
cout<<"\nwhich function do you want to use :\n 1.type double \n 2.type int \nenter (1/2) :";
int option;
cin>>option;
if (option==1)
{
float L,B,H;
cout<<"\nenter the length: ";
cin>>L;
cout<<"enter the Bredth: ";
cin>>B;
H=10;
cout<<"enter the height: ";
cin>>H;
cout<<"\n total area in square foot= "<<function(L,B,8.0)<<"\n\n"; // calling function , height omitted, default value of 8 used
}
else if(option==2)
{
int L,B,H;
cout<<"\nenter the length: ";
cin>>L;
cout<<"enter the Bredth: ";
cin>>B;
cout<<"enter the height: ";
cin>>H;
cout<<"\n total area in square foot= "<<function(L,B,8)<<"\n\n"; // calling function , height omitted, default value of 8 used
}
else
{
cout<<"invalid option\n";
}
return 0();
}
output