190 likes | 397 Views
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 :
E N D
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 : 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; }; Composition Person Name,age Member Functions Birthday
student sid name ... ... semester dob student(...) .. . void print() void set_semester(int) 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;} };
Date month year day Date(...) void print() 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; }
dob month sem day date(...) void print() Student(...) .. . void print() void set_semester(int) Student sid name sem ... ...
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(); // the data member dob //(which is an object) invokes //its print() member }
The constructor Student calls the constructor of Date by passing three arguments to it. ==> the object dob is created even before the sid , nama, and sem are passed to the student data members An object aStud is created ==> its constructor is called ==> data members are initialised what about the data member (component) dob 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) { // refer to notes Lecture 10 page 2 // copy id in sid, nam in name and sem in semester }
int main() { Student aStud(“f12012”, “Ahmad”, aStud.print(); ... } 2, 5, 12, 1980); Student sid name sem f12012 \0 Ahmad\0 2 dob month sem day 5 12 1980 date(...) void print() Student(...) .. . void print() void set_semester(int)
Friends In Object Orientation (luckily not in our human dimension) • 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 some times the above is in correlation with our daily life!
Friends in C++ • 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
Friend Functions 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(); ... }; // can access private //data of Lim The functions perform_sthg (classless) has access to private data of Chong Only the member functions Yin_f1 has access to private data of Lim
// can access private data of Singh All member functions of Raj (f1, f2, f3) have access to private data of Singh Friend Class class Raj { … public: Raj(); // constructor int Raj_f1(); int Raj_f2(); int Raj_f3(); }; class Singh {… public: Singh(); // constructor friend class Raj; ... };
Friend Function: An Example The Customer Class
A concrete example j 0 1 2 3 4 0 1 2 3 20 2 4 2 4 2 2 12 1 3 1 3 1 1 20 2 4 2 4 2 2 12 1 3 1 3 1 1 2 class vect { private: int array [5]; public: ... } class matrix { private: int array [4][5]; public: ... } equals to i 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
class vector; // forward reference class 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 &); }
vector 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.data[i,j] * v.data[j]; } return answer; } } multiply ( ) got a privilege access to private data [array [4][5] of matrix and array [5] of vector ] j 2 0 1 2 3 4 0 1 2 3 1 20 2 4 2 4 2 j i i 2 12 1 3 1 3 1 equals to 1 20 2 4 2 4 2 2 12 1 3 1 3 1 answer v m