write a program to illustrate difference between static data members and non static data members.
//Static Member Example
#include <iostream>
using namespace std;
class A
{
public:
A() {cout << "This is A's Constructor\n";}
};
class B
{
static A a;
public:
B() {cout << "This is B's Constructor\n";}
};
int main()
{
B b;
return (0);
}
Non-Static Example
#include <iostream>
using namespace std;
class A
{
int x;
public:
A() { cout << "Th is A's constructor called\n"; }
};
class B
{
static A a;
public:
B() { cout << "This is B's constructor\n"; }
static A getValA() { return a; }
};
A B::a;
int main()
{
B b1, b2, b3;
A a = b1.getValA();
return 0;
}
Comments
Leave a comment