Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two or more functions can have the same name but different parameters

Similar presentations


Presentation on theme: "Two or more functions can have the same name but different parameters"— Presentation transcript:

1 Two or more functions can have the same name but different parameters
OOP Function Overloading Two or more functions can have the same name but different parameters Example: int max(int a, int b) { if (a>= b) return a; else return b; } float max(float a, float b) { if (a>= b) return a; else return b; }

2 OOP ‘this ‘ pointer The this pointer holds the memory address of the current object that is using the function The this pointer is automatically supplied when you call a non-static member function of a class

3 OOP ‘this’ pointer

4 OOP ‘this ‘ pointer #include <iostream.h> class Rectangle { public: Rectangle(); ~Rectangle(); void SetLength(int length) this->itsLength = length; } int GetLength() const return this->itsLength; void SetWidth(int width) itsWidth = width;

5 OOP ‘this ‘ pointer int GetWidth() const { return itsWidth; } private: int itsLength; int itsWidth; }; Rectangle::Rectangle() itsWidth = 5; itsLength = 10; Rectangle::~Rectangle() { }

6 OOP ‘this ‘ pointer int main() { Rectangle theRect;
cout << "theRect is " << theRect.GetLength() << " feet long.\n"; cout << "theRect is " << theRect.GetWidth() << " feet wide.\n"; theRect.SetLength(20); theRect.SetWidth(10); cout << "theRect is " << theRect.GetLength()<< " feet long.\n"; cout << "theRect is " << theRect.GetWidth()<< " feet wide.\n"; return 0; } Output: theRect is 10 feet long. theRect is 5 feet wide. theRect is 20 feet long. theRect is 10 feet wide.

7 OOP Who is a friend ? A friend is a one who has access to all your “ PRIVATE” Stuff

8 OOP Friend function C++ friend functions are special functions which can access the private members of a class.

9 How is it different from a normal function?
OOP How is it different from a normal function? A member is “access” through the “object” Ex : sample object; object.getdata( ); Whereas a friend function requires object to be passed by value or by reference as a parameter Ex : sample object; getdata(object) ;

10 Friend Functions Example: class myclass { int a, b; public:
OOP Friend Functions Example: class myclass { int a, b; public: friend int sum(myclass x); void set_val(int i, int j); }; Syntax: class class_name { //class definition public: friend rdt fun_name(formal parameters); };

11 Code Snippet { OOP friend void display(demo) ; };
class demo { int x ; public : demo(int xxx) x = xxx; } friend void display(demo) ; }; void display(demo d1) cout<<dd1.x; int main( ) { demo d(5); display(d); return 0; }

12 OOP Friend function Friend functions have the following properties:
1) Friend of the class can be member of some other class. 2) Friend of one class can be friend of another class or all the classes in one program, such a friend is known as GLOBAL FRIEND. 3) Friend can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object. 4) Friends are non-members hence do not get “this” pointer. 5) Friends, can be friend of more than one class, hence they can be used for message passing between the classes. 6) Friend can be declared anywhere (in public, protected or private section) in the class.

13 Program for Implementation
OOP Program for Implementation Pgm to create a class ACCOUNTS with function read() to input sales and purchase details. Create a Friend function to print total tax to pay. Assume 4% of profit is tax.


Download ppt "Two or more functions can have the same name but different parameters"

Similar presentations


Ads by Google