Hello! This is C++. Q3. Write a program Define a Super class named Point contain
ID: 3717891 • Letter: H
Question
Hello! This is C++.
Q3. Write a program
Define a Super class named Point containing:
An instance variable named x of type int.
An instance variable named y of type int.
Declare a method named toString() Returns a string representation of the point.
Constructor that accepts values of all data members as arguments.
Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point. Each Circle object should have the following public methods:
Circle(x, y, radius)
Constructs a new circle with a center specified by the given coordinators x,y and with the given double radius.
getArea()
Returns the area occupied by the circle, using the formula ?r2.
getCircumference()
Returns the circle's circumference (distance around the circle), using the formula 2?r.
toString()
Returns a string representation of the circle, such as "Circle[center=(75, 20); radius=30]".
Write an application class where you construct objects from subclass and print the result on the screen.
Q3. Write a program
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
double pi=3.14159265358979323846;
class Point
{
public:
int x,y;
Point(int a,int b)
{
x=a;
y=b;
}
string toString()
{
string temp="Point[x=" + to_string(x)+",y="+to_string(y)+"]";
return temp;
}
};
class Circle: public Point
{
public:
double radius;
Circle(int a, int b, double Radius) : Point(a,b)
{
radius=Radius;
}
double getArea()
{
return pi*radius*radius;
}
double getPerimeter()
{
return 2*pi*radius;
}
string toString()
{
string temp="Circle[center=(" + to_string(x)+","+to_string(y)+"); radius="+to_string(radius)+"]";
return temp;
}
};
int main(void)
{
Point p(3,4);
cout<<p.toString()<<endl;
Circle c(3,4,5);
cout<<c.getArea()<<endl;
cout<<c.getPerimeter()<<endl;
cout<<c.toString()<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.