Skip to content

Commit d31a1bc

Browse files
committed
-> practice on inheritance.
1 parent 082a814 commit d31a1bc

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

practice14.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Shape
6+
{
7+
private:
8+
int getWidth();
9+
int getHeight();
10+
11+
protected:
12+
int width;
13+
int height;
14+
15+
public:
16+
Shape() = default;
17+
Shape(int, int);
18+
};
19+
20+
int Shape::getWidth() { return width; }
21+
int Shape::getHeight() { return height; }
22+
Shape::Shape(int w, int h)
23+
{
24+
width = w;
25+
height = h;
26+
}
27+
28+
class Rectangle : public Shape
29+
{
30+
public:
31+
Rectangle(int w, int h);
32+
int area();
33+
};
34+
35+
// this is the way to initialize parent's contructor
36+
Rectangle::Rectangle(int w, int h) : Shape(w, h) {}
37+
38+
int Rectangle::area() { return width * height; }
39+
40+
int main()
41+
{
42+
Rectangle r1(11, 11);
43+
cout << "Area of rectange: " << r1.area() << endl;
44+
return 1;
45+
}

0 commit comments

Comments
 (0)