[go: up one dir, main page]

0% found this document useful (0 votes)
13 views17 pages

4-Edited - Class Relationships - Aggregation, Composition

Uploaded by

l226676
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)
13 views17 pages

4-Edited - Class Relationships - Aggregation, Composition

Uploaded by

l226676
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/ 17

Class/Object Relationships

CS(217)) Object Oriented Programming


Abeeda Akram

11/1/2020 1
Aggregation (has-a)
• Subset of association relation where ownership is involved
• Weak relation
• Object of one class can contain object(s) of other class(s) for specific
amount of time
1. one-to-one,
2. one-to-many
• Unidirectional object of container class knows about its parts
• Objects have independent life time (creation and destruction)

11/1/2020 2
Aggregation (has-a)Examples
a)Examples
• One department has many students. Department Student
• A department has many teachers. Department Teacher
• A University has many departments. University Department
• A vehicle has many tires. Vehicle Tire
• A car has an engine. Car Engine
• A person owns a house. Person House
• A person owns many cars. Person Car

11/1/2020 3
Aggregation (has-a)
a) Implementation
A B class B{
int b;
• Use a pointer to aggregate class public:
object(s). B(int b=0){ this->b=b;}
void main(){ };
A a(1), a2; class A{
B b, b2(3); int a;
a.addB(&b); B * objB; //pointer
a.changeB(&b2); public:
a2.add(&b); A(int a=0){ this->a=a;}
} void addB(B*b){ this->objB = b;}
void removeB(){ objB = nullptr;}
Void changeB(B*b){ objB= b;}
~A(){ objB=nullptr;}
//nothing to do with objB
};

11/1/2020 4
Aggregation (has-a)
a) Implementation
Person House
• One to one
class Person{
• House pointer in person class int pid;
points to aggregate class object. House * hptr; //pointer for house
class House{ public:
int hid; Person(int pid, House * hptr){
public: this->hptr =hptr;
House(int h=0){ this->hid=h;} this->pid = pid;
}; }
void changeHouse(House * h){
void main(){ hptr = h;
t;
House * h = new House(1); }
Person p(1, h); void removeHouse(){ hptr = nullptr;}
p.removeHouse(); ~Person(){
delete h; hptr = nullptr;
} }
};
11/1/2020 5
Aggregation (has-a)
a) Implementation
Department Teacher
• One to many class Department{
int did, noofteachers, current;
class Teacher{
int tid; Teacher ** tList; //pointers list
char * name; public:
public: Department(int id = 0, int noofteachers = 10){
Teacher(int t=0, char*n=nullptr){
tid=t; this->noofteachers= noofteachers;
name = nullptr; tList = new Teacher * [noofteachers];
if(n!=nullptr){ current = 0;
name = new char[strlen(n)+1]; }
strcpy(name, n); void AddTeacher(Teacher * t){ tList[current++] = t; }
}
void RemoveTeacher(int tid);
}
~Teacher(){ ~Department(){
if(name != nullptr) for(int i=0; i< noofteachers;i++}
delete [] name; tList[i] = nullptr;
} delete[] tlist;
}; }
11/1/2020 }; 6
Composition (whole-part)
part)
• Subset of aggregation relation where ownership is involved
• Strong relation
• Object of one class can contain object(s) of other class(s) for lifetime
1. one-to-one,
2. one-to-many
• Unidirectional object of container class knows about its parts
• Objects have dependent life time (creation and destruction)
• When whole destroy part is also destroyed
• Creation and destruction of part is controlled by whole
• Part object can belong only to one whole class

11/1/2020 7
Composition (whole-part
part) Examples
House cannot exist without rooms.
House Room
• Book cannot exist without author(s), ISBN, publisher, pages.
Author
Book Publisher
Page
• Person cannot exist without name, date of birth, ID, address.
Date
Person Name
Address
11/1/2020 8
Composition (whole-part
part) Implementation
A B class B{
int b;
• Add object variable as member of class. public:
void main(){ B(int b=0){ this->b=b;}
A a, a2(3, 4); };
} class A{
• When object of A is created object of B is int a;
B objB; //variable
created inside A too. public:
• When object of A is destroyed part A(int a=0, int b=0):objB(b){
this->a=a;
object B is also destroyed. }
//call parametrized constructor of part
~A(){}
//nothing to do with part destroyed
automatically
};

11/1/2020 9
Composition (whole-part)
part) Implementation
A B class B{
int b;
• Add Pointer to part object as member public:
of class. B(int b=0){ this->b=b;}
void main(){ };
A a, a2(3, 4); class A{
} This page is int a; left blank
intentionally
• When object of A is created object of B * objB; //pointer
B is created inside A too. public:
A(int a=0, int b=0){
• When object of A is destroyed part this->a=a;
object B is also destroyed. objB = new B(b);
//create part with whole
}
~A(){ delete objB;}
//destroy part with whole
};
11/1/2020 10
Composition (whole-part)
part) Example Person

• Single class person controls every


thing • Not scalable
class Person{
int pid; • Error prone
// Name
char * fname; • Not reusable in other class
char * lname;
//Date of Birth
• Redefine all attributes and functions
int day; separately for other classes
int mon;
int year;
• For example student, doctor and
//Address teacher, patient
char * city;
char *country;
int streetNo;
int houseNo;
};

11/1/2020 11
Composition (whole-part)
part) Example
Date
class person{
• Design separate classes
class name{
int pid; Person Name
char * fname; name pname;
char * lname; date dateofBirth; Address
}; address paddress;
class date{ };
int day;
int mon;
• Add objects as variables in class
int year; • Scalable
};
class address • Less Error prone
char * city; • Reusable
eusable in other classes such as
char *country;
int streetNo; student,, doctor and teacher, patient
int houseNo; • No need to redefine all attributes and
}; functions separately for other classes

11/1/2020 12
Composition (whole-part)
part) Example
Date
• Call functions of composed classes Person Name
class person{
int pid; Address
name pname;
date dateofBirth;
address paddress;
public:
person(int pid, char*fn, char*ln, int d, int m, int y, char*city, char*country,
int street, int house)
:pname(fn,ln), dateofBirth(d,m,y), paddress(city,
paddress country, street, house)
{
this->pid=pid;
}
//call parameterized constructors for object separately
};

11/1/2020 13
Composition (whole-part)
part) Example
• Call functions of composed classes Date
class person{
int pid; Person Name
name pname;
date dateofBirth; Address
address paddress;
public:
void setName(char*fn, char*ln){
pname.setname(fn, ln);
}
void setDateofBirth(int d, int m, int y){
y
dateofBirth.setDate(d,m,y);
}
void setAddress(char*city,
char*city, char*country, int street, int house){
paddress.setaddress(city,
, country, street, house);
house
}
//Reuse functions of defined objects in person class
};

11/1/2020 14
Composition (whole-part)
part) Example
• Call functions of composed classes Date
class person{
int pid; Person Name
name pname;
date dateofBirth; Address
address paddress;
public:
char * getfirstName(){
return pname.getfirstname();
}
char * getlastName(){
return pname.getlastname();
}
Date getDateofBirth(int d, int m, int y){
y
return dateofBirth.getDate();
}
//Reuse functions of defined objects in person class
};

11/1/2020 15
Composition (whole-part)
part) Example
• Call functions of composed classes
Date
class person{
int pid;
Person Name
name pname;
date dateofBirth;
Address
address paddress;
public:
friend ostream& operator<< (ostream& , const Person&);
//Reuse functions of defined objects in person class
};
friend ostream& operator<< (ostream& out , const Person& p){
out<< “Person id:” << pid;
out<< “Name:” << pname;
out<< “Date of Birth:” << dateofBirth;
dateofBirth
out<< “Address:” << paddress;
} //Call ostream operator functions of name, date and address class

11/1/2020 16
Composition (whole-part)
part) Example
pname
Date
class person{
int pid; pid dateofBirth Person Name
name pname; paddress
date dateofBirth; Address
address paddress;
};
void main(){ Person p; }
• Calling sequence
• Default constructor: in same order as defined objects in class
1)name 2)date 3)address 4)person
• Destructor: in reverse order as defined objects in class
1)person 2)address 3)date 4)name
• Parametrized constructor: called in order of member initializer
syntax
: dateofBirth(d,m,y), pname(fn,ln),
), paddress(city, country, street, house)
1)date 2)name 3)address 4)person
11/1/2020 17

You might also like