Answer to Question #150714 in C++ for muhammad shoaib zar

Question #150714

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



1
Expert's answer
2020-12-13T22:51:56-0500

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;
}

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