• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecture10
 

Lecture10

on

  • 225 views

 

Statistics

Views

Total Views
225
Views on SlideShare
225
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lecture10 Lecture10 Presentation Transcript

    • Composition, Friends Lesson # 10Learn about:i) Object in object: Compositionii) Member initializeriii) Friend functions & classes 1/13
    • Composition Example : class Date {• Definition : Objects within Objects private:int month, day,year; public:• Ie one or more members of a class is of another class type void setdate(int d,int m, int y) {month = m;day = d;year = y; }Example : void print() class Person could have name, age, and birthday. {cout<<day << << month<< year;} }; the birthday can be put in a class class Person { Date could be used to keep the public: information about the birth date Person( char *, int); such as day, month and year. void print(); Name,age private: char name[25]; Member Functions Person int age; Birthday 2/13 birthDate; Date
    • Lets redefine our student class as follows studentclass Student sid name{ private: ... ... char sid[20]; semester char name[20]; int semester; dob Date dob ; //Date of Birth public: . student(...) Student (char [], char [], int); . . void print( ) ; void print() void set_semester(int s ) void set_semester(int) {semester = s;}}; 3/13
    • Lets define Date as followsclass Date Date{ private: int day; int month; day month year int year; Date(...) public: Date(int, int, int); void print (); void print()}Date:: Date(int d, int m, int y){ day = d; month = m; year = y;}void Date:: print (){ cout << day<<”/”<<month<<”/”<<year;} 4/13
    • Student sid name sem ... ... dob day month sem date(...) void print() Student(...) .. . void print() void set_semester(int) 5/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(); // the data member dob //(which is an object) invokes //its print() member} 6/13
    • An object aStud is createdint main() ==> its constructor is called{ ==> data members are initialisedStudent aStud(“f12012”, “Ahmad”, 2);what about the data member (component) dob}Lets redefine the student constructor againStudent:: 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} 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 7/13
    • int main(){Student aStud(“f12012”, “Ahmad”, 2, 5, 12, 1980);aStud.print();... Student sid name} sem f12012 0 Ahmad0 2 dob day month sem 5 12 1980 date(...) void print() Student(...) .. . void print() void set_semester(int) 8/13
    • FriendsIn 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! 9/13
    • 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 10/13
    • Friend Functionsclass Chong class Yin{… {…public: public: Chong(); // constructor Yin(); // constructor friend void perform_sthg( ); int Yin_f1(); // can access... ... private //data of Lim}; };… class Limvoid perform_sthg() { public:{// can access private data of Lim(); // constructor//Chong friend int Yin::Yin_f1();... ...} };The functions perform_sthg (classless) has access to private Only the member functions Yin_f1data of Chong has access to private data of Lim 11/13
    • Friend Classclass Raj{… public: Raj(); // constructor int Raj_f1(); // can access private data of Singh int Raj_f2(); int Raj_f3();}; All member functions ofclass Singh Raj (f1, f2, f3) have access to{… private data of Singh public: Singh(); // constructor friend class Raj;...}; 12/13
    • Friend Function: An Example The Customer Class 13/13
    • Using a Friend Function with Two Classes 14/13
    • Using a Friend Function with Two Classes 15/13
    • A concrete exampleclass matrix class vect{ private: { private: int array [4][5]; int array [5]; public: public: ... ...} } j 0 1 2 3 4 0 2 20 2 4 2 4 2 1 12 1 1 3 1 3 1 equals to i 2 2 20 2 4 2 4 2 1 12 3 1 3 1 3 1 2 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 16/13
    • 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 &);} 17/13
    • vector multiply (const matrix & m, const vector & v){ vector answer (...); for (int i= 0, i <=3 ; i ++) multiply ( ) got a privilege access to { private data [array [4][5] of matrix and answer.data [i]=0; array [5] of vector ] for (int j= 0, j <=4 ; j ++) { answer.data [i]+= m.data[i,j] * v.data[j]; m.data v.data } return answer; }} j 0 1 2 3 4 2 0 2 4 2 4 2 1 20 j 2 12 i 1 2 1 3 1 3 1 1 equals to 20 i 2 4 2 4 2 3 1 3 1 3 1 2 12 m v answer 18/13