Presentation 1
Presentation 1
with
C++
Container
class
Group 22 Members :-
4.
5.
Container Class
The class which contains the object of another class is called the
container class.
#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 add • The container class can’t add
data or functions to the anything to the contained
base class. class.
• 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
• This makes the design more modular and flexible — if the battery class changes, the
laptop class doesn't break.