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

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Chapter 13 – Introduction to Classes
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Classes and Objects Presented by: Gunjan Chhabra.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Java and C++, The Difference An introduction Unit - 00.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 Class 19 Chapter 13 – Creating a class definition.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Operator Overloading Introduction
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Functions + Overloading + Scope
Friend functions.
Access Functions and Friend Functions
Review: Two Programming Paradigms
Introduction to Classes
Chapter 5 Classes.
Class: Special Topics Copy Constructors Static members Friends this
This technique is Called “Divide and Conquer”.
Pointers Revisited What is variable address, name, value?
this Pointer Scope Resolution Operator Friend Function
group work #hifiTeam
Static Data Member and Functions
Introduction to Classes
Overloading the << operator
Object Oriented Programming (OOP) Lecture No. 9
Classes and Objects.
References, const and classes
Introduction to Classes and Objects
Class.
Submitted By : Veenu Saini Lecturer (IT)
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
Class: Special Topics Overloading (methods) Copy Constructors
Methods/Functions.
Overloading the << operator
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

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

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

OOP ‘this’ pointer

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;

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

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.

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

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

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

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); };

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

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.

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.