-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor_copy.cpp
More file actions
115 lines (95 loc) · 3.21 KB
/
constructor_copy.cpp
File metadata and controls
115 lines (95 loc) · 3.21 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
/*
author Alessandro Ferrante
* constructor_copy.cpp
*/
/*
? The example illustrates using the copy constructor when you want to create
? a copy of one object in another and the default constructor when creating objects
?without specifying initial values.
? The default constructor is a parameterless constructor useful
? when passing no parameters, uses the constructor with values (0, 0)
? The copy constructor creates a new object by copying the attributes
? of an existing object (r in this case).
*/
#include <iostream>
using namespace std;
class Rectangle{
private:
// attributes
float b, h;
public:
// constructor
Rectangle (float _b, float _h){
b = _b;
h = _h;
}
// copy constructor
Rectangle (const Rectangle & r) : Rectangle(r.b, r.h){}
// default constructor
Rectangle() : Rectangle(0, 0){}
// destrcutor
~Rectangle(){}
//methods
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;
};
float compute_area(){
return b * h;
};
float compute_perimeter(){
return (b + h) * 2;
};
};
/**
// | Rectangle constructor
// ? Initialize member variables b and h with the provided values
Rectangle::Rectangle(float _b, float _h) : b(_b), h(_h){
cout << "Rect constructor" << endl;
}
// | Rectangle copy constructor
// ? : Rectangle(r.b, r.h) -> Invoke the main constructor to copy
Rectangle::Rectangle(const Rectangle & r) : Rectangle(r.b, r.h){
cout << "Copy constructor " << endl;
}
// | Rectangle default constuctor
// ? : Rectangle(0, 0) -> Invoke the main constructor with default values
Rectangle::Rectangle() : Rectangle(0, 0){
cout << "Default constructor " << endl;
}
*/
// child class, inherits from the rectangle class
class Square : public Rectangle{
public:
Square(float side) : Rectangle(side, side){
//...Square specific
}
void set_side(float side){
set_dimension(side, side);
}
};
int main(int argc, char ** argv){
// Initialize the attributes of rect1 and rect2
Rectangle my_rect_1(3, 10);
Rectangle my_rect_2(22.5, 44);
cout << "\nPerimeter 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() << "\n" << endl;
// ? invokes the copy constructor, to copy the values to the new object
Rectangle r2 = my_rect_1;
cout << "Area of r2 = " << r2.compute_area() << endl;
// ? invokes the default constructor, to initiliaze the objects
cout << "\nArray of 3 Rectangle objects" << endl;
Rectangle * r_array = new Rectangle[3];
delete []r_array;
cout << " " << endl;
}