Answer to Question #294837 in C++ for Zain

Question #294837

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.



1
Expert's answer
2022-02-08T01:16:17-0500
#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";
	}
};




int main()
{
	
	try {
		Circle c(5);	
		c.setdraw(5,1);
		c.getdraw();
		c.setarea();
	} catch(exception* e ) {
		cout<<e->what()<<"\n";
	}








	
	int k;
	cin>>k;








}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog