forked from rahulgoyal911/cPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicConstructor.cpp
More file actions
57 lines (57 loc) · 857 Bytes
/
dynamicConstructor.cpp
File metadata and controls
57 lines (57 loc) · 857 Bytes
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
//new and delete
//dynamic constructor
// int *p=new int;
//int *p=new int[10];//means array
//int *p=new int(10);//means value not array
//for 1D array
//int *a=new int[10];
//for 2D array
//int** a = new int*[rowCount];
//for(int i = 0; i < rowCount; ++i)
// a[i] = new int[colCount];
//delete p
//delete [] p//deacllocating memory
using namespace std;
#include<iostream>
class dynamic
{
int *p;
public:
//default
dynamic()
{
p=new int;
*p=5;
//or p=new int(5)
}
//parametrised
dynamic(int x)
{
p=new int;
*p=x;
}
//copy
dynamic(dynamic &ob)
{
p=ob.p;
}
int value()
{
return *p;
}
//in case of pointer we can delete the value
~dynamic()
{
delete p;
}
};
int main()
{
dynamic ob;
dynamic ob1(5);
dynamic ob2(ob1);
cout<<ob.value();
cout<<ob1.value();
cout<<ob2.value();
return 0;
}