SlideShare una empresa de Scribd logo
1 de 18
Composition, Friends


               Lesson # 10

Learn about:
i) Object in object: Composition
ii) Member initializer
iii) 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

                                                     student
class 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 follows

class 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 created
int main()                          ==> its constructor is called
{                                   ==> data members are initialised
Student aStud(“f12012”, “Ahmad”, 2);what about the data member (component)
                                    dob
}

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
}

                           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
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!


                                                   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 Functions

class Chong                          class Yin
{…                                   {…
public:                              public:
    Chong(); // constructor              Yin(); // constructor
    friend void perform_sthg( );         int Yin_f1(); // can access
...                                  ...                 private //data of Lim
};                                   };
…                                    class Lim
void 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_f1
data of Chong                         has access to private data of Lim
                                                       11/13
Friend Class
class Raj
{…
   public:
      Raj(); // constructor
      int Raj_f1();              // can access private data of Singh
      int Raj_f2();
      int Raj_f3();
};
                                 All member functions of
class 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 example
class 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

Más contenido relacionado

Similar a Lecture10

Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classesAlisha Korpal
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingKulari Lokuge
 
12 2. structure
12 2. structure12 2. structure
12 2. structure웅식 전
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.pptBArulmozhi
 
C++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsC++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsMuhammadAli224595
 
(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_typesNico Ludwig
 
Inheritance 3.pptx
Inheritance 3.pptxInheritance 3.pptx
Inheritance 3.pptxSamanAshraf9
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Unified modeling language
Unified modeling languageUnified modeling language
Unified modeling languageamity2j
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and ClassesSvetlin Nakov
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdfaplolomedicalstoremr
 
F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013Phillip Trelford
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING Gurwinderkaur45
 

Similar a Lecture10 (20)

Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
Structures
StructuresStructures
Structures
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Clang2018 class5
Clang2018 class5Clang2018 class5
Clang2018 class5
 
L1
L1L1
L1
 
12 2. structure
12 2. structure12 2. structure
12 2. structure
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
C++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for StudentsC++ Object Oriented Programming Lecture Slides for Students
C++ Object Oriented Programming Lecture Slides for Students
 
(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types
 
Inheritance 3.pptx
Inheritance 3.pptxInheritance 3.pptx
Inheritance 3.pptx
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Unified modeling language
Unified modeling languageUnified modeling language
Unified modeling language
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
10slide.ppt
10slide.ppt10slide.ppt
10slide.ppt
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
 
F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 

Más de elearning_portal (9)

Lecture05
Lecture05Lecture05
Lecture05
 
Lecture19
Lecture19Lecture19
Lecture19
 
Lecture18
Lecture18Lecture18
Lecture18
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture06
Lecture06Lecture06
Lecture06
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture04
Lecture04Lecture04
Lecture04
 

Último

↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...noor ahmed
 
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Serviceanamikaraghav4
 
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Serviceanamikaraghav4
 
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...aamir
 
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...noor ahmed
 
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...SofiyaSharma5
 
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...noor ahmed
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...ritikasharma
 
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
Call Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls GoaCall Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls Goa
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goasexy call girls service in goa
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...noor ahmed
 
Call Girl Service Belur - 7001035870 with real photos and phone numbers
Call Girl Service Belur - 7001035870 with real photos and phone numbersCall Girl Service Belur - 7001035870 with real photos and phone numbers
Call Girl Service Belur - 7001035870 with real photos and phone numbersanamikaraghav4
 
VIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur Escorts
VIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur EscortsVIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur Escorts
VIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
Call Girl Nashik Amaira 7001305949 Independent Escort Service NashikCall Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
Call Girl Nashik Amaira 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment BookingCall Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Bookingnoor ahmed
 
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...ritikasharma
 
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...noor ahmed
 
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl GoaRussian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goasexy call girls service in goa
 

Último (20)

Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171
Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171
Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171
 
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
 
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
 
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
 
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
 
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
 
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort GoaDesi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
 
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
 
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
Call Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls GoaCall Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls Goa
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Call Girl Service Belur - 7001035870 with real photos and phone numbers
Call Girl Service Belur - 7001035870 with real photos and phone numbersCall Girl Service Belur - 7001035870 with real photos and phone numbers
Call Girl Service Belur - 7001035870 with real photos and phone numbers
 
VIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur Escorts
VIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur EscortsVIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur Escorts
VIP Call Girls Nagpur Megha Call 7001035870 Meet With Nagpur Escorts
 
Call Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
Call Girl Nashik Amaira 7001305949 Independent Escort Service NashikCall Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
Call Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
 
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment BookingCall Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
 
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
 
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl GoaRussian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
 
Call Girls South Avenue Delhi WhatsApp Number 9711199171
Call Girls South Avenue Delhi WhatsApp Number 9711199171Call Girls South Avenue Delhi WhatsApp Number 9711199171
Call Girls South Avenue Delhi WhatsApp Number 9711199171
 

Lecture10

  • 1. Composition, Friends Lesson # 10 Learn about: i) Object in object: Composition ii) Member initializer iii) Friend functions & classes 1/13
  • 2. 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
  • 3. Lets redefine our student class as follows student class 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
  • 4. Lets define Date as follows class 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
  • 5. Student sid name sem ... ... dob day month sem date(...) void print() Student(...) .. . void print() void set_semester(int) 5/13
  • 6. 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
  • 7. An object aStud is created int main() ==> its constructor is called { ==> data members are initialised Student aStud(“f12012”, “Ahmad”, 2);what about the data member (component) dob } 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 } 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
  • 8. 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
  • 9. 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! 9/13
  • 10. 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
  • 11. Friend Functions class Chong class Yin {… {… public: public: Chong(); // constructor Yin(); // constructor friend void perform_sthg( ); int Yin_f1(); // can access ... ... private //data of Lim }; }; … class Lim void 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_f1 data of Chong has access to private data of Lim 11/13
  • 12. Friend Class class Raj {… public: Raj(); // constructor int Raj_f1(); // can access private data of Singh int Raj_f2(); int Raj_f3(); }; All member functions of class Singh Raj (f1, f2, f3) have access to {… private data of Singh public: Singh(); // constructor friend class Raj; ... }; 12/13
  • 13. Friend Function: An Example The Customer Class 13/13
  • 14. Using a Friend Function with Two Classes 14/13
  • 15. Using a Friend Function with Two Classes 15/13
  • 16. A concrete example class 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
  • 17. 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
  • 18. 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