Answer to Question #274616 in C++ for Amit

Question #274616

Create a class Data with data members: height and breadth of object given by the user and member functions get_data() to read the values and put_data() to display the values. Create another class Rectangle that inherits class Data and implement its methods area Rectangle() and perimeter Rectangle() that computes the area and perimeter of a rectangle. Create another class Triangle that inherits class Data and implement its methods area Triangle() and perimeter Triangle() that computes the area and perimeter of a triangle. 



1
Expert's answer
2021-12-04T10:19:13-0500
include<iostream>
#include<conio.h>
using namespace std;
class shape {            
     protected:
          long ar, prmtr;
     public:
          virtual void area()=0;
          virtual void parimeter()=0;
};
class square : public shape {
     private:
          long side;          
     public:
          square(){      
              ar=prmtr=side=0;
          }
          void get(){         
              cout<<"Enter a side of square:";
              cin>>side;
          }
          void area(){
              ar=side*side;       
              cout<<"Area of the square is:"<<ar;
          }
          void parimeter(){
              prmtr=4*side;       
              cout<<endl<<"The perimeter of the square is:"<<prmtr;
          }
};
class triangle : public shape {
     private:
          long base, height, hyp;  
     public:
          triangle(){             
              ar=prmtr=base=height=hyp=0;
          }
          void input(){            
              cout<<endl<<"Enter three sides of Right triangle:";
              cin>>base>>height>>hyp;
          }
          void area(){
              ar=0.5*base*height; 
              cout<<endl<<"The area of Right Triangle is:"<<ar;
          }
          void parimeter(){
              prmtr=base+height+hyp;   
              cout<<endl<<"The parimeter of Right Triangle id:"<<prmtr;
          }
};
int main(){
     square s1;
     triangle t1;
     s1.get();
     s1.area();
     s1.parimeter();
     t1.input();
     t1.area();
     t1.parimeter();
     getch();
     return 0;
    
}

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