If three objects of a class are defined, how many copies of that class’s data items are stored in memory? How many copies of its member functions? Explain with the help of a valid example by writing code and depict it via diagrams where appropriate
When any object is created, it occupy separate space for each data member of the class because each object may have different-different data requirements, but the member functions of the class are shared by every object of the class in order to save the memory.
No of copy of class data members:2
No of copy of member functions:1
#include <iostream>
using namespace std;
class sqaure
{
public:
int l;
int getPeri();
};
int sqaure :: getPeri()
{
return 4*l;
}
int main()
{
sqaure s1,s2;
s1.l=3;
s2.l=4;
cout<<s1.getPeri()<<endl;
cout<<s2.getPeri()<<endl;
return 0;
}
Comments
Leave a comment