Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Introduction to Programming Lecture 31. Operator Overloading.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Operator Overloading Fundamentals
1 CS 201 Introduction to c++ (2) Debzani Deb. 2 Classes (1) A class definition – in a header file :.h file A class implementation – in a.cc,.cpp file.
Chapter 14: Overloading and Templates
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Operator Overloading and Type Conversions
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Working With Objects Tonga Institute of Higher Education.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Overloading a constructor If a constructor is overloaded, then an if statement in the client is needed when creating the object. We have always declared.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Classes - Intermediate
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
1 CSC241: Object Oriented Programming Lecture No 08.
Learners Support Publications Constructors and Destructors.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
Constructors and Destructors
Chapter VII: Arrays.
Operator Overloading Ritika Sharma.
Objectives Define polymorphism Overload functions
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Operator Overloading BCA Sem III K.I.R.A.S.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
More on Classes and Objects
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Java Programming Language
Andy Wang Object Oriented Programming in C++ COP 3330
Names of variables, functions, classes
Chapter 9 Introduction To Classes
Templates CMSC 202, Version 4/02.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Text Overloading Techniques –Using CPP

2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading 4.Operator Overloading -Unary Operator Overloading -Binary Operator Overloading Q & A

3 Overloading What is Overloading? –Providing additional behaviour in terms of Logic for the existing behaviour –Ex:Lets Assume there is CalculateSum() behaviour exists for 2 No's –Why cant you make the Same function(Behaviour) to work on the three Nos,Four Numbers or No of Arguments of different types –Advantage: Readability and Easy Maintanance As Client Program / Developer no need to remember different behaviour Names / function names to use in their code They can always use same function name but with difference in arguments in terms of No of arguments,their types or order of the arguments.

4 One or More Function with a Same Name with different logic,which is based on the Function signature –So logic varies with different function signature –Function Signature: –Combination of Function Name,Total Number of Arguments,Data Type of the each Arguments and Order of the Arguments. int sum(int a, int b); --->1 int incr() -->2 int sum(); --->3 int sum(float a, float b); --->4 int sum(int a, float b); --->5 int sum(float a, int b) ; --->6 int sum(int a, float b, char* name)-->7 int sum(float a, int b, char* name)-->8 int sum(char* name, float a,int b)-->9 Obervation on above functions: –Check 1 and 2--Signature differ in function name -Not Overloading –1 and 3 -->Overloaded with different Signature on No of arguments. –1 and 4, 1 and 5,1 and 6 -->Overloaded with different Signature on Data Type –5 and 6 --->Overloaded with different Signature on Order of Parameters Function Overloading

Constructor Overloading Similar to function overloading but parameterized constructor gets overloaded –Means One or More Constroctors with a Same Name with different logic,which is based on the Constructor signature interms of Arguements. Ex: class Student{ int id; string name,std; Student(){ id=0; name=”ILP” } Student(int lid, string lname){ id=ild; name=lname; } Student(int lid,string lname,string lstd){ id=ild; name=lname; std=lstd; } } int main() { Student stdObj; Student stdObj1(1,"Kalam"); // Calls Constructor two parameters Student stdObj2(2,"Raman","Fifth");// Calls Constructor with three //parameters }

6 Providing additional behaviour / meaning for the existing / supported operators in C++ Why to Provide Additional Meaning? –Lets Assume an Example,if you want to add two integers, Two decimal bumbers OR one decimal and one integer number To perform above operation, C++ supports predefined “+” Operator –But What if, if I want to perform addition operation On Complex Numbers Sum of balance amount from Two Bank Accounts Total Credit scores of a student from various Compututive exam –So Here the default behaviour of “ + ”provided by C++ compiler will not sufficient, As is works on only the variables of predefined data types –But here Complex No,Bank Account and Competutive Exam are the user defined types –So How to Make the “ + “ operator to work on User defined types –To make the operations like these to work on User defined objects, Operator overloading is introduced. Operator Overloading

7 Let us Summarize the Concept : Operator Overloading is nothing but defining the user defined logic for the existing/supported operators so as to make these operators to work on the even user defined Data types. So Providing addition meaning of working on user defineds types inaddition to the existing behaviour of working on predefined data types. Overload all of the Operators supported except This,sizeof,*,*-> operators like Syntax: rutuentype Operator (Objects as Arguements) Ex: Complex Operator +(Complex obj2); Operator Overloading

8 Its nothing but overloading the binary operators like “+”, ”-”,”/” etc.., Various ways to Overload to binary Operator: –One as Member Function of the Class –Other one as Friend Function So If we overload Binary Operator as Member function, we would require to pass Second Operand(Object ) as a parameter –Because First Operand(Object) can be accessed implecitely as the function is a member and is called using the first operand itself Ex: “+” as Member Function Counter Counter:: operator + (Counter C1) { Counter temp; temp.x = x+ C1.x; temp.y = y + C1.y; return temp; } int main() { Counter C1,C2,C3; C3 = C1 + C2; // Call converted to “C1.+(C2) “,Which is not Matching with the function def C3.display(); } Operator Overloading -Binary Operators

Binary Operator overloading using Friend function : -Required Two arguments would be passed as the function is called without the Object, so No object gets passed implecitely Example: Counter operator + (Counter ); //member function declaration for Binary + operator friend Counter operator + ( Counter, Counter); // Friend declaration for binary + operator Redefinition for friend would look like: Counter operator + (Counter C1,Counter C2) { Counter temp; temp.x = C1.x+ C2.x; temp.y = C1.y + C2.y; return temp; } Operator Overloading -Binary Operators

10 Its nothing but overloading the unary operators like “++(Pre)”,”(Post)++”,“-” etc.., Various ways to Overload to binary Operator: –One as Member Function of the Class –Other one as Friend Function So If we overload Unary Operator as Member function, No need to pass the Operand(Object ) as a parameter Because the Operand(Object) can be accessed implecitely as the function is a member and is called using the operand (Object) itself Ex: “++” as Member Function Counter operator ++() { Counter temp; temp.count = ++count; return temp; } int main() { Counter C1,C2; C2=++C1; //// Call converted to “C1.++() “,Which is not Matching with the function def } Operator Overloading -Unary Operators

11 Operator Overloading -Unary Operator UNARY Operator overloading using Friend function : -One arguments would be passed as the function is called without the Object, so No object gets passed implecitely Example: Counter operator - ( ); //member function declaration for Unary - operator friend Counter operator - ( Counter); // Friend declaration for Unary - operator Redefinition for friend would look like: Counter operator - (Counter C1) { Counter temp; temp.x = - C1.x; return temp; } int main() { Counter C1; C2=-C1; //// Call converted to “-(C1) “,Which is not Matching with the function def

Text Thank you