Composition, Friends

Post on 07-Jan-2016

26 views 0 download

Tags:

description

Composition, Friends. Lesson # 10. Learn about: i) Object in object: Composition ii) Member initializer iii) Friend functions & classes. Definition : Objects within Objects Ie one or more members of a class is of another class type Example : - PowerPoint PPT Presentation

transcript

1/13

Composition, Friends

Lesson # 10

Learn about:

i) Object in object: Composition

ii) Member initializer

iii) Friend functions & classes

2/13

Composition• Definition : Objects within Objects

• Ie one or more members of a class is of another class type

Example :

class Person could have name, age, and birthday.

the birthday can be put in a class Date could be used to keep the information about the birth date such as day, month and year.

Example : class Date {

private:int month, day,year;

public:

void setdate(int d,int m, int y)

{month = m;day = d;year = y; }

void print()

{cout<<day << << month<< year;}

};

class Person {

public:

Person( char *, int);

void print();

private: char name[25];

int age;

Date birthDate;

};

PersonMemberFunctions

Name,age

Birthday

3/13

Lets redefine our student class as follows

class Student { private:

char sid[20];char name[20];int semester;Date dob ; //Date of Birth

public: Student (char [], char [], int); void print( ) ; void set_semester(int s )

{semester = s;}};

student(...)

void print()

void set_semester(int)

semester

namestudent

.

.

.

...sid

...

dob

4/13

Lets define Date as follows

class Date{ private: int day; int month;

int year;

public: Date(int, int, int);void print ();

}

Date:: Date(int d, int m, int y) { day = d; month = m; year = y;}

void Date:: print (){ cout << day<<”/”<<month<<”/”<<year;}

Date(...)

void print()

yearmonthday

DateDate

5/13

semnameStudentStudent

Student(...)

void print()

void set_semester(int)

..

.

...

sid...

dob

date(...)

void print()

semmonthday

6/13

Student:: Student(char * id, char * nama, int sem) //constructor{// refer to notes Lecture 10 page 2// copy id in sid , nama in name and sem in semester}

void Student:: print(){ cout << “sid:”<<sid<<endl; cout << “name:”<<name<<endl; cout << “sem:”<<semester<<endl;

dob.print();dob.print(); // the data member dob //(which is an object) invokes //its print() member

}

7/13

int main(){Student aStud(“f12012”, “Ahmad”, 2);

}

Lets redefine the student constructor again

Student:: Student(char * id, char * nam, int sem, int d, int m, int y):Date (d, m, y):Date (d, m, y){// refer to notes Lecture 10 page 2// copy id in sid, nam in name and sem in semester}

An object aStud is created ==> its constructor is called==> data members are initialised what about the data member (component) dob

The constructor Student calls the constructor of Dateby passing three arguments to it.==> the object dob is created even before the sid , nama, and sem are passed to the student data members

8/13

int main(){Student aStud(“f12012”, “Ahmad”, aStud.print();...}

2

Student(...)

void print()

void set_semester(int)

semnameStudentStudent

..

.

sidf12012 \0\0

dob

1980date(...)

void print()

semmonth125

day

Ahmad\0\0

2, 5, 12, 1980);

9/13

•If I’m your friend, I would have to know your PRIVATE life

•However, you cannot.

•If I’ m your friend, my friends are NOT your friends.

•If I’ m your friend, your friends are NOT my friend

Friends

In Object Orientation (luckily not in our human dimension)

some times the above is in correlation with our daily life!

10/13

• A friend function is a function that can access private data members of a class, even though the function itself is not a member of the class

• A friend class is a class whose functions can all access private data members of another class

• A friend function can access private data from a class of which it is not a member, but a friend function cannot be a friend on its own

• The friend relationship is always one-sided

Friends in C++

11/13

class Chong{ …public: Chong(); // constructor friend void perform_sthg( );...};…void perform_sthg(){// can access private data of //Chong...}

class Yin{ …public: Yin(); // constructor int Yin_f1();...};class Lim{ public: Lim(); // constructor friend int Yin::Yin_f1();...};

Only the member functions Yin_f1has access to private data of Lim

The functions perform_sthg (classless) has access to private data of Chong

// can access private //data of Lim

Friend Functions

12/13

class Raj{ … public: Raj(); // constructor int Raj_f1(); int Raj_f2(); int Raj_f3();};

class Singh {… public: Singh(); // constructor friend class Raj;...};

All member functions of Raj (f1, f2, f3) have access to private data of Singh

// can access private data of Singh

Friend Class

13/13

Friend Function: An ExampleThe Customer Class

14/13

Using a Friend Function with Two Classes

15/13

Using a Friend Function with Two Classes

16/13

A concrete exampleA concrete example

class matrix{ private: int array [4][5]; public: ...}

class vect{ private: int array [5]; public: ...}

21212

20122012

We need to define a function multiply ( ) that take data from matrix and data from vector and multiply them

multiply ( ) doesn’t belong to neither one. Yet it needs private data from both of them

i2 4 2 4 21 3 1 3 12 4 2 4 21 3 1 3 1

0 1 2 3 40123

j

equals to

17/13

class vector; // forward referenceclass matrix{ private: int data [4][5]; public: … friend vector multiply (const matrix &, const vector &);}class vector{ private: int data [5]; public: … … friend vector multiply (const matrix &, const vector &);}

18/13

vector multiply multiply (const matrix & m, const vector & v){ vector answer (...); for (int i= 0, i <=3 ; i ++) { answer.data [i]=0; for (int j= 0, j <=4 ; j ++) { answer.data [i]+= m.datam.data[i,j] * v.datav.data[j]; } return answer; }}

2 4 2 4 22121

1 3 1 3 12 4 2 4 21 3 1 3 1 2

20122012

0 1 2 3 40123

i

j

m v answer

j i

multiply ( ) got a privilege access to private data [array [4][5] of matrix andarray [5] of vector ]

equals to