we create a class and define three object
There is one non-static data member for each instance (object). So, three, in this case.
There is one member function total, no matter how many instances (objects) you have. So, one, in this case.
Now, if a data member is static (i.e., sometimes referred to as a class member), there is only one static data member total, no matter ho many instances (objects) you have. So, one, in this case. A static data member is shared across all instances (objects) of the class. You can have multiple static data members, but no matter how many instances (objects) you have, only one of each of these static data members exists.
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
public:
int id;
//Default Constructor
Geeks()
{
cout << "Default Constructor called" << endl;
id=-1;
}
//Parametrized Constructor
Geeks(int x)
{
cout << "Parametrized Constructor called" << endl;
id=x;
}
};
int main() {
// obj1 will call Default Constructor
Geeks obj1;
cout << "Geek id is: " <<obj1.id << endl;
// obj1 will call Parametrized Constructor
Geeks obj2(21);
cout << "Geek id is: " <<obj2.id << endl;
return 0;
}
Comments
Leave a comment