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.
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;
}
Comments
Leave a comment