0% found this document useful (0 votes)
5 views10 pages

Presentation 1

The document discusses Object-Oriented Programming (OOP) concepts in C++, focusing on container classes and containership. It explains how a container class holds objects of another class, illustrating the 'has-a' relationship, and contrasts it with inheritance, which represents an 'is-a' relationship. The document also provides real-life examples to clarify these concepts and highlights the benefits of using containership for modular design and code reusability.

Uploaded by

dang2342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Presentation 1

The document discusses Object-Oriented Programming (OOP) concepts in C++, focusing on container classes and containership. It explains how a container class holds objects of another class, illustrating the 'has-a' relationship, and contrasts it with inheritance, which represents an 'is-a' relationship. The document also provides real-life examples to clarify these concepts and highlights the benefits of using containership for modular design and code reusability.

Uploaded by

dang2342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OOP’s

with
C++

Container
class
Group 22 Members :-

S.no, Name Regd. No.

1. Raunak Raj 24BCE11152

2. Shalvi Pandey 24BCE11141

3. Kartikey Singh 24BCE11116

4.

5.
Container Class

The class which contains the object of another class is called the
container class.

We can create an object of one class inside another, and that


object can be accessed by the other class. This relationship
between classes is called containership.
Example of Container class :

#include <iostream> class retest{


using namespace std; test x;
public:
class test retest(){
{ x.show();
public: }
void show() };
{
cout << "I am class test"; int main()
} {
}; retest y;
}
Containership
Containership, also known as composition, is an
object-oriented programming concept where
one class contains an object of another class
as a field (or member). This helps to build
complex systems by combining simpler classes.
It’s based on the “has-a” relationship.
Why we use containership
•To reuse code without needing inheritance.

•To follow modular design: breaking a system into


smaller, manageable parts.

•To support flexibility: you can replace or update


components independently.

•Encourages loose coupling, making code easier to


maintain and extend.
Example of Containership

#include <iostream>
using namespace std;
class Engine { int main() {
public:
void start() { Car myCar;
cout << "Engine started." << myCar.startCar();
endl;
} return 0;
}; }

class Car {
private:
Engine engine;
public:
void startCar() {
engine.start();
cout << "Car started." << endl;
}
};
DIFFERENCE BETWEEN INHERITANCE AND
CONTAINERSHIP :
Inheritance Containership
• It enables a class to inherit • It enables a class to contain
data and functions from a objects of different classes
base class as its data member.

• The derived class may • The container class can’t


override the functionality override the functionality of
of the base class. the contained class.

• The derived class may add • The container class can’t add
data or functions to the anything to the contained
base class. class.

• Inheritance represents a • Containership represents a


“is-a” relationship. “has-a” relationship.
REAL LIFE EXAMPLES FOR CONTAINERSHIP
AND INHERITANCE:
Inheritance – (is-a) type
• Let’s imagine a Laptop and an Electronic Device.

• A laptop is a type of electronic device. It has all the features of an electronic


device — like needing electricity, having circuits, and being operated
digitally. So, we say

• ‘A Laptop is an Electronic Device.’

• This is exactly what inheritance is in C++.

• The Laptop class can inherit from a ElectronicDevice base class, and it
will automatically get all its common features like poweron(), shutdown()
functions.
REAL LIFE EXAMPLES FOR CONTAINERSHIP
AND INHERITANCE:
Containership – (has-a) type
• Now, consider the relationship between a Laptop and a Battery

• A Laptop is not a Battery, but it has a Battery.


The laptop uses the battery to work, but the battery is a separate component that you
can even replace.

This shows a has-a relationship, which is what we call containership or composition
in C++.
• In code, the laptop class will contain an object of the battery class and use its methods
like getChargelevel() or supplypower().

• This makes the design more modular and flexible — if the battery class changes, the
laptop class doesn't break.

You might also like