Write a class Quadrilateral that has: 4 Variables representing the length of the
ID: 3758175 • Letter: W
Question
Write a class Quadrilateral that has:
4 Variables representing the length of the 4 sides
A function that returns the perimeter of the quadrilateral.
Any other necessary accessor methods you find needed to complete this assignment.
Write a subclass Rectangle that:
Has a variable that represents the length of the diagonal.
Has a function that calculates the area.
Has a function that returns the diagonal.
Write a subclass Trapezoid that:
Has a function that calculates the area.
Has a boolean function that tells whether the Trapezoid is an isosceles trapezoid (two of the sides are equal).
Write a main that tests all three classes.
Help with this C++ problem
Explanation / Answer
Answer:
//inheritance program for Quadrilateral
#include <iostream>
#include <cmath>
#include<cstdlib>
using namespace std;
//class Quadrilateral
class Quadrilateral
{
//four variables for 4 sides
protected:
double side1,side2,side3,side4;
public:
//constructor
Quadrilateral(double s1,double s2,double s3,double s4)
: side1(s1), side2(s2), side3(s3), side4(s4) {}
Quadrilateral() {}
//get the perimeter
double getPerimeter()
{
return (side1+side2+side3+side4);
}
};
// class Rectangle is inherited from Quadrilateral
class Rectangle: public Quadrilateral
{
//has variable for diagonal
protected:
double diagonal;
public:
Rectangle(double s1,double s2)
: Quadrilateral(s1,s2,s1,s2) { diagonal =0; }
//calculate the area
double getArea()
{
return (side1*side2);
}
//calculate the diagonal
double getDiagonal()
{
diagonal=sqrt((side1*side1)+(side2*side2));
return diagonal;
}
};
// class Trapezoid is inherited from Quadrilateral
class Trapezoid: public Quadrilateral
{
public:
Trapezoid(double s1,double s2,double s3,double s4)
: Quadrilateral(s1,s2,s3,s4) { }
//return the area of the Trapezoid
double getArea()
{
double temp1,temp2;
temp1=(side1+side2-side3+side4)*(side1-side2-side3+side4)*(side1+side2-side3-side4)*(side2+side4-side1+side3);
temp2=(side1+side3)/(4*(side1-side3));
return (temp1*temp2);
}
//check Trapezoid is isoceless
bool isIsoceless()
{
if(side2==side4)
return true;
return false;
}
};
//main method
int main()
{
//create object for Quadrilateral
Quadrilateral quad(10,20,30,40);
//print the Quadrilateral perimeter
cout<<" QUADRILATERAL PERIMETER:"<<quad.getPerimeter()<<endl;
//create object for Rectangle
Rectangle rec(10,20);
cout<<" RECTANGLE AREA:"<<rec.getArea()<<endl;
cout<<" RECTANGLE DIAGONAL:"<<rec.getDiagonal()<<endl;
Trapezoid trap(10,20,20,30);
cout<<"TRAPEZOID AREA:"<<trap.getArea()<<endl;
cout<<" IS TRAPEZOID Isoceless:"<<trap.isIsoceless()<<endl;
return 0;
}//main ends
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.