12.7k views
4 votes
Create a base class called Shape with the function to get the two double values that could be used to compute the area of figures. Derive three classes called as triangle, rectangle and circle from the base class Shape. Add to the base class a member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine this function in the derived classes to suit their requirements. Using these four classes, design a program that will accept, the dimensions of a triangle and rectangle and the radius of circle, and display the area. The two values given as input will be treated as lengths of two sides in the case of rectangles and as base and height in the case of triangles and used as follows: Area of rectangle = x * y Area of triangle = 1/2 * x * y [In case of circle, get_data() will take only one argument i.e radius so make the second argument as default argument with the value set to zero.The Sales_Employee is a class derived from Fulltime Employee class with a function to fix the basic salary, to assign the sales target, to get the number of units sold from the employee and to calculate the bonus. The bonus calculation is as follows: the basic salary is less than 5000 and the unit sold is greater than 10 then the bonus is 25% of basic pay. If the basic pay is less than 10,000 and the unit sold is greater than 5 then the bonus

User Rifat
by
8.4k points

1 Answer

5 votes

Answer:

To implement the classes and program as described, you could use the following C++ code:

#include <iostream>

#include <cmath>

using namespace std;

class Shape {

protected:

double a;

double b;

public:

virtual void get_data() {

cout << "Enter the value of a: ";

cin >> a;

cout << "Enter the value of b: ";

cin >> b;

}

virtual void display_area() {

cout << "The area is: " << endl;

}

};

class Triangle : public Shape {

public:

void get_data() {

cout << "Enter the base of the triangle: ";

cin >> a;

cout << "Enter the height of the triangle: ";

cin >> b;

}

void display_area() {

cout << "The area of the triangle is: " << 0.5 * a * b << endl;

}

};

class Rectangle : public Shape {

public:

void get_data() {

cout << "Enter the length of the rectangle: ";

cin >> a;

cout << "Enter the width of the rectangle: ";

cin >> b;

}

void display_area() {

cout << "The area of the rectangle is: " << a * b << endl;

}

};

class Circle : public Shape {

public:

void get_data() {

cout << "Enter the radius of the circle: ";

cin >> a;

b = 0;

}

void display_area() {

cout << "The area of the circle is: " << 3.14 * a * a << endl;

}

};

int main() {

Shape *s;

Triangle t;

Rectangle r;

Circle c;

s = &t;

s->get_data();

s->display_area();

s = &r;

s->get_data();

s->display_area();

s = &c;

s->get_data();

s->display_area();

return 0;

}

This code defines the base class Shape with variables a and b to represent the dimensions of the shape. It also defines a virtual function get_data() to get the input for the dimensions, and a virtual function display_area() to compute and display the area of the shape.

The derived classes Triangle, Rectangle, and Circle override the get_data() and display_area()

Step-by-step explanation:

User Yecenia
by
8.2k points

No related questions found