-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass & this pointer.cpp
97 lines (76 loc) · 2.65 KB
/
class & this pointer.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
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
#include <iostream>
const double PI{22/7};
class Cylinder{
public :
// functions -> constructor type 1
Cylinder(){
std::cout << "Cylinder Constructor Called...! @ " << this << std::endl ;
this->height = 0.000;
this->base_radius= 0.000;
}
Cylinder(double height, double base_radius){
std::cout << "Cylinder Constructor Called...! @ " << this << std::endl ;
this->height = height;
this->base_radius=base_radius;
}
// functions -> method
double volume(){
*volume_pointer = PI*base_radius*base_radius*height;
return PI*base_radius*base_radius*height;
}
double get_height_(){
return get_height();
}
Cylinder* set_height(double height){
this->height=height;
return this; // for use incgabged calls
}
Cylinder* set_base_radius(double base_radius){
this->base_radius=base_radius;
return this;
}
Cylinder* set_volume(double volume){
*(this->volume_pointer) = volume;
return this;
}
Cylinder& set_height2(double height){
this->height=height;
return *this; // for use incgabged calls
}
Cylinder& set_base_radius2(double base_radius){
base_radius = base_radius;// this do nothing use 56 line mode
this->base_radius=base_radius;
return *this;
}
Cylinder& set_volume2(double volume){
*(this->volume_pointer) = volume;
return *this;
}
//member variables
double base_radius = 0.0;
double height = 0.0;
double *volume_pointer = new double;
//destructor
//release heap memory ;
~Cylinder(){
std::cout << "Cylinder Destructor Called...! @ " << this << std::endl ;
//delete volume_pointer;
}
private:
double get_height(){
return height;
}
};
int main(){
Cylinder cylinder = Cylinder(15.5 ,2.0);
std::cout << cylinder.get_height_() << std::endl;
//chainde calls
cylinder.set_height(10.231)->set_base_radius(32.11)->set_volume(4535);
cylinder.set_height2(2131.2).set_base_radius2(213.1).set_volume2(213);
std::cout << cylinder.height << std::endl ;
Cylinder *cylinder2 = new Cylinder(15.5 ,2.0);
std::cout << cylinder2->get_height_() << std::endl;
cylinder2->~Cylinder();
//chainde calls
cylinder2->set_height(10.231)->set_base_radius(32.11)->set_volume(4535);
}