Search⌘ K
AI Features

Object Sizes in Multi level Inheritance

Explore how object sizes are determined in multi level inheritance. Learn that an object's size includes its own data members plus those of its base classes, regardless of member accessibility, using C++ examples to clarify.

We'll cover the following...

Problem

Write a program that throws light on object sizes in multi level inheritance.

Coding solution

Here is a solution to the problem above.

C++
// Multi-level inheritance
#include <iostream>
class Sample
{
private : int a ;
protected : int b ;
public : int c ;
} ;
class NewSample : public Sample
{
private : int i ;
protected : int j ;
public : int k ;
} ;
class FreshSample : public NewSample
{
private : int x ;
protected : int y ;
public : int z ;
} ;
int main( )
{
Sample s1 ;
NewSample s2 ;
FreshSample s3 ;
std :: cout << "Size of s1 = " << sizeof ( s1 ) << std :: endl ;
std :: cout << "Size of s2 = " << sizeof ( s2 ) << std :: endl ;
std :: cout << "Size of s3 = " << sizeof ( s3 ) << std :: endl ;
}

Explanation

...