-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.cpp
More file actions
154 lines (126 loc) · 4.44 KB
/
inheritance.cpp
File metadata and controls
154 lines (126 loc) · 4.44 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
author Alessandro Ferrante
* inheritance.cpp
*/
/*
? The example introduces the concept of inheritance,
? which allows you to create new classes based on existing classes.
? A new Square class is defined which inherits the methods and everything
? that is part of the access modify public from the Rectangle class.
? Inheritance is a fundamental concept in object-oriented programming (OOP)
? that allows you to create new classes based on existing classes.
? In OOP, a class can be defined as a template or a "factory"
? for creating objects of a certain type. Inheritance allows you to create
? a new class that inherits all the properties and methods of the base class,
? while adding new properties and methods.
? The base class is also known as the 'parent class' or 'superclass',
? while the new class created through inheritance is known
? as the 'child class' or subclass.
? The 'child class' inherits all the fields and methods of
? the parent class and can also add new fields and methods or
? override existing methods.
? Inheritance is useful because it helps you avoid code duplication
? and create class hierarchies to organize your code in a more logical
? and maintainable way.
*/
#include<iostream>
using namespace std;
// ? parent class or superclass of the square class
class Rectangle {
private:
// attributes
float b, h;
public:
// constructor
Rectangle(float _b, float _h);
// destructor
~Rectangle();
// methods
float compute_area();
float compute_perimeter();
float get_b() { return b; };
float get_h() { return h; };
void get_dimension(float & _b, float & _h){
_b = b;
_h = h;
}
float set_b(float _b) { b = _b; };
float set_h(float _h ){ h = _h; };
void set_dimension(float _b, float _h){
b = _b;
h = _h;
}
void show();
};
// ? child class that inherits from the rectangle class
class Square : public Rectangle{
public:
// ? constructor
Square(float side) ; //: Rectangle (side, side);
// ? method
void set_side(float side);
};
// ? Square constructor
// ? :Rectangle(side, side) is the invocation of the parent-class constructor
Square::Square(float side) : Rectangle(side, side){
//...Square-specific (it is not needed in this case)
}
// ? Square class method
void Square::set_side(float side){
set_dimension(side, side);// ? use inherited method
}
// Rect constructor
Rectangle:: Rectangle(float _b, float _h){
cout << "Rect constructor " << endl;
this->b = _b;
this->h = _h;
}
//Rect destructor
Rectangle::~Rectangle(){
cout << "Rect destructor " << endl;
}
// Rectangle class method
float Rectangle::compute_area(){
return b * h;
}
// Rectangle class method
float Rectangle::compute_perimeter(){
return (b + h) * 2;
}
// function to display the size
void show_dimension(Rectangle * r){
cout << "The size is " << r->get_b() << " x " << r->get_h() << endl;
}
// Rectangle class method
void Rectangle::show(){
show_dimension(this);
}
int main (int argc, char ** argv){
/*
//* Initialize the attributes
Rectangle my_rect_1(3, 10);
Rectangle my_rect_2(20.5, 44);
cout << "Perimeter of rect 1 = " << my_rect_1.compute_perimeter() << endl;
cout << "Area of rect 1 = " << my_rect_1.compute_area() << endl;
cout << "Perimeter of rect 2 = " << my_rect_2.compute_perimeter() << endl;
cout << "Area of rect 2 = " << my_rect_2.compute_area() << endl;
//* Create dynamic object using pointer
Rectangle * my_rect_3;
my_rect_3 = new Rectangle(30, 40);
cout << "Perimeter of rect 3 = " << my_rect_3->compute_perimeter() << endl;
cout << "Area of rect 3 = " << my_rect_3->compute_area() << endl;
//* metodi diversi di visualizzazione delle dimensioni
cout << "The size of rect 3 is " << my_rect_3->get_b() << " x " << my_rect_3->get_h() << endl;
float base, height;
my_rect_3->get_dimension(base, height);
cout << "The size of rect 3 is " << base << " x " << height << endl;
//* deallocation
delete my_rect_3;
my_rect_1.set_dimension(3, 4);
cout << "Perimeter of rect 1 = " << my_rect_1.compute_perimeter() << endl;
*/
// ? Square
Square q(30);
cout << "Area of square is " << q.compute_area() << endl;
cout << "Perimeter of square is " << q.compute_perimeter() << endl;
}