]Full code to copy please Define the class Shape with two private data members w
ID: 3859705 • Letter: #
Question
]Full code to copy please Define the class Shape with two private data members width and height (both double). The Shape class should have a default constructor as well as constructor with parameters. The class should have get and set methods for its private data members. The Shape class must also have a method called area() that returns an integer. For the implementation of the area() method simply use “cout” to print the “base class” and return 0.
a. Include the exception handling routine to set methods for width and height. The set methods should throw runtime_error exception with the message “ bad input”.
b. Define the class Rectangle derived from shape. Rectangle has no additional private data members. But it overrides the method area() in Shape class by computing and returning the area of the rectangle.
c. Define the class Triangle derived from shape. Triangle has no additional private data members. But it overrides the method area() in Shape class by computing and returning the area of the Triangle. The Triangle object interprets the width as the base of the triangle “b” and height as the height “h” of the triangle. The area is given by b*h/2.
d. Call the set method of the shape class to set its value to a negative number. Catch the exception. No need to allow user to reenter value.
e. Demonstrate polymorphism for the classes shape, rectangle, and Triangle. Note that the class has no print method, just call and print the value returned by the area() method when demonstrating polymorphism.
Explanation / Answer
#include<iostream>
#include <stdexcept> // for exception, runtime_error, out_of_range
using namespace std;
class Shape {
private:
double width;
double length;
public:
Shape() {
width = 0;
length = 0;
}
Shape(double width, double length) {
this->width = width;
this->length = length;
}
double getWidth() {
return width;
}
void setWidth(double width) {
if (width < 0)
throw runtime_error ("bad input");
this->width = width;
}
double getLength() {
return length;
}
void setLength(double length) {
if (length < 0)
throw runtime_error ("bad input");
this->length = length;
}
int area() {
cout << "base class" << endl;
return 0;
}
};
class Rectangle : public Shape {
public:
int area() {
return (int)(getWidth()*getLength());
}
};
class Triangle : public Shape {
public:
int area() {
return (int)(getWidth()*getLength()*0.5);
}
};
int main() {
Shape a;
try {
a.setLength(-5);
}
catch (runtime_error &e) {
cout << "Caught a runtime_error exception: " << e.what () << ' ';
}
Shape *b;
b = new Shape(2, 10);
cout << "Area of Shape is : " << b->area() << endl;
cout << "Area of Rectangle is : " << ((Rectangle*)b)->area() << endl;
cout << "Area of Triangle is : " << ((Triangle*)b)->area() << endl;
}
here you go champ! Like the code?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.