Answer to Question #228308 in C++ for Arup

Question #228308

Static data members of a class occupy memory once whereas non static data members occupy members as per the number of objects created. Justify the statement by writing a program.


1
Expert's answer
2021-08-22T07:16:12-0400

Non static data members on the other hand occupy members as per the number of objects created.

#include <iostream>
class NonStaticData
{
private:
	int x { 5 };    
	int y { 5 };    
	std::string Petname { "Sample Petname" }; 
public:
	NonStaticData() {
		std::cout << "NonStaticData   {" << x << ", " << y << ", \"" << Petname << "\"}\n";
	}
	~NonStaticData() { std::cout << "NonStaticData::destructor\n"; }
};
int main()
{
	NonStaticData sampleObject;
}

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