-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10_area.cpp
72 lines (58 loc) · 2.21 KB
/
10_area.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// // Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called Rectangle and parallelogram from the base shape.Add to the base class, a member function setData() to initialise base class data members and another member function displayArea() to compute and display the area of figures.Make displayArea() as a pure virtual function and redefine this function in the derived classes to suit their requirements.Using these three classes, design a program that will accept dimensions of a square or a parallelogram interactively, and display the area.
// // Header files
#include <iostream>
// // use namespace
using namespace std;
// // define ab abstract class Shape
class Shape
{
protected:
// // instance member variables
double length, breadth;
public:
// // constructor
Shape(int l, int b) : length(l), breadth(b) {}
// // pure virtual function to be overridden by derived classes
virtual void displayArea() const = 0;
};
// // define class Rectangle by inheriting an abstract class Shape
class Rectangle : public Shape
{
public:
// // inheriting the constructor of the base class
using Shape::Shape;
// // override base class function displayArea
void displayArea() const
{
cout << "\nArea of Rectangle => " << length * breadth;
}
};
// // define class Sphere by inheriting an abstract class Volume
class Parallelogram : public Shape
{
public:
// // inheriting the constructor of the base class
using Shape::Shape;
// // override base class function displayArea
void displayArea() const
{
cout << "\nArea of Parallelogram => " << 0.5 * length * breadth;
}
};
int main()
{
double length, breadth;
cout << "\nEnter Dimensions of A Rectangle => ";
cin >> length >> breadth;
// // create an instance of Rectangle
Rectangle r1(length, breadth);
r1.displayArea();
cout << "\n\nEnter Breadth And Height of A Parallelogram => ";
cin >> breadth >> length;
// // create an instance of Sphere
Parallelogram p1(length, breadth);
p1.displayArea();
cout << endl; // Add new line
cin.ignore();
return 0;
}