-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnonymous-class.cpp
More file actions
86 lines (73 loc) · 1.65 KB
/
Anonymous-class.cpp
File metadata and controls
86 lines (73 loc) · 1.65 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
// CPP program to illustrate
// concept of Anonymous Class
// by scope restriction
#include<iostream>
using namespace std;
// Anonymous Class : Class is not having any name
class
{
// data member
int i;
public:
void setData(int i)
{
// this pointer is used to differentiate
// between data member and formal argument.
this->i = i;
}
void print()
{
cout << "Value for i : " << this->i << endl;
}
} obj1; // object for anonymous class
// Anonymous Class : Class is not having any name
class
{
// data member
int i;
public:
void setData(int i)
{
// this pointer is used to differentiate
// between data member and formal argument.
this->i = i;
}
void print()
{
cout << "Value for i : " << this->i << endl;
}
} obj2, obj3; // multiple objects for anonymous class
// Anonymous Class : Class is not having any name
typedef class
{
// data member
int i;
public:
void setData(int i)
{
// this pointer is used to differentiate
// between data member and formal argument.
this->i = i;
}
void print()
{
cout << "Value for i : " << this->i << endl;
}
} myClass; // using typedef give a proper name
// Driver function
int main()
{
// multiple objects
myClass obj4, obj5;
obj1.setData(10);
obj1.print();
obj2.setData(20);
obj2.print();
obj3.setData(30);
obj3.print();
obj4.setData(40);
obj4.print();
obj5.setData(50);
obj5.print();
return 0;
}