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