-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathabs.cpp
51 lines (44 loc) · 1.09 KB
/
abs.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
#include <iostream>
using namespace std;
class Shape {
public:
virtual int Area() = 0; // Pure virtual function is declared as follows.
// Function to set width.
void setWidth(int w) {
width = w;
}
// Function to set height
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// A rectangle is a shape; it inherits shape.
class Rectangle: public Shape {
public:
// The implementation for Area is specific to a rectangle.
int Area() {
return (width * height);
}
};
// A triangle is a shape too; it inherits shape.
class Triangle: public Shape {
public:
// Triangle uses the same Area function but implements it to
// return the area of a triangle.
int Area() {
return (width * height)/2;
}
};
int main() {
Rectangle R;
Triangle T;
R.setWidth(5);
R.setHeight(10);
T.setWidth(20);
T.setHeight(8);
cout << "The area of the rectangle is: " << R.Area() << endl;
cout << "The area of the triangle is: " << T.Area() << endl;
}