Answer to Question #150556 in C++ for Maaz Shahzad

Question #150556
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:47:43-0500

we create a class and define three object

There is one non-static data member for each instance (object). So, three, in this case.

There is one member function total, no matter how many instances (objects) you have. So, one, in this case.

Now, if a data member is static (i.e., sometimes referred to as a class member), there is only one static data member total, no matter ho many instances (objects) you have. So, one, in this case. A static data member is shared across all instances (objects) of the class. You can have multiple static data members, but no matter how many instances (objects) you have, only one of each of these static data members exists.

#include <bits/stdc++.h> 
using namespace std; 
class Geeks 
{ 
    public: 
    int id; 
      
    //Default Constructor 
    Geeks() 
    { 
        cout << "Default Constructor called" << endl;  
        id=-1; 
    } 
      
    //Parametrized Constructor 
    Geeks(int x) 
    { 
        cout << "Parametrized Constructor called" << endl; 
        id=x; 
    } 
}; 
int main() { 
      
    // obj1 will call Default Constructor 
    Geeks obj1; 

    cout << "Geek id is: " <<obj1.id << endl;

      
    // obj1 will call Parametrized Constructor 
    Geeks obj2(21); 
    cout << "Geek id is: " <<obj2.id << 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