0% found this document useful (0 votes)
11 views2 pages

Oop - Differencebetween Aggregation and Compositio

Uploaded by

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

Oop - Differencebetween Aggregation and Compositio

Uploaded by

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

Lab Task: Difference b/w

Aggregation and Composition


AGGREGATION
#include <iostream>
#include <vector>

class Engine {
public:
void start() {
std::cout << "Engine started" << std::endl;
}
};

class Car {
private:
std::vector<Engine*> engines;

public:
void addEngine(Engine* engine) {
engines.push_back(engine);
}

void startCar() {
std::cout << "Starting the car..." << std::endl;
for (Engine* engine : engines) {
engine->start();
}
}
};

int main() {
Engine engine1, engine2;
Car car;
car.addEngine(&engine1);
car.addEngine(&engine2);
car.startCar();
return 0;
}

COMPOSTION
#include <iostream>

class Engine {
public:
void start() {
std::cout << "Engine started" << std::endl;
}
};

class Car {
private:
Engine engine;

public:
void startCar() {
std::cout << "Starting the car..." << std::endl;
engine.start();
}
};

int main() {
Car car;
car.startCar();
return 0;
}

You might also like