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

OOPRelationships

Uploaded by

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

OOPRelationships

Uploaded by

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

Relationships in OOP: Code Examples

1. Association
Sample Program-1
#include <iostream>

#include <string>

using namespace std;

class Bank

private:

string name;

public:

Bank(string n)

name = n;

string getName()

return name;

void setName(string n)

name = n;

};

class Employee

private:
string name;

public:

Employee(string n)

name = n;

string getName()

return name;

void setName(string n)

name = n;

};

class BankEmployeeAssociation

public:

Bank *b;

Employee *e;

};

int main()

Bank b1("HBL");

Employee e1("Ali");

Employee e2("Usman");

BankEmployeeAssociation bea1;

bea1.b = &b1;
bea1.e = &e1;

BankEmployeeAssociation bea2;

bea2.b = &b1;

bea2.e = &e2;

cout << "Bank Name: " << bea1.b->getName() << ",\tEmployee Name: " <<
bea1.e->getName() << endl;

cout << "Bank Name: " << bea2.b->getName() << ",\tEmployee Name: " <<
bea2.e->getName() << endl;

system("pause");

return 0;

2. Aggregation
Sample Program-2
#include <iostream>

#include <string>

using namespace std;

class Address {

public:

string streetAddress;

string city;

Address(string sa, string c)

streetAddress = sa;

city = c;

~Address() {

cout << "Called destructor of Address class" << endl;


}

};

class Employee

private:

Address *address; //Employee HAS-A Address

public:

int employeeId;

string name;

Employee(int id, string n, Address* addr)

employeeId = id;

name = n;

address = addr;

void display()

cout << "Employee ID: " << employeeId << endl;

cout << "Employee Name: " << name << endl;

cout << "Employee Address: " << address->streetAddress << ", " <<
address->city << endl;

~Employee() {

cout << "Called destructor of Employee class" << endl;

};

int main() {

//Address a1 = Address("House # 1024, Street # 40, F-7/2", "Islamabad");

Address a1("House # 1024, Street # 40, F-7/2", "Islamabad");

{
Employee e1 = Employee(1010, "Usman", &a1);

e1.display();

//Employee e2 = Employee(2020, "Ali", &a1);

Employee e2(2020, "Ali", &a1);

e2.display();

system("pause");

return 0;

3. Composition
Sample Program-3
#include <iostream>

#include <string>

using namespace std;

class Room {

public:

string roomName;

Room()

cout << "Default constructor of Room class" << endl;

Room(string rn)

cout << "Parameterized constructor of Room class" << endl;

roomName = rn;

}
~Room() {

cout << "Called destructor of Room class" << endl;

};

class House

private:

Room r; //Employee HAS-A Address

public:

string houseNo;

House()

cout << "Default constructor of House class" << endl;

House(string hn, string rn)

cout << "Parameterized constructor of House class" << endl;

houseNo = hn;

r.roomName = rn;

void display()

cout << "House No: " << houseNo << endl;

cout << "Rooms: " << r.roomName << endl;

}
~House() {

cout << "Called destructor of House class" << endl;

};

int main() {

House h1 = House("40-A", "Living Room");

h1.display();

House h2 = House("1-B", "Sitting Room");

h2.display();

system("pause");

return 0;

You might also like