OOP - Lecture - 9 - 10 (Composition and Aggerigation)
OOP - Lecture - 9 - 10 (Composition and Aggerigation)
------------------------------------------------------
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;
int main()
{
Employee e1(100, "Ahmad Ali");
Employee e2(101, "Hassan");
Employee e3(102, "Ubaid");
Employee e4(103, "Waqas");
ee.addEmployee(&e1);
ee.addEmployee(&e4);
cs.displayDepartment();
ee.displayDepartment();
//DegreeProgram cs("Bsc. Computer Science", 4, 100000);
//cs.displayDP();
-----------------------------------------------------------------
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;
}
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)
{
}
};