-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic.cpp
More file actions
73 lines (68 loc) · 1.8 KB
/
static.cpp
File metadata and controls
73 lines (68 loc) · 1.8 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
#include <iostream>
using namespace std;
class sample
{
private:
int i;
static int count;
public:
sample(int i)
{
this->i = i;
count++;
}
static void staticDisplay()
{
/* Next line would give error because static fns can access only static data.
No non-static member fns can be accessed in static fns
this ptr does not get pass in static fns. */
// cout<<count<<" "<<this->i<<endl;
cout<<"Count: "<<count<<endl;
}
void display()
{
cout<<"Count: "<<count<<" "<<"i: "<<i<<endl;
}
};
class singleton
{
private:
int i;
static singleton *p;
singleton() //CTOR needs to be private so that it doesnt get called outside the class.
{
i = 47;
}
public:
static singleton* create()
{
if (p==NULL) // PTR p is static so that it remains constant across every object and static can acess only static data member.
{
p = new singleton;
}
return p;
}
};
int sample::count = 0;
singleton* singleton::p = NULL;
int main()
{
cout<<"Calling without any instance defined: ";
sample::staticDisplay();
sample s1(1), s2(2);
cout<<"Using object s1 => ";
s1.display();
s1.staticDisplay();
cout<<"Using object s2 => ";
s2.display();
s2.staticDisplay();
cout<<"Using class namespace => ";
sample::staticDisplay();
/* Size of single int. If data member 'i' is removed then sizeof() returns 1 */
cout<<"Size of 'sample' objects: "<<sizeof(s1)<<endl;
// singleton sig1; //Wont work as CTOR is made private.
cout<<"--------Singleton--------"<<endl;
singleton *sigPtr1 = singleton::create();
singleton *sigPtr2 = singleton::create();
cout<<sigPtr1<<" "<<sigPtr2<<endl; //Same address gets printed
}