Skip to content
Related Articles

Related Articles

Improve Article

Difference between Containership and Inheritance in C++

  • Last Updated : 26 Jun, 2021

Containership: When an object of one class is created into another class then that object will be a member of that class, this type of relationship between the classes is known as containership or has_a relationship as one class contains the object of another class.

The class which contains the object and members of another class in this kind of relationship is called a container class and the object that is part of another object is called a contained object whereas the object that contains another object as its part or attribute is called a container object.

Syntax for Containership:

C++




// Class that is to be contained
class first {
  
    // Class Definition
};
  
// Container class
class second {
  
    // Creating object of first
    first f;
    .
        .
};

Program 1:



Below is the program to explain the concept of containership in C++:

C++




// C++ program to illustrate the
// concept of containership
#include <iostream>
using namespace std;
  
class first {
public:
    void showf()
    {
        cout << "Hello from first class\n";
    }
};
  
// Container class
class second {
  
    // Create object of the first-class
    first f;
  
public:
    // Define Constructor
    second()
    {
        // Call function of first-class
        f.showf();
    }
};
  
// Driver Code
int main()
{
    // Create object of second class
    second s;
}
Output:
Hello from first class

Explanation: In the second class above, there is an object of class first. This type of inheritance is known as has_a relationship as the class second has an object of class first as its member. From object f, the function of class first is called.

Inheritance: It is the capability of a class to derive properties and characteristics from another class which is known as Base Class. It is one of the most important features of Object-Oriented Programming. The class that inherits properties from another class is called Subclass or Derived Class. The class whose properties are inherited by sub-class is called Base Class or Super Class.

Syntax:

C++




class subclass_name : access_mode base_class_name {
    // body of subclass
};

Explanation: The subclass_name is the name of the subclass, access_mode is the mode in which one wants to inherit this subclass. For Example, public, private, etc. The base_class_name is the name of the base class from which one wants to inherit the subclass.

Note: A derived class doesn’t inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.

Program 2:

Below is the program to explain Inheritance in C++:

C++




// C++ program to demonstrate the
// concept of inheritance
  
#include <bits/stdc++.h>
using namespace std;
  
// Base class
class Parent {
public:
    int id_p;
};
  
// Sub class inheriting from the
// Base Class(Parent)
class Child : public Parent {
public:
    int id_c;
};
  
// Driver Code
int main()
{
  
    Child obj1;
  
    // An object of class child has
    // all data members and member
    // functions of class parent
    obj1.id_c = 7;
    obj1.id_p = 91;
  
    cout << "Child ID is "
         << obj1.id_c << endl;
  
    cout << "Parent ID is "
         << obj1.id_p << endl;
  
    return 0;
}
Output:
Child ID is 7
Parent ID is 91

Explanation: In the above program, the child class is publicly inherited from the parent class so the public data members of the parent class will also be inherited by the child class.

Difference between Inheritance and Containership:

InheritanceContainership
It enables a class to inherit data and functions from a base classIt enables a class to contain objects of different classes as its data member.
The derived class may override the functionality of the base class.The container class can’t override the functionality of the contained class.
The derived class may add data or functions to the base class.The container class can’t add anything to the contained class.
Inheritance represents a “is-a” relationship.Containership represents a “has-a” relationship.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :