Answer to Question #230182 in C++ for Arup

Question #230182

write a program to illustrate difference between static data members and non static data members.


1
Expert's answer
2021-08-30T01:36:04-0400
//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;
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS