Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Composition Vs Inheritance

Data Structures using C++ Program

Question: 1. Explain the concept of composition with example.

Composition Vs Inheritance

 

Definition of Inheritance

• Inheritance means deriving a new class from a base class which can make use of some of the properties of base class.

• Inheritance represents the is‒a relationship.


Definition of Composition

• Composition means a class contains the object of another class so that the derived class can use the functionalities of the container class.

• Composition represents the has‒a relationship.


Programming Example of Composition

#inlcude<iostream>

using namespace std;

class A

{

   int i;

   public:

   A(){i=0;}

   void setValue(int value)

   {

      i=value:

   }

   int readValue()

   {

      retum i;

   }

};

class B

{

   int i;

   public:

   A a;//composition because object is embedded

   B(){i=0;}

   void fun(int v)

   {

      i=v;

   }

   int readData()

   {

      return i;

   }

};

void main()

{

   B b;

   b.fun(10);

   b.a.setValue(20);//accessing function of embedded object

   cout<<"\n Value from class B: "<<b.readData();

   cout<<"\n Value from class A via class B: "<<b.a.readValue();

   cout<<"\n Value from class A direct: "<<a.readValue();

}

Output

Value from class B: 10

Value from class A via class B: 20

 

Programming Example of Inheritance

#include<iostream>

using namespace std;

class A

{

   int i;

   public:

   A(){i=0;}

   void setValue(int value)

   {

      i=value;

   }

   int readValue()

   {

      return i;

   }

};

class B: public A

{

   int i;

   public:

   B(){i=0;}

   void fun(int v)

   {

      i=v;

   }

   int readData()

   {

      return i;

   }

};

void main()

{

   B b;

   b.fun(10);

   b.setValue(20);

   cout<<"\n Value from class B: "<<b.readData();

   cout<<"\n Value from class A using derived class B: "<<b.readValue();

}

Output

Value from class B: 10

Value from class A using derived class B: 20

 

• Note that, the level of indirection gets increased in case of composition. That means we can not access the members of class A directly. We have to make use of the object of class A which is embedded within the class B.

• On the other hand, the inheritance allows us to access the members of class A directly using the instance(object) of class B.

 

Review Question

1. Explain the concept of composition with example.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : Data Structures using C++ Program - Composition Vs Inheritance


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation