-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassInheritance.cpp
More file actions
43 lines (35 loc) · 890 Bytes
/
classInheritance.cpp
File metadata and controls
43 lines (35 loc) · 890 Bytes
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
#include <iostream>
//base/super class
class Polygon {
protected:
int width, height;
public:
void setValues(int w, int h) {
width = w;
height = h;
}
};
//derived/child class
//: is different than :: which is used for scope resolution
class Rectangle: public Polygon {
public:
int area() {
return width * height;
}
};
//derived/child class
class Triangle: public Polygon {
public:
int area() {
return width * height / 2;
}
};
int main() {
Rectangle rect; //instantiate object
Triangle trgl; //instantiate object
rect.setValues(4, 5); //initialize object
trgl.setValues(4, 5); //initialize object
std::cout << "Rectangle area: " << rect.area() << std::endl;
std::cout << "Triangle area: " << trgl.area() << std::endl;
return 0;
}