Answers for "Use of static Data Members"

0

Use of static Data Members

#include <iostream>
using namespace std;
 
class A
{
    int x;
public:
    A() { cout << "A's constructor called " << endl;  }
};
 
class B
{
    static A a;
public:
    B() { cout << "B's constructor called " << endl; }
    static A getA() { return a; }
};
 
A B::a;  // definition of a
 
int main()
{
    B b1, b2, b3;
    A a = b1.getA();
 
    return 0;
}
Posted by: Guest on April-27-2022

Browse Popular Code Answers by Language