Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class type \"Quadrilateral\" that represents general 2-dimensional shap

ID: 3586523 • Letter: D

Question

Define a class type "Quadrilateral" that represents general 2-dimensional shapes having four sides (for example: rectangle, square, trapezoid, etc). You can determine a four-sided shape by the coordinates of its four vertices. Use another class type Point to representa point on the shape (vertex for example). For the class type Point, add the necessary members as appropriate (coordinates data members, functions to read/write coordinates, constructors, etc). For the class Type Quadrilateral, include four vertices, in addition to the following function members: 1- constructors to initialize the shape 2- Accessor and Manipulator functions to read/write the vertex members 3- A function to print out the information about the four vertices of the shape After this, define a new class type Rectangle" that inherits from Quadrilateral. Make sure to add code to check for right angle whern initializing or updating information of a Rectangle object. Add two new methods to the new type to determine its area and perimeter, respectively Also, override the print function of the base type to also print the info about area and perimeter of the rectangle. Write a test program that instantiates a Rectangle object and prints its information to the screen Attach File

Explanation / Answer

#include <iostream>
#include <math.h>
using namespace std;

//Class Point definition
class Point
{
protected:
//Data member to store x and y coordinates
double x1, y1;
public:
//Constructor to initialize coordinates to zero
Point()
{
x1 = y1 = 0;
}//End of constructor
//Getter and setter methods
void setX1(double x)
{
x1 = x;
}//End of function
void setY1(double y)
{
y1 = y;
}//End of function
double getX1()
{
return x1;
}//End of function
double getY1()
{
return y1;
}//End of function
//Function to accept x and y coordinates
void accept()
{
cout<<" Enter x1 coordinate: ";
cin>>x1;
cout<<" Enter y1 coordinate: ";
cin>>y1;
}//End of function
};//End of class Point

//Class Quadrilateral definition
class Quadrilateral
{
//Point class objects to store 4 vertices of the shape
Point bottomLeft, bottomRight, topRight, topLeft;
//To store length and breadth
double length;
double breadth;
public:
//Getter and setter method
void setBottomLeft(double x, double y)
{
bottomLeft.setX1(x);
bottomLeft.setY1(y);
}//End of function
void setBottomRight(double x, double y)
{
bottomRight.setX1(x);
bottomRight.setY1(y);
}//End of function
void setTopRight(double x, double y)
{
topRight.setX1(x);
topRight.setY1(y);
}//End of function
void setTopLeft(double x, double y)
{
topLeft.setX1(x);
topLeft.setY1(y);
}//End of function
double getLength()
{
return length;
}//End of function
double getBreadth()
{
return breadth;
}//End of function

//Function to accept 4 vertices value
void accept()
{
cout<<" Enter enter the coordinates for bottom left: ";
bottomLeft.accept();
cout<<" Enter enter the coordinates for bottom right: ";
bottomRight.accept();
cout<<" Enter enter the coordinates for top right: ";
topRight.accept();
cout<<" Enter enter the coordinates for top left: ";
topLeft.accept();
//Calculate the length of side 1 using the distance formula
length = pow(pow((bottomRight.getX1() - bottomLeft.getX1()), 2) + pow((bottomRight.getY1() - bottomLeft.getY1()), 2), .5);
//Calculate the length of side 2 using the distance formula
breadth = pow(pow((topRight.getX1() - bottomRight.getX1()), 2) + pow((topRight.getY1() - bottomLeft.getY1()), 2), .5);
}//End of function
};//End of function
//Class Rectangle derived from Quadrilateral
class Rectangle : public Quadrilateral
{
//To store area
double area;
//To store perimeter
double perimeter;
public:
//Calculate and return area
double getArea()
{
area = getLength() * getBreadth();
return area;
}//End of function
//Calculate and return perimeter
double getPerimeter()
{
perimeter = 2*(getLength() + getBreadth());
return perimeter;
}//End of function
};//End of class

//Main function definition
int main()
{
//Creates rectangle class object
Rectangle r;
//Calls the accept method to accept 4 vertices value
r.accept();
//Displays area and perimeter values
cout<<" Area of Rectangle: "<<r.getArea();
cout<<" Perimeter of Rectangle: "<<r.getPerimeter();
}//End of function

Sample Run:


Enter enter the coordinates for bottom left:
Enter x1 coordinate: 10

Enter y1 coordinate: 5

Enter enter the coordinates for bottom right:
Enter x1 coordinate: 10

Enter y1 coordinate: 20

Enter enter the coordinates for top right:
Enter x1 coordinate: 5

Enter y1 coordinate: 20

Enter enter the coordinates for top left:
Enter x1 coordinate: 5

Enter y1 coordinate: 5

Area of Rectangle: 237.171
Perimeter of Rectangle: 61.6228

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote