• Save
Aae oop xp_08
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Aae oop xp_08

on

  • 528 views

 

Statistics

Views

Total Views
528
Views on SlideShare
439
Embed Views
89

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 89

http://niitcourseslides.blogspot.in 89

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Aae oop xp_08 Presentation Transcript

  • 1. PolymorphismObjectivesIn this lesson, you will learn to: 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©NIIT OOPS/Lesson 8/Slide 1 of 27
  • 2. PolymorphismStatic Polymorphism Refers to an entity existing in different physical forms simultaneously©NIIT OOPS/Lesson 8/Slide 2 of 27
  • 3. PolymorphismFunction 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©NIIT OOPS/Lesson 8/Slide 3 of 27
  • 4. PolymorphismConstructor 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 };©NIIT OOPS/Lesson 8/Slide 4 of 27
  • 5. PolymorphismProblem Statement 6.D.1In an editor application such as the Microsoft Word, thereare several functions that perform various tasks, forexample, reading, editing, and formatting text, checkingfor grammar, searching and replacing text, opening andsaving a file, and closing the application.The description of a set of functions that perform the taskof opening a file is given below:1. Opens the file on the basis of the file name specified by the user2. Opens the file on the basis of the file name and directory path specified by the user©NIIT OOPS/Lesson 8/Slide 5 of 27
  • 6. PolymorphismProblem Statement 6.D.1 (Contd.)3. Opens the file on the basis of the file name, the directory path, and the file format like the specified by the userChoose appropriate function names.©NIIT OOPS/Lesson 8/Slide 6 of 27
  • 7. PolymorphismProblem Statement 6.P.1State which of the following sets of functions areoverloaded: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);©NIIT OOPS/Lesson 8/Slide 7 of 27
  • 8. PolymorphismProblem Statement 6.P.2Identify functions that you are required to code for theexisting employee class to implement the followingrequirements:1. Display all employee records2. Display an employee detail based on employee code3. Display employee names based on department name©NIIT OOPS/Lesson 8/Slide 8 of 27
  • 9. PolymorphismNeed for Operator Overloading To make operations on a user-defined data type as simple as the operations on a built-in data type©NIIT OOPS/Lesson 8/Slide 9 of 27
  • 10. PolymorphismClassification 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©NIIT OOPS/Lesson 8/Slide 10 of 27
  • 11. PolymorphismSimple 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)©NIIT OOPS/Lesson 8/Slide 11 of 27
  • 12. PolymorphismOverloading Simple Prefix Unary OperatorsExample:#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;}©NIIT OOPS/Lesson 8/Slide 12 of 27
  • 13. PolymorphismOverloading 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;}©NIIT OOPS/Lesson 8/Slide 13 of 27
  • 14. PolymorphismPre and Post Increment and DecrementOperators 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 compilerPostfix 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©NIIT OOPS/Lesson 8/Slide 14 of 27
  • 15. PolymorphismOverloading Pre and Post Increment andDecrement OperatorsExample:#include<iostream> class MyNum { private: int number; public: ... /* Operator */ MyNum operator ++();//Pre Increment MyNum operator ++(int);//Post //Increment };©NIIT OOPS/Lesson 8/Slide 15 of 27
  • 16. PolymorphismOverloading Pre and Post Increment andDecrement 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 }©NIIT OOPS/Lesson 8/Slide 16 of 27
  • 17. PolymorphismOverloading Pre and Post Increment andDecrement 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 }©NIIT OOPS/Lesson 8/Slide 17 of 27
  • 18. PolymorphismOverloading Binary OperatorsExample:#include<iostream> class MyNum { private: int number; public: MyNum(); MyNum(int); /* Operator */ MyNum operator +(MyNum); ... };©NIIT OOPS/Lesson 8/Slide 18 of 27
  • 19. PolymorphismOverloading Binary Operators (Contd.)MyNum MyNum ::operator +(MyNum N){ MyNum temp; temp.number = number+N.number; return temp;}©NIIT OOPS/Lesson 8/Slide 19 of 27
  • 20. PolymorphismJust a Minute…Modify the existing employee class such that when thefollowing statements are given in the main() function,the program successfully compares the basic salary ofthe 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”;©NIIT OOPS/Lesson 8/Slide 20 of 27
  • 21. PolymorphismJust a Minute…(Contd.)else cout<< “Employee 2 draws less salary than Employee 1”;}©NIIT OOPS/Lesson 8/Slide 21 of 27
  • 22. PolymorphismProblem Statement 6.P.3Consider the following class declaration:#include<iostream>class distance{ int length; public: distance(int);void operator =(distance);};Define the member-functions of the above class. Theoperator =()’ function should overload the ‘=’operator to assign the value of an object of thedistance class to another object of the distance class.The operator function should display a meaningfulmessage.©NIIT OOPS/Lesson 8/Slide 22 of 27
  • 23. PolymorphismProblem Statement 6.P.4Modify the existing employee class such that when thefollowing statements are given in the main() function,the program successfully increments the basic salary ofthe employee with the given amount. #include <iostream> void main() { Employee eObj1; eObj1.getdata(); //Accepts data eObj1 += 1000; }©NIIT OOPS/Lesson 8/Slide 23 of 27
  • 24. PolymorphismSummaryIn 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©NIIT OOPS/Lesson 8/Slide 24 of 27
  • 25. PolymorphismSummary (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©NIIT OOPS/Lesson 8/Slide 25 of 27
  • 26. PolymorphismSummary (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©NIIT OOPS/Lesson 8/Slide 26 of 27
  • 27. PolymorphismSummary (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 << >>©NIIT OOPS/Lesson 8/Slide 27 of 27