forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Number_of_object.cpp
More file actions
55 lines (47 loc) · 1002 Bytes
/
Count_Number_of_object.cpp
File metadata and controls
55 lines (47 loc) · 1002 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
//count the number of the objects in this program
#include<iostream>
using namespace std;
class myclass
{
private:
int a=8,b=9;
public:
myclass()
{
a=8;
b=9;
count++;
}
myclass(int x,int y)
{
a=x;
b=y;
count++;
}
static int count;
void display()
{
cout<<"\na= "<<a<<"\nb= "<<b<<endl;
}
static void count1()
{
cout<<"No of objects= "<<count<<endl;
}
};
int myclass::count=0;
int main()
{
cout<<"\nFor object1--->";
myclass s1; //creating object s1 with default constructor
s1.display(); //a=8 b=9
s1.count1(); //count=0;---> count++;---> count=1
cout<<"\nFor object2--->";
myclass s2(20,60); //creating object s2 with parameterized constructor
s2.display(); // a=20 b=60
s2.count1(); //count=1--->count++ ---> count=2
cout<<"\nFor object3--->";
myclass s3; //creating object s3 with default constructor
s3.display(); //a=8 b=9
s3.count1(); //count=2--->count++ ---> count=3
return 0;
}