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

This Study Resource Was

The document describes a class diagram with composition and aggregation relationships. The Vehicle class has an Engine as a component. The Driver class has a Vehicle through aggregation. The code implements these classes with the necessary methods and demonstrates their usage.

Uploaded by

Naveed Hassan
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)
53 views2 pages

This Study Resource Was

The document describes a class diagram with composition and aggregation relationships. The Vehicle class has an Engine as a component. The Driver class has a Vehicle through aggregation. The code implements these classes with the necessary methods and demonstrates their usage.

Uploaded by

Naveed Hassan
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

Task 2

Implement the classes according to the given class diagram. It shows both the composition
and aggregation relationships.

Driver has a Vehicle and Engine is a part of that Vehicle.

[Note: consider the necessary data members, constructors, destructors and member functions
accordingly]

m
er as
co
eH w
o.
rs e
ou urc
#include<iostream>
#include<string>
using namespace std;
o

class Vehicle
aC s

{
v i y re

private:
string type;
string company;
string model;
Engine e;
ed d

public:
ar stu

Vehicle(string t, string c, string m) :type(t), company(c), model(m)


{

cout << "Vehicle Created" << endl;


sh is

}
Th

~Vehicle()
{
cout << "Vehicle Destroyed" << endl;
}
void print()
{
cout << "Vehicle type " << type << endl;
cout << "Vehicle Compnay " << company << endl;
cout << "Vehicle Model " << model << endl;
}

};
class Driver
{
private:

This study source was downloaded by 100000822924914 from CourseHero.com on 04-06-2021 15:20:01 GMT -05:00

https://www.coursehero.com/file/64599086/OOP-21docx/
string Type;
string name;
int age;
Vehicle *c;
public:
Driver(Vehicle *ob) :c(ob)
{

cout << "Driver created" << endl;


}
~Driver()
{
cout << "Driver destroyed" << endl;
}
Driver(string t, string n, int a) :Type(t), name(n), age(a)
{
cout << "Driver type " << Type << endl;
cout << "Driver name " << name << endl;
cout << "Driver age " << age << endl;
}
};

m
class Engine

er as
{

co
private:

eH w
int number;
string lifetime;

o.
string company; rs e
public:
Engine()
ou urc
{}
Engine(int n, string l, string c) :number(n), lifetime(l), company(c)
{
o

cout << "Engine created" << endl;


cout << "Engine Number " << number << endl;
aC s

cout << "Engine lifetime " << lifetime << endl;


v i y re

cout << "Engine company " << company << endl;


}
~Engine()
{
cout << "Engine destroyed" << endl;
ed d

}
ar stu

};
int main()
{
Vehicle vobj("Car", "Honda", "2020");
sh is

vobj.print();
Driver *dobj = new Driver(&vobj);
Th

Driver dob("Male", "Hassan", 35);


Vehicle *vob = new Vehicle("Car", "Honda", "2020");
Engine eobj(1234, "2 years", "Honda");
return 0;
}

This study source was downloaded by 100000822924914 from CourseHero.com on 04-06-2021 15:20:01 GMT -05:00

https://www.coursehero.com/file/64599086/OOP-21docx/
Powered by TCPDF (www.tcpdf.org)

You might also like