...

/

Object Sizes in Multi level Inheritance

Object Sizes in Multi level Inheritance

Learn about the sizes of objects in multi level inheritance.

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.

Press + to interact
// 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

...