Write a program that creates a class hierarchy for simple geometry. IN C++ 1. St
ID: 3780504 • Letter: W
Question
Write a program that creates a class hierarchy for simple geometry. IN C++
1. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation).
2. Create a pure abstract base class Shape, which will form the basis of your shapes. The Shape class will contain abstract functions to calculate area and circumference of the shape, plus provide the coordinates (Points) of a rectangle that encloses the shape (a bounding box). These will be overloaded by the derived classes. Create a display() function that will display the name of the class, and all stored information about the class (including area, circumference, and bounding box).
3. Build the hierarchy by creating the Shape classes Circle, Square, and Triangle. For these derived classes, create default constructors, and constructors whose arguments can initialize the shapes appropriately using the correct number of Point objects (i.e., Circle requires a Point center and a radius, Square requires four Point vertices, while Triangle requires three Point vertices).
4. In main(), create one instance each of the following: Circle (10, -5) with a radius of 23; Square (5, -5)(-10,7)(4,23)(-6,12); and Triangle(0,0)(10,10)(-15,15). Display the information from each object.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
class Point
{
int x,y;
public:
Point()
{
x=0;
y=0;
}
Point(int a,int b)
{
x=a;
y=b;
}
Point operator+(Point& obj)
{
Point t;
t.x=x+obj.x;
t.y=y+obj.y;
return t;
}
Point operator-(Point& obj)
{
Point t;
t.x=x-obj.x;
t.y=y-obj.y;
return t;
}
friend ostream& operator<<(ostream& o,Point& p)
{
o<<"X coordinate :"<<p.x<<endl;
o<<"Y coordinate :"<<p.y<<endl;
return o;
}
};
int main(int argc, char const *argv[])
{
Point p1(3,2);
Point p2(2,1);
cout<<"Point P1 "<<p1<<endl;
cout<<"Point P2 "<<p2<<endl;
Point p3=p1+p2;
cout<<"Addition is P3 "<<p3<<endl;
Point p4=p1-p2;
cout<<"Substraction is P4 "<<p4<<endl;
return 0;
}
==============================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ hier.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Point P1
X coordinate :3
Y coordinate :2
Point P2
X coordinate :2
Y coordinate :1
Addition is P3
X coordinate :5
Y coordinate :3
Substraction is P4
X coordinate :1
Y coordinate :1
===============================================================
#include<bits/stdc++.h>
using namespace std;
class Point
{
public:
int x,y;
Point()
{
x=0;
y=0;
}
Point(int a,int b)
{
x=a;
y=b;
}
Point operator+(Point& obj)
{
Point t;
t.x=x+obj.x;
t.y=y+obj.y;
return t;
}
Point operator-(Point& obj)
{
Point t;
t.x=x-obj.x;
t.y=y-obj.y;
return t;
}
friend ostream& operator<<(ostream& o,Point& p)
{
o<<"X coordinate :"<<p.x<<endl;
o<<"Y coordinate :"<<p.y<<endl;
return o;
}
};
class Shape
{
public:
void area();
void circumference();
void display();
};
class Circle:public Shape
{
Point p;
int radius;
public:
Circle(Point& p1,int r)
{
p=p1;
radius=r;
}
void area()
{
cout<<"Area of circle is "<<3.14*radius*radius<<endl;
}
void circumference()
{
cout<<"circumference of circle is "<<3.14*2*radius<<endl;
}
void display()
{
cout<<"Area of circle is "<<3.14*radius*radius<<endl;
cout<<"circumference of circle is "<<3.14*2*radius<<endl;
}
};
class Square:public Shape
{
Point p1,p2,p3,p4;
public:
Square(Point& p1,Point& p2,Point& p3,Point& p4)
{
this->p1=p1;
this->p2=p2;
this->p3=p3;
this->p4=p4;
}
void area()
{
int side=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
cout<<"Area of Square is "<<side*side<<endl;
}
void circumference()
{
int side=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
cout<<"circumference of Square is "<<4*side<<endl;
}
void display()
{
int side=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
cout<<"Area of Square is "<<side*side<<endl;
cout<<"circumference of Square is "<<4*side<<endl;
}
};
class Triangle:public Shape
{
Point p1,p2,p3;
public:
Triangle(Point& a,Point& b,Point& c)
{
p1=a;
p2=b;
p3=c;
}
void circumference()
{
int s1=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
int s2=sqrt( (p1.x-p3.x)*(p1.x-p3.x) + (p1.y-p3.y)*(p1.y-p3.y) );
int s3=sqrt( (p3.x-p2.x)*(p3.x-p2.x) + (p3.y-p2.y)*(p3.y-p2.y) );
cout<<"circumference of Triangle is "<<s1+s2+s3<<endl;
}
void area()
{
int area=abs(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y))/2;
cout<<"Area of Triangle is "<<area<<endl;
}
void display()
{
int s1=sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
int s2=sqrt( (p1.x-p3.x)*(p1.x-p3.x) + (p1.y-p3.y)*(p1.y-p3.y) );
int s3=sqrt( (p3.x-p2.x)*(p3.x-p2.x) + (p3.y-p2.y)*(p3.y-p2.y) );
cout<<"circumference of Triangle is "<<s1+s2+s3<<endl;
int area=abs(p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y))/2;
cout<<"Area of Triangle is "<<area<<endl;
}
};
int main(int argc, char const *argv[])
{
Point p1(10,-5);
Circle c(p1,23);
c.area();
c.circumference();
cout<<"========================================= ";
Point p2(5,-5);
Point p3(-10,7);
Point p4(4,23);
Point p5(-6,12);
Square s(p2,p3,p4,p5);
s.area();
s.circumference();
cout<<"========================================= ";
Point p6(0,0);
Point p7(10,10);
Point p8(-15,15);
Triangle t(p6,p7,p8);
t.area();
t.circumference();
return 0;
}
=================================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ hier.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Area of circle is 1661.06
circumference of circle is 144.44
=========================================
Area of Square is 361
circumference of Square is 76
=========================================
Area of Triangle is 150
circumference of Triangle is 60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.