forked from thakursaurabh1998/cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ques1.cpp
72 lines (65 loc) · 1.27 KB
/
ques1.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 shape and store two double type values used to compute area of figures.
// Derive two classes triangle and rectangle from the base shape.
// Use get_data() method in base class to initialize the data members and display area to compute and display the area of figures.
// Make this function virtual and redefine it in derived classes to display their area.
#include <iostream>
using namespace std;
class Shape
{
protected:
double length, breadth, area;
public:
// Shape(double l, double b)
// {
// length = l;
// breadth = b;
// }
void get_data()
{
cout << "Enter 2 sides: " << endl;
cin >> length >> breadth;
// length = l;
// breadth = b;
}
void virtual display()
{
area = length * breadth;
cout << area << endl;
}
// ~Shape();
};
class Rectangle : public Shape
{
public:
// Rectangle();
void display()
{
area = length * breadth;
cout << "Area of rectangle: " << area << endl;
}
// ~Rectangle();
};
class Triangle : public Shape
{
public:
// Square();
void display()
{
area = length * breadth / 2.0;
cout << "Area of triangle: " << area << endl;
}
// ~Square();
};
int main()
{
Shape *ptr;
Rectangle r;
ptr = &r;
ptr->get_data();
ptr->display();
Triangle t;
ptr = &t;
ptr->get_data();
ptr->display();
return 0;
}