Computer Notes - Friend Functions

0

No comments posted yet

Comments

Slide 1

Friend Functions http://ecomputernotes.com

Slide 2

Composition Conceptual notation: String() SetString(char *) : void GetString() const : const char * ~String() … string : char * String http://ecomputernotes.com

Slide 3

Composition Main Function: int main(){ Student aStudent("Fakhir", 899, 3.1); cout << endl; cout << “Name:” << aStudent.GetNamePtr() << endl; return 0; } http://ecomputernotes.com

Slide 4

Composition Output: Constructor::String.. Constructor::Student.. Name: Fakhir Destructor::Student.. Destructor::String.. http://ecomputernotes.com

Slide 5

Composition Student::Student(char * n, int roll, float g){ cout <<"Constructor:: Student..\n"; name.SetString(n); rollNumber = roll; gpa = g; } http://ecomputernotes.com

Slide 6

Composition To assign meaningful values to the object, the function SetString is called explicitly in the constructor This is an overhead Sub-object name in the student class can be initialized using the constructor “Member initialization list” syntax is used http://ecomputernotes.com

Slide 7

Composition Add an overloaded constructor to the String class defined above: class String{ char *ptr; public: String(); String(char *); String(const String &); void SetName(char *); ~String(); … }; //String(char * = NULL); http://ecomputernotes.com

Slide 8

Composition String::String(char * str){ if(str != NULL){ ptr = new char[strlen(str)+1]; strcpy(ptr, str); } else ptr = NULL; cout << "Overloaded Constructor::String..\n"; } http://ecomputernotes.com

Slide 9

Composition Student class is modified as follows: class Student{ private: float gpa; int rollNumber; String name; public: … Student(char *=NULL, int=0, float=0.0); }; http://ecomputernotes.com

Slide 10

Composition Student class continued: Student::Student(char * n,int roll, float g): name(n){ cout << "Constructor::Student..\n"; rollNumber = roll; gpa = g; } http://ecomputernotes.com

Slide 11

Composition Main Function: int main(){ Student aStudent("Fakhir", 899, 3.1); cout << endl; cout << “Name:” << aStudent.GetNamePtr() << endl; return 0; } http://ecomputernotes.com

Slide 12

Composition Output: Overloaded Constructor::String.. Constructor::Student.. Name: Fakhir Destructor::Student.. Destructor::String.. http://ecomputernotes.com

Slide 13

Composition Now consider the following case: String name: char * String() String(char *) ~String() … … Date() Date(int,int,int) Date(const Date &) … Student day: int Month: int year: int Date

Slide 14

Composition Student class is modified as follows: class Student{ private: … Date birthDate; String name; public: Student(char *, const Date &, int, float); ~Student(); … }; http://ecomputernotes.com

Slide 15

Composition Student class continued: Student::Student(char * n, const Date & d, int roll, flaot g): name(n),birthDate(d){ cout << "Constructor::Student..\n"; rollNumber = roll; gpa = g; } Student::~Student(){ cout << "Destructor::Student..\n"; } http://ecomputernotes.com

Slide 16

Composition Main function: int main(){ Date _date(31, 12, 1982); Student aStudent("Fakhir", _date,899,3.5); return 0; } http://ecomputernotes.com

Slide 17

Composition Output: Overloaded Constructor::Date.. Copy Constructor::Date.. Overloaded Constructor::String.. Constructor::Student.. Destructor::Student.. Destructor::String.. Destructor::Date.. Destructor::Date.. http://ecomputernotes.com

Slide 18

Aggregation Composition vs. Aggregation Aggregation is a weak relationship Room(char *, int) ~Room() FoldChair(int) : bool … Chair() DoSomething() : void FoldChair() : bool UnFoldChair() : bool ~Chair() … area : float chairs[50]:Chair * Room … Chair

Slide 19

Aggregation In aggregation, a pointer or reference to an object is created inside a class The sub-object has a life that is NOT dependant on the life of its master class e.g: Chairs can be moved inside or outside at anytime When Room is destroyed, the chairs may or may not be destroyed

Slide 20

Aggregation class Room{ private: float area; Chair * chairs[50]; Public: Room(); void AddChair(Chair *, int chairNo); Chair * GetChair(int chairNo); bool FoldChair(int chairNo); … }; http://ecomputernotes.com

Slide 21

Aggregation Room::Room(){ for(int i = 0; i < 50; i++) chairs[i] = NULL; } void Room::AddChair(Chair * chair1, int chairNo){ if(chairNo >= 0 && chairNo < 50) chairs[chairNo] = chair1; } http://ecomputernotes.com

Slide 22

Aggregation Chair * Room::GetChair(int chairNo){ if(chairNo >= 0 && chairNo < 50) return chairs[chairNo]; else return NULL; } bool Room::FoldChair(int chairNo){ if(chairNo >= 0 && chairNo < 50) return chairs[chairNo]->FoldChair(); else return false; } http://ecomputernotes.com

Slide 23

Aggregation int main(){ Chair ch1; { Room r1; r1.AddChair(&ch1, 1); r1.FoldChair(1); } ch1.UnFoldChair(1); return 0; } http://ecomputernotes.com

Slide 24

Friend Functions Consider the following class: class X{ private: int a, b; public: void MemberFunction(); … } http://ecomputernotes.com

Slide 25

Friend Functions Global function: void DoSomething(X obj){ obj.a = 3; //Error obj.b = 4; //Error } http://ecomputernotes.com

Slide 26

Friend Functions In order to access the member variables of the class, function definition must be made a friend function: class X{ private: int a, b; public: … friend void DoSomething(X obj); } Now the function DoSomething can access data members of class X http://ecomputernotes.com

Slide 27

Friend Functions Prototypes of friend functions appear in the class definition But friend functions are NOT member functions http://ecomputernotes.com

Slide 28

Friend Functions Friend functions can be placed anywhere in the class without any effect Access specifiers don’t affect friend functions or classes class X{ ... private: friend void DoSomething(X); public: friend void DoAnything(X); ... }; http://ecomputernotes.com

Slide 29

Friend Functions While the definition of the friend function is: void DoSomething(X obj){ obj.a = 3; // No Error obj.b = 4; // No Error … } friend keyword is not given in definition

Slide 30

Friend Functions If keyword friend is used in the function definition, it’s a syntax error //Error… friend void DoSomething(X obj){ … } http://ecomputernotes.com

Slide 31

Friend Classes Similarly, one class can also be made friend of another class: class X{ friend class Y; … }; Member functions of class Y can access private data members of class X http://ecomputernotes.com

Slide 32

Friend Classes Example: class X{ friend class Y; private: int x_var1, x_var2; ... }; http://ecomputernotes.com

Slide 33

Friend Classes class Y{ private: int y_var1, y_var2; X objX; public: void setX(){ objX.x_var1 = 1; } }; http://ecomputernotes.com

Slide 34

Friend Classes int main(){ Y objY; objY.setX(); return 0; } http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Friend Functions in Object oriented Programming what is Friend Functions Explain about it in detail .explain it with example

Tags: computer notes friend functions

URL:
More by this User
Most Viewed