-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass & destructor.cpp
80 lines (58 loc) · 1.8 KB
/
class & destructor.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
#include <iostream>
const double PI{22/7};
class Cylinder{
public :
// functions -> constructor type 1
Cylinder(){
this->height = 0.000;
this->base_radius= 0.000;
}
Cylinder(double height, double base_radius){
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();
}
//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...!" << std::endl ;
delete volume_pointer;
}
private:
double get_height(){
return height;
}
};
void do_something(){
Cylinder *cylinder = new Cylinder(10.5,20.5);
}
void do_something(Cylinder cylinder){
}
int main(){
do_something();
// pointer mode
Cylinder *cylinder1 = new Cylinder(10.5,20.5);
std::cout << cylinder1->base_radius << std::endl;
std::cout << cylinder1->volume() << std::endl ;
std::cout << (*cylinder1).volume() << std::endl;
std::cout << *cylinder1->volume_pointer<< std::endl;
delete cylinder1;
//normal
Cylinder cylinder2 = Cylinder(10.5,20.5);
std::cout << cylinder2.base_radius << std::endl;
std::cout << cylinder2.volume() << std::endl ;
Cylinder cylinder3 = Cylinder(15.5 ,2.0);
std::cout << cylinder3.get_height_() << std::endl;
do_something(cylinder3);
}