Answer to Question #213023 in C++ for Hemambar

Question #213023

 Create a base class called Shape which has two double type values. Use member functions get() and set() to get and set the values. Use a virtual function display_area() to display the area. Derive two specific classes called Triangle and Rectangle from the base class Shape. Redefine display_area() in the derived classes. Write a program to display the rectangle or triangle area. 




1
Expert's answer
2021-07-11T14:34:19-0400
#include<iostream>

using namespace std;
class Shape {
private:
double first, second;
public:
void setFirst(double f)
{ first =f;}
void  setSecond(double s)
{second = s;
}
double getFirst(){
return first;
}
double getSecond(){ 
return second;
}
virtual void display_area(){}
};
class Triangle: public Shape{
public:
void display_area(){
cout<<"The area of the triangle is \t"<< 0.5*getFirst()*getSecond();
}
};
class Rectangle: public Shape{
public:
void display_area(){
cout<<"The area of the rectangle is \t"<< getFirst()*getSecond()<<endl;}
};
int main(){
Rectangle rec;
rec.setSecond(9);
rec.setFirst(12); 
rec.display_area();
Triangle tr;
tr.setSecond(9);
tr.setFirst(12);
 tr.display_area();}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS