2 bytesc++ course_2014_c7_ operator overloading, friends and references

54
Kinan keshkeh IT Engineering-Damascus University 3 rd year Summer course- 2014 2 bytes team

Upload: kinan-ke

Post on 27-Jul-2015

39 views

Category:

Software


2 download

TRANSCRIPT

Page 1: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Summer course- 2014

2 bytes team

Page 2: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Welcome !

Hi my friends !! ?

Page 3: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friends !

Page 4: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend functions !

Page 5: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend functions

• Declarations : friend returnedtype FuncName(..,…); just in declaration .

• A friend function of a class is not a member function of the class, but it has access to the private members of that class (to both private member variables and private member functions) just as a member function does.

Page 6: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend functions

Page 7: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend functions

Page 8: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend functions

Page 9: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend Classes !

Page 10: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Friend Classes

• in the same way that a function can be a friend of a class.

• If the class F is a friend of the class C, then every member function of the class F is a friend of the class C.

Page 11: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference !

Page 12: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference

• A reference is the name of a storage location.

• You can link more then VariablesName with the same address !!

• robert is another name for bob

Page 13: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference

• A reference is the name of a storage location.

• You can link more then VariablesName with the same address !!

• robert is another name for bob

Page 14: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference

Page 15: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference

Output: 99 42

Page 16: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference

Output: 99 42

Page 17: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

Reference

Output: 99 42

Page 18: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators !

Page 19: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

Overloaded Operators

Like what we took in past lecture + , == , = , - , …..

A little difference in declaration and calling

Page 20: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

overloaded operators as standalone functions

overloading as member functions

Page 21: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

• Overloaded operators as standalone functions :

const ClassName operator +(const ClassName& amount1, const ClassName& amount2);

ClassName m3 = (m1 + m2);

Page 22: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

const ClassName operator +(const ClassName& amount1, const ClassName& amount2);

• (m1 + m2).input( );

• ClassName m3 = (m1 + m2); m3.input( );

False

True

• Overloaded operators as standalone functions :

Page 23: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

const ClassName operator +(const ClassName& amount1, const ClassName& amount2);

• (m1 + m2).input( );

• ClassName m3 = (m1 + m2); m3.input( );

False

True

• Overloaded operators as standalone functions :

Page 24: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

const ClassName operator +(const ClassName& amount1, const ClassName& amount2);

• (m1 + m2).input( );

• ClassName m3 = (m1 + m2); m3.input( );

False

True

• Overloaded operators as standalone functions :

Page 25: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

const ClassName operator +(const ClassName& amount1, const ClassName& amount2);

• & Reference for more efficient( it’s not important)

• Overloaded operators as standalone functions :

Page 26: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

const ClassName operator +(const ClassName& amount1, const ClassName& amount2);

• The same way for ” - ” , “ ++ ”

• Overloaded operators as standalone functions :

Page 27: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

• Overloading as member functions :

const ClassName operator +(const ClassName& amount);

ClassName m3 = (m1 + m2);

From Class members !! (private)

Page 28: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloaded operators as standalone functions :

Page 29: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloaded operators as standalone functions :

Page 30: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloaded operators as standalone functions :

Page 31: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloaded operators as standalone functions :

Page 32: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloaded operators as standalone functions :

Page 33: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

• Overloaded operators as standalone functions :

Page 34: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloaded operators as standalone functions :

Output:

Page 35: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

• Overloading “<<” and “>>” operators:

• Make you use “ cin “ and “ cout” in main() , with Objects !!

• It’s better to define them as friends functions !

Page 36: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

class Class_Name { . . . public: . . . friend istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2); friend ostream& operator <<(ostream& Parameter_3,const Class_Name& Parameter_4);

overloaded operators

• Overloading “<<” and “>>” operators:

istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2) { . . . return Parameter_1 } ostream& operator <<(ostream& Parameter_3, const Class_Name& Parameter_4) { . . . return Parameter_3 }

Page 37: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

class Class_Name { . . . public: . . . friend istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2); friend ostream& operator <<(ostream& Parameter_3,const Class_Name& Parameter_4);

overloaded operators

• Overloading “<<” and “>>” operators:

istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2) { . . . return Parameter_1 } ostream& operator <<(ostream& Parameter_3, const Class_Name& Parameter_4) { . . . return Parameter_3 }

Do the input operations with Parameter_1 “ >>“

Page 38: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

class Class_Name { . . . public: . . . friend istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2); friend ostream& operator <<(ostream& Parameter_3,const Class_Name& Parameter_4);

overloaded operators

• Overloading “<<” and “>>” operators:

istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2) { . . . return Parameter_1 } ostream& operator <<(ostream& Parameter_3, const Class_Name& Parameter_4) { . . . return Parameter_3 }

Then return Parameter_1

Page 39: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

class Class_Name { . . . public: . . . friend istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2); friend ostream& operator <<(ostream& Parameter_3,const Class_Name& Parameter_4);

overloaded operators

• Overloading “<<” and “>>” operators:

istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2) { . . . return Parameter_1 } ostream& operator <<(ostream& Parameter_3, const Class_Name& Parameter_4) { . . . return Parameter_3 }

Do the output operations and put them in Parameter_3“ <<“

Page 40: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

class Class_Name { . . . public: . . . friend istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2); friend ostream& operator <<(ostream& Parameter_3,const Class_Name& Parameter_4);

overloaded operators

• Overloading “<<” and “>>” operators:

istream& operator >>(istream& Parameter_1,Class_Name& Parameter_2) { . . . return Parameter_1 } ostream& operator <<(ostream& Parameter_3, const Class_Name& Parameter_4) { . . . return Parameter_3 }

Then return Parameter_3

Page 41: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “<<” and “>>” operators:

Page 42: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “<<” and “>>” operators:

Page 43: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “<<” and “>>” operators:

Page 44: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

• Overloading “<<” and “>>” operators:

Output:

Page 45: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

• Overloading “++” , “--” and “ [] ” operators:

Page 46: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “++” , “--” operators:

Page 47: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “++” , “--” operators:

Page 48: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “++” , “--” operators:

Page 49: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators

Output:

• Overloading “++” , “--” operators:

Page 50: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “ [] “ operator:

Page 51: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “ [] “ operator:

Page 52: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

overloaded operators • Overloading “ [] “ operator:

Output:

Page 53: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

That’s for today

Enjoy and play !!

Page 54: 2 BytesC++ course_2014_c7_ operator overloading, friends and references

2 bytes team

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team