0% found this document useful (0 votes)
9 views6 pages

OOP - Lecture - 9 - 10 (Composition and Aggerigation)

The document covers Object Oriented Programming concepts, specifically focusing on Association, Composition, and Aggregation. It explains the relationships between classes, their cardinalities, and provides examples of each type, along with code implementations for a DegreeProgram, Employee, Department, Course, and Student classes. Additionally, it illustrates the constructor and destructor invocation order in Composition and the nature of relationships in Aggregation.

Uploaded by

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

OOP - Lecture - 9 - 10 (Composition and Aggerigation)

The document covers Object Oriented Programming concepts, specifically focusing on Association, Composition, and Aggregation. It explains the relationships between classes, their cardinalities, and provides examples of each type, along with code implementations for a DegreeProgram, Employee, Department, Course, and Student classes. Additionally, it illustrates the constructor and destructor invocation order in Composition and the nature of relationships in Aggregation.

Uploaded by

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

Object Oriented Programming (OOP)

------------------------------------------------------
Lecture Topic: Association, Composition and Aggeriation (Week 10)
------------------------------------------------------
Instructor: Muhammad Aizaz Akmal
------------------------------------------------------
Lecture Oultine
0:Recape of previous Lecture
1:Association
It is the relationship between more than one entities(classes) so that
they can communicate with each other.
Type of Association based cardinality
-one to one
{Department, Program}
1 1
<----------------
-one to many
{Department, Program}
1 m/*
-many to one
{Course , Student}
m/* 1
-many to many
{Course , Student}
1 m
m 1
m n
Class of Association
1: Composition
2: Aggerigation
2:Composition
- "Has a relation" or "consist of" strong relation
- child class has no existance without parent class
- scope of child class is dependent on parent class
i.e
House , Room
Univeristy, Departement
Human, Brain, Heart
- Graphically Representation
-Filled Dimond Shape is used for composition
- Constructor Evaluation Order
the constructor of dependent class (child class) inovke first then
parent class constructor inovke.
- Destructor Evaluation Order
the destructor of independent class (parent class) inovke first then
child class destructor inovke.
3:Aggerigaion
- "Has a relation " weak relation
- child class can exists without parent class
i.e.
Car, Driver
Car, Engine
Department, Employee
University, Student
- Graphically Representation
-Empty Dimond Shape is used for aggerigation

DegreeProgam---------<->Department-----------<->University
-------------------------------------------------------------
Code
-------------------------------------------------------------
#include<iostream>
#include<string>
using namespace std;

class DegreeProgram{
private:
string title;
float duration;
int fee;
public:
DegreeProgram(string ttitle = "None", float tdur = 0, int tfee =
0) :title(ttitle), duration(tdur), fee(tfee)
{
cout << "Deg Pro Constructor invoked..." << endl;
}
void setProgram(string ttitle, float tdur, int tfee)
{
title = ttitle;
duration = tdur;
fee = tfee;
}
void displayDP()
{
cout << "Program Title: " << title << "\nDuration: " << duration << "
Years \nFee: " << fee << endl;
}
~DegreeProgram()
{
cout << "Deg Pro Destructor invoked..." << endl;
}
};
class Employee{
private:
int ecode;
string ename;
public:
Employee(int code = 0, string name = "None") : ename(name), ecode(code)
{

}
void setEmployee(int code, string name)
{
ecode = code;
ename = name;
}

void display()
{
cout << "Emp Name: " << ename << "\nEmp Code: " << ecode << endl;
}
};
class Department
{
private:
string depName;
string hodName;
Employee *emp[5];

int ecount;

DegreeProgram dp;

public:
Department(string tdn = "None", string thod = "None", string ttitle = "None",
float tdur = 0, int tfee = 0) :dp(ttitle, tdur, tfee), depName(tdn), hodName(thod)
{

ecount = 0;
cout << "Department Constructor invoked..." << endl;

}
void setDepartement(string tdn, string thod, string ttitle, float tdur, int
tfee)
{
depName = tdn;
hodName = thod;

dp.setProgram(ttitle, tdur, tfee);


}
void addEmployee(Employee *temp)
{
if (ecount < 5)
{
emp[ecount] = temp;
ecount++;
}
else
cout << "NO more employee can be add" << endl;
}
void setDepartement(string tdn, string thod, const DegreeProgram &tobj)
{
depName = tdn;
hodName = thod;
dp=tobj;
}
void displayDepartment()
{
cout << "Department Name: " << depName << "\nChairperson: " << hodName
<< endl;
dp.displayDP();
for (int i = 0; i < ecount; i++)
{
emp[i]->display();
}
}
~Department()
{
cout << "Department Destructor invoked..." << endl;
}
};

int main()
{
Employee e1(100, "Ahmad Ali");
Employee e2(101, "Hassan");
Employee e3(102, "Ubaid");
Employee e4(103, "Waqas");

Department cs("CS", "Mr. A", "MSCS", 2, 500000);


Department ee("EE", "Mr. B", "BSEE", 4, 1000000);
cs.addEmployee(&e3);
cs.addEmployee(&e2);

ee.addEmployee(&e1);
ee.addEmployee(&e4);

cs.displayDepartment();
ee.displayDepartment();
//DegreeProgram cs("Bsc. Computer Science", 4, 100000);

//cs.displayDP();

/*Department d1("CS", "Mr. A", "BSc. CS",4,100000);


d1.displayDepartment();
cout << "----------------------" << endl;*/

/*Department d2("EE", "Mr. B");


d2.displayDepartment();
*/
/*Department d3;
d3.setDepartement("CS", "Mr.C", cs);
d3.displayDepartment();*/
system("pause");
return 0;
}

-----------------------------------------------------------------
Sample Code For Many to Many Relation
-----------------------------------------------------------------
class Course{
private:
string courseName;
string courseCode;
int stulist[10];
int scount;

public:
Course(string cname = "None", string ccode = 0) : courseName(cname),
courseCode(ccode), scount(0)
{

}
void setCourse(string cname, string ccode)
{
courseName = cname;
courseCode = ccode;
}

void displayCourseInfo() const


{

cout << "Course Name: " << courseName << "\nCourse Code:" << courseCode
<< endl << endl;
// also display student enrolled in it.
}

};

class Student{
private:
string stuName;
int stuId;
int myCourse[3];
int cindex;
public:
Student(string sname = "None", int sid = 0) :stuName(sname), stuId(sid),
cindex(0)
{

}
void setStudent(string sname, int sid)
{
stuName = sname;
stuId = sid;
}
void addCourse(Course *tcourse)
{
if (cindex < 3)
{
myCourse[cindex] = tcourse;
cindex++;
}
else
{
cout << "You can not take more course" << endl;
}
}
void displayStudentInfo()
{
cout << "<------- Student Information ------->" << endl;
cout << "Student Name: " << stuName << "\nID:" << stuId << endl;
displayCourseList();

}
void displayCourseList()
{
cout << "<------List of Course Enrolled------>" << endl;
for (int i = 0; i < cindex; i++)
{
myCourse[i]->displayCourseInfo();
}
}
};
class Resolve
{
private:
Student stulist[10];
Course courlist[6];
int scount;
int ccount;
public:
Resolve() :scount(0), ccount(0)
{}
void addStudent(string sname, int sid)
{
//check stu limit
stulist[scount].setStudent(sname, sid);
scount++;
}
void addCourse(string cname, string ccode)
{
//check course limit
courlist[ccount].setCourse(cname, ccode);
ccount++;
}
void registerCourse(int sid)
{
int si;
for (int i = 0; i < scount; i++)
{
if (stulist[i].getStudentID() == sid)
{
break;
}
// when student found
for (int j = 0; j < ccount; j++)
{
courlist[j].displayCourseInfo();
//do you want to register this course
stulist[i].setCourse(courlist[j].getCourseCode());
courlist[j].setstulist(stulist[i].getId());
}
}

}
void myRegisterCourse(int sid)
{

}
void myResigterStudent(int cid)
{

}
};

You might also like