Object-Oriented Programming (OOP) Lecture 15: Composition and Aggregation, Slides of Object Oriented Programming

The concepts of composition and aggregation in object-oriented programming (oop) through lecture notes. It covers the use of string and student classes, constructor and destructor implementations, and the differences between composition and aggregation. The document also includes examples of room and chair classes, and friend functions.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 15
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Object-Oriented Programming (OOP) Lecture 15: Composition and Aggregation and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 15

Composition

Conceptual notation:

**String()SetString(char ) : voidGetString() const : const char ~String()…

gpa : floatrollNo : intname : StringStudent(char * = NULL, int = 0,

float = 0.0);

**Student(const Student &)GetName() const : StringGetNamePtr() const : const char SetName(char ) : void~Student()…

Student

**string : char ***

String

Composition

►Output:

Constructor::String..Constructor::Student..Name:

Fakhir

Destructor::Student..Destructor::String..

Composition

Student::Student(char * n,

int

roll, float g){

cout <<"Constructor::

Student..\n";

name.SetString(n);rollNumber = roll;gpa = g; }

Composition

►Add an overloaded constructor to the String

class defined above:

class String{

char

*ptr; public:

String();String(char *);String(const

String

void

SetName(char

~String();…

//String(char * = NULL);

Composition

String::String(char * str){

if(str != NULL){

ptr = new char[strlen(str)+1];strcpy(ptr, str);

} else ptr = NULL;cout << "Overloaded

Constructor::String..\n";

Composition

Student class

continued:^ Student::Student(char * n,int roll,

float g): name(n){

cout << "Constructor::Student..\n";rollNumber = roll;gpa = g;

Composition

Main Function: int main(){

Student aStudent("Fakhir", 899,

cout << endl;cout << “Name:”

<< aStudent.GetNamePtr()<< endl;

return 0;

Composition

Now consider the following case:

String name: char


String()String(char )~String()… …Date()Date(int,int,int)*

**…name : StringbirthDate : DateStudent()Student( char *,const Date &, int, float)SetName(char ) : voidGetName() : char ~Student()…

Student

day: intMonth: intyear: int

Date

Composition

►Student class is modified asfollows:

class

Student{ private: … Date

birthDate; String

name; public: Student(char

const

Date

int,

float);

~Student();…

Composition

►Main function:

int main(){ Date _date(31, 12, 1982);Student aStudent("Fakhir",

_date,899,3.5);

return 0;

Composition

►Output:

Overloaded Constructor::Date..Copy Constructor::Date..Overloaded Constructor::String..Constructor::Student..Destructor::Student..Destructor::String..Destructor::Date..Destructor::Date..

Aggregation

►In aggregation, a pointer orreference to an object is createdinside a class ►The sub-object has a life that is NOT

dependant on the life of its

master class ►e.g:

Chairs can be moved inside or outside atanytime When Room is destroyed, the chairs may or

Aggregation class

Room{ private: float

area; Chair

*^

chairs[50];

Public: Room();void

AddChair(Chair

int

chairNo);

Chair

*^

GetChair(int

chairNo);

bool

FoldChair(int

chairNo);