Create a class named Rectangle. Make 4 2-D points. Create the following function: -
1)Input Function.
2)Area Function. (In this function area will be calculated and returned).
3)IsRectangle (This will return Boolean i.e. if it is a rectangle, it will return true else it will return False)
4)Output Function.
5) Length Function.
6) Height Function.
7) Perimeter Function.
8) Diagonal Function.
#include <iostream>
#include <vector>
using namespace std;
class Rectangle{
private:
vector<int> points2D;
//B________C A: X = points2D[0], Y = points2D[1]
//| | B: X = points2D[2], Y = points2D[3]
//| | C: X = points2D[4], Y = points2D[5]
//| | D: X = points2D[6], Y = points2D[7]
//|________|
//A D
public:
//1)Input Function.
void Input(){
for(int i=1;i<=4;i++){
int x;
int y;
cout<<"Enter X"<<i<<": ";
cin>>x;
points2D.push_back(x);
cout<<"Enter Y"<<i<<": ";
cin>>y;
points2D.push_back(y);
}
}
//2)Area Function. (In this function area will be calculated and returned).
float Area(){
return Length()*Height();
}
//3)IsRectangle (This will return Boolean i.e. if it is a rectangle, it will return true else it will return False)
bool IsRectangle(){
int AB=(pow((float)(points2D[0]-points2D[2]),2)+pow((float)(points2D[1]-points2D[3]),2));
int BC=(pow((float)(points2D[4]-points2D[2]),2)+pow((float)(points2D[5]-points2D[3]),2));
int AD=(pow((float)(points2D[0]-points2D[6]),2)+pow((float)(points2D[1]-points2D[7]),2));
int CD=(pow((float)(points2D[4]-points2D[6]),2)+pow((float)(points2D[5]-points2D[7]),2));
int ACDiagonal=(pow((float)(points2D[0]-points2D[4]),2)+pow((float)(points2D[1]-points2D[5]),2));
return ( ACDiagonal == (AB + BC)) && (ACDiagonal == (AD + CD));
}
//4)Output Function.
void Output(){
if(IsRectangle()){
cout<<"\nIt is a rectangle.\n";
cout<<"Area: "<<Area()<<"\n";
cout<<"Length: "<<Length()<<"\n";
cout<<"Height: "<<Height()<<"\n";
cout<<"Perimeter: "<<Perimeter()<<"\n";
cout<<"Diagonal: "<<Diagonal()<<"\n";
}else{
cout<<"\nIt is not a rectangle.\n";
}
}
//5) Length Function.
float Length(){
return sqrt(pow((float)(points2D[4]-points2D[2]),2)+pow((float)(points2D[5]-points2D[3]),2));
}
//6) Height Function.
float Height(){
return sqrt(pow((float)(points2D[0]-points2D[2]),2)+pow((float)(points2D[1]-points2D[3]),2));
}
//7) Perimeter Function.
float Perimeter(){
return 2*(Length()+Height());
}
//8) Diagonal Function.
float Diagonal(){
return sqrt(pow((float)(points2D[0]-points2D[4]),2)+pow((float)(points2D[1]-points2D[5]),2));
}
};
//The start point of the program
int main (){
Rectangle rectangle;
rectangle.Input();
rectangle.Output();
system("pause");
return 0;
}
Comments
Leave a comment