Write a program that declares two classes. The parent class is called Shape that has two data members num1 and num2 to store two numbers. It also has four members functions.
· The setarea() function set the area and displays the result.
· The getarea() function get the area and displays the result.
· The setdraw() function set the coordinates for draw and displays the result.
· The getdraw() function get the coordinates for draw and displays the result.
The child class is called Circle that overrides all four functions. Each function in the child class checks the value of data members. It calls the corresponding member function in the parent class if the values are greater than 0. Otherwise it displays error message. All the possible exception is handle through exception handling.
#include<iostream>
#include<string>
using namespace std;
//class shape
class Shape
{
protected:
double num1;
double num2;
public:
//The setarea() function set the area and displays the result.
void setarea(){
cout<<"Area: "<<getarea()<<"\n";
}
//The getarea() function get the area and displays the result.
double getarea(){
return this->num1*this->num1;
}
//The setdraw() function set the coordinates for draw and displays the result.
void setdraw(double num1,double num2){
if( num1 < 0 || num2<0) {
throw "The values are less than 0!";
}
this->num1=num1;
this->num2=num2;
}
//The getdraw() function get the coordinates for draw and displays the result.
void getdraw(){
cout<<"X: "<<this->num1<<"\n";
cout<<"Y: "<<this->num2<<"\n";
}
};
//class Circle inheriting class Shape
class Circle: public Shape
{
private:
double radius;
public:
Circle(){}
Circle(double radius){
this->radius=radius;
}
//The setarea() function set the area and displays the result.
void setarea(){
cout<<"The area of a circle: "<<getarea()<<"\n";
}
//The getarea() function get the area and displays the result.
double getarea(){
const double PI = 3.1416;
return PI * radius * radius;
}
//The setdraw() function set the coordinates for draw and displays the result.
void setdraw(double num1,double num2){
if( num1 < 0 || num2<0) {
throw "The values are less than 0!";
}else{
this->num1=num1;
this->num2=num2;
}
}
//The getdraw() function get the coordinates for draw and displays the result.
void getdraw(){
cout<<"Radius: "<<this->radius<<"\n";
cout<<"X: "<<this->num1<<"\n";
cout<<"Y: "<<this->num2<<"\n";
}
};
void main()
{
try {
Circle c(5);
c.setdraw(5,1);
c.getdraw();
c.setarea();
} catch(exception* e ) {
cout<<e->what()<<"\n";
}
int k;
cin>>k;
}
Comments
Leave a comment