Aggregation, Composition - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Aggregation, Composition, Composition and Aggregation, Pointer to an object, Reference to an object, Friend Functions, Global function, Prototypes of friend functions are points you can learn in this Object Oriented Programming lecture.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 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 Aggregation, Composition - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 15

Conceptual notation:

*String() SetString(char ) : void GetString() const : const char * ~String() …

*gpa : float rollNo : int name : String Student(char * = NULL, int = 0, float = 0.0); Student(const Student &) GetName() const : String GetNamePtr() const : const char * SetName(char ) : void ~Student() …

Student

**string : char ***

String

  • Output:

Constructor::String..

Constructor::Student..

Name: Fakhir

Destructor::Student..

Destructor::String..

Student::Student(char * n,

int roll, float g){

cout <<"Constructor::

Student..\n";

name.SetString(n);

rollNumber = roll;

gpa = g;

}

  • 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);

String::String(char * str){

if(str != NULL){

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

strcpy(ptr, str);

else ptr = NULL;

cout << "Overloaded

Constructor::String..\n";

  • Student class continued:

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

float g): name(n){

cout << "Constructor::Student..\n";

rollNumber = roll;

gpa = g;

Main Function:

int main(){

Student aStudent("Fakhir", 899,

cout << endl;

cout << “Name:”

<< aStudent.GetNamePtr()

<< endl;

return 0;

Now consider the following case:

*String name: char * String() String(char ) ~String() …

… Date() Date(int,int,int) Date(const Date &) …

**Student() Student( char , const Date &, int, float) SetName(char ) : void GetName() : char * ~Student() …

… name : String birthDate : Date

Student

day: int Month: int year: int

Date

  • Student class is modified as

follows:

class Student{ private: … Date birthDate; String name; public: Student(char *, const Date &, int, float); ~Student(); … };

  • Main function:

int main(){

Date _date(31, 12, 1982);

Student aStudent("Fakhir",

_date,899,3.5);

return 0;

}

  • 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 or reference

to an object is created inside 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 at

anytime

  • When Room is destroyed, the chairs may or

may not be destroyed

Aggregation

class Room{

private:

float area; Chair * chairs[50];

Public:

Room();

void AddChair(Chair *, int chairNo);

Chair * GetChair(int chairNo); bool FoldChair(int chairNo);

};