Objectives Define polymorphism Overload functions

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
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. 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.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
Classes - Intermediate
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Chapter 15 - C++ As A "Better C"
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
C++ LANGUAGE MULTIPLE CHOICE QUESTION SET-3
Operator Overloading Ritika Sharma.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 13: Overloading and Templates
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Templates.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment and Interactive Input.
Introduction to C++ Systems Programming.
Object-Oriented Programming (OOP) Lecture No. 21
Basic Elements of C++.
Function Overloading.
University of Central Florida COP 3330 Object Oriented Programming
Objectives Identify the built-in data types in C++
Operator Overloading BCA Sem III K.I.R.A.S.
Basic Elements of C++ Chapter 2.
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Overview of C++ Overloading
Operator overloading Dr. Bhargavi Goswami
Operator Overloading, Friends, and References
Introduction to C++ Programming
Operator Overloading.
Operator Overloading.
Chapter 11: Inheritance and Composition
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
Chapter 6 Polymorphism.
Predefined Functions Revisited
C++ Programming Basics
Andy Wang Object Oriented Programming in C++ COP 3330
C++ for Engineers and Scientists Second Edition
HNDIT11034 More Operators.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Objectives Define polymorphism Overload functions Identify overloading functions as an implementation of static polymorphism Understand the need for overloading operators Overload the following unary operators: Simple prefix unary operators Pre and post increment and decrement operators Overload a binary operator

Static Polymorphism Refers to an entity existing in different physical forms simultaneously

Function Overloading Is the process of using the same name for two or more functions Requires each redefinition of a function to use a different function signature that is: different types of parameters, or sequence of parameters, or number of parameters Is used so that a programmer does not have to remember multiple function names

Constructor Overloading Is commonly used in C++ Example: #include <iostream> class Calculator { int number1, number2, tot; public: Calculator();//Default Constructor Calculator(int,int);//Two-Argument //Constructor };

Problem In an editor application such as the Microsoft Word, there are several functions that perform various tasks, for example, reading, editing, and formatting text, checking for grammar, searching and replacing text, opening and saving a file, and closing the application. The description of a set of functions that perform the task of opening a file is given below: 1. Opens the file on the basis of the file name specified by the user 2. Opens the file on the basis of the file name and directory path specified by the user

Problem 3. Opens the file on the basis of the file name, the directory path, and the file format like the specified by the user Choose appropriate function names.

Problem Statement State which of the following sets of functions are overloaded: 1. void add(int, int); void add(float, float); 2. void display(int, char); int display(int, char); 3. int get(int); int get(int, int); 4. int square(int); float square(float);

Problem Statement Identify functions that you are required to code for the existing employee class to implement the following requirements: 1. Display all employee records 2. Display an employee detail based on employee code 3. Display employee names based on department name

Need for Operator Overloading To make operations on a user-defined data type as simple as the operations on a built-in data type

Classification of Operators Unary operators Simple prefix unary operators Pre and post increment and decrement operators Binary operators Simple operators Comparison operators The assignment operator The insertion and extraction operator Special operators

Simple Prefix Unary Operators Are defined by either a member function that takes no parameter or a non-member function that takes one parameter Example: i1.operator -() or operator -(i1)

Overloading Simple Prefix Unary Operators Example: #include <iostream> class MyInt { private : int a;int b; public : void operator -(); // member function void accept(int, int); void print(); }; void MyInt ::operator –() a=-a;b=-b; }

Overloading Simple Prefix Unary Operators (Contd.) void MyInt ::accept(int x, int y) { a=x;b=y; } void MyInt ::print() cout<<"a="<<a<<endl;cout<<"b="<<b<<endl; int main() MyInt i1; i1.accept(15, -25); i1.print();-i1; i1.print(); return 0;

Pre and Post Increment and Decrement Operators Prefix application of the operator Causes the variable to be incremented before it is used in an expression Causes the operator function with no argument to be invoked by the compiler Postfix application of the operator Causes the variable to be incremented after it is used in an expression Causes the operator function with an int argument to be invoked by the compiler

Overloading Pre and Post Increment and Decrement Operators Example: #include<iostream> class MyNum { private: int number; public: ... /* Operator */ MyNum operator ++();//Pre Increment MyNum operator ++(int);//Post //Increment };

Overloading Pre and Post Increment and Decrement Operators (Contd.) MyNum MyNum ::operator ++() //Pre //Increment { MyNum temp; number = number + 1; //First //Increment number temp.number = number; // Then Assign The //Value To temp return temp; //Return The //Incremented Value }

Overloading Pre and Post Increment and Decrement Operators (Contd.) MyNum MyNum ::operator ++(int) //Post //Increment { MyNum temp; temp.number = number; //First Assign //The Current Value Of //number To temp number = number + 1; // Then Increment //number return temp; // Return The //Original Value }

Overloading Binary Operators Example: #include<iostream> class MyNum { private: int number; public: MyNum(); MyNum(int); /* Operator */ MyNum operator +(MyNum); ... };

Overloading Binary Operators (Contd.) MyNum MyNum ::operator +(MyNum N) { MyNum temp; temp.number = number+N.number; return temp; }

Just a Minute… Modify the existing employee class such that when the following statements are given in the main() function, the program successfully compares the basic salary of the employees and displays the given message. #include <iostream> void main() { Employee eObj1, eObj2; eObj1.getdata(); //Accepts data eObj2.getdata(); if(eObj1<eObj2) cout<< “Employee 1 draws less salary than Employee 2”;

Just a Minute…(Contd.) else cout<< “Employee 2 draws less salary than Employee 1”; }

Problem Statement 6.P.3 Consider the following class declaration: #include<iostream> class distance { int length; public: distance(int);void operator =(distance); }; Define the member-functions of the above class. The 'operator =()’ function should overload the ‘=’ operator to assign the value of an object of the distance class to another object of the distance class. The operator function should display a meaningful message.

Problem Statement 6.P.4 Modify the existing employee class such that when the following statements are given in the main() function, the program successfully increments the basic salary of the employee with the given amount. #include <iostream> void main() { Employee eObj1; eObj1.getdata(); //Accepts data eObj1 += 1000; }

Summary In this lesson, you learned that: The term polymorphism has been derived form the Greek words ‘poly’ and ‘morphos’, which means ‘many’ and ‘forms’, respectively Function overloading is the process of using the same name for two or more functions The number, type, or sequence of parameters for a function is called the function signature Static polymorphism is exhibited by a function when it exists in different forms

Summary (Contd.) Operator overloading refers to providing additional meaning to the normal C++ operators when they are applied to user-defined data types Operator overloading improves the clarity of user- defined data types The predefined C++ operators can be overloaded by using the operator keyword Operators may be considered as functions internal to the compiler Operators may be classified into two types: Unary and Binary Unary operators work with one operand

Summary (Contd.) Unary operators can be classified as: Simple prefix unary operators, for example, ! and - Pre and post increment and decrement operators A prefix unary operator may be defined by a member function taking no parameter or a non-member function taking one parameter In order to avoid confusion between pre and post-fix operators, all postfix unary operators take in a dummy integer Binary operators work with two operands

Summary (Contd.) Overloading a binary operator is similar to overloading a unary operator except that a binary operator requires an additional parameter In order to understand their overloading better, binary operators may be classified as follows: Simple operators, like + - * / % += -= Comparison operators, like < > <= >= != == The assignment operator = The insertion and extraction operator << >>