SlideShare a Scribd company logo
1 of 27
Polymorphism


Objectives
In 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
Polymorphism


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




©NIIT                                  OOPS/Lesson 8/Slide 2 of 27
Polymorphism


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


©NIIT                                  OOPS/Lesson 8/Slide 3 of 27
Polymorphism


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

©NIIT                       OOPS/Lesson 8/Slide 4 of 27
Polymorphism


Problem Statement 6.D.1
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


©NIIT                                 OOPS/Lesson 8/Slide 5 of 27
Polymorphism


Problem 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 user
Choose appropriate function names.




©NIIT                                   OOPS/Lesson 8/Slide 6 of 27
Polymorphism


Problem Statement 6.P.1
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);
©NIIT                                 OOPS/Lesson 8/Slide 7 of 27
Polymorphism


Problem Statement 6.P.2
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




©NIIT                               OOPS/Lesson 8/Slide 8 of 27
Polymorphism


Need 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
Polymorphism


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

©NIIT                                 OOPS/Lesson 8/Slide 10 of 27
Polymorphism


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)




©NIIT                              OOPS/Lesson 8/Slide 11 of 27
Polymorphism


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;
}
©NIIT                        OOPS/Lesson 8/Slide 12 of 27
Polymorphism


Overloading Simple Prefix Unary Operators
(Contd.)
void MyInt ::accept(int x, int y)
{
  a=x;b=y;
}
void MyInt ::print()
{
  couta=aendl;coutb=bendl;
}
int main()
{
  MyInt i1;
  i1.accept(15, -25);
  i1.print();-i1;
  i1.print();
  return 0;
}
©NIIT                        OOPS/Lesson 8/Slide 13 of 27
Polymorphism


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

©NIIT                                  OOPS/Lesson 8/Slide 14 of 27
Polymorphism


Overloading Pre and Post Increment and
Decrement Operators
Example:
#includeiostream
 class MyNum
 {
 private:
     int number;
 public:
      ...
 /* Operator */
     MyNum operator ++();//Pre Increment
     MyNum operator ++(int);//Post
                            //Increment
 };
©NIIT                        OOPS/Lesson 8/Slide 15 of 27
Polymorphism


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
  }



©NIIT                        OOPS/Lesson 8/Slide 16 of 27
Polymorphism


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
 }

©NIIT                        OOPS/Lesson 8/Slide 17 of 27
Polymorphism


Overloading Binary Operators
Example:
#includeiostream
 class MyNum
 {
 private:
     int number;
 public:
      MyNum(); MyNum(int);
     /* Operator */
      MyNum operator +(MyNum);
     ...
 };
©NIIT                          OOPS/Lesson 8/Slide 18 of 27
Polymorphism


Overloading 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
Polymorphism


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(eObj1eObj2)
      cout “Employee 1 draws less salary
      than Employee 2”;
©NIIT                               OOPS/Lesson 8/Slide 20 of 27
Polymorphism


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




©NIIT                     OOPS/Lesson 8/Slide 21 of 27
Polymorphism


Problem Statement 6.P.3
Consider the following class declaration:
#includeiostream
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.
©NIIT                                OOPS/Lesson 8/Slide 22 of 27
Polymorphism


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

©NIIT                               OOPS/Lesson 8/Slide 23 of 27
Polymorphism


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


©NIIT                               OOPS/Lesson 8/Slide 24 of 27
Polymorphism


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
©NIIT                               OOPS/Lesson 8/Slide 25 of 27
Polymorphism


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



©NIIT                               OOPS/Lesson 8/Slide 26 of 27
Polymorphism


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             



©NIIT                                 OOPS/Lesson 8/Slide 27 of 27

More Related Content

What's hot

Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14Abu Saleh
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismJussi Pohjolainen
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++PRINCE KUMAR
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
C programming session 02
C programming session 02C programming session 02
C programming session 02AjayBahoriya
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversionNabeelaNousheen
 
Inline assembly language programs in c
Inline assembly language programs in cInline assembly language programs in c
Inline assembly language programs in cTech_MX
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 

What's hot (20)

Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
 
Lambda
LambdaLambda
Lambda
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Inline function
Inline functionInline function
Inline function
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Inline assembly language programs in c
Inline assembly language programs in cInline assembly language programs in c
Inline assembly language programs in c
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 

Viewers also liked

Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

Viewers also liked (7)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Oops ppt
Oops pptOops ppt
Oops ppt
 

Similar to Polymorphism Overloading Functions Operators

Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfesuEthopi
 
Aae oop xp_07
Aae oop xp_07Aae oop xp_07
Aae oop xp_07Niit Care
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismCHAITALIUKE1
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxAaliyanShaikh
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxMehakBhatia38
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloadingRai University
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Look at the following main function- Write a program using the main fu.docx
Look at the following main function- Write a program using the main fu.docxLook at the following main function- Write a program using the main fu.docx
Look at the following main function- Write a program using the main fu.docxhendriciraida
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingRai University
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfstudy material
 

Similar to Polymorphism Overloading Functions Operators (20)

Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Aae oop xp_07
Aae oop xp_07Aae oop xp_07
Aae oop xp_07
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
 
Function
FunctionFunction
Function
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
C Operators
C OperatorsC Operators
C Operators
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
Oops recap
Oops recapOops recap
Oops recap
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Look at the following main function- Write a program using the main fu.docx
Look at the following main function- Write a program using the main fu.docxLook at the following main function- Write a program using the main fu.docx
Look at the following main function- Write a program using the main fu.docx
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Polymorphism Overloading Functions Operators

  • 1. Polymorphism Objectives In 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. Polymorphism Static Polymorphism Refers to an entity existing in different physical forms simultaneously ©NIIT OOPS/Lesson 8/Slide 2 of 27
  • 3. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 3 of 27
  • 4. Polymorphism 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 }; ©NIIT OOPS/Lesson 8/Slide 4 of 27
  • 5. Polymorphism Problem Statement 6.D.1 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 ©NIIT OOPS/Lesson 8/Slide 5 of 27
  • 6. Polymorphism Problem 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 user Choose appropriate function names. ©NIIT OOPS/Lesson 8/Slide 6 of 27
  • 7. Polymorphism Problem Statement 6.P.1 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); ©NIIT OOPS/Lesson 8/Slide 7 of 27
  • 8. Polymorphism Problem Statement 6.P.2 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 ©NIIT OOPS/Lesson 8/Slide 8 of 27
  • 9. Polymorphism Need 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. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 10 of 27
  • 11. Polymorphism 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) ©NIIT OOPS/Lesson 8/Slide 11 of 27
  • 12. Polymorphism 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; } ©NIIT OOPS/Lesson 8/Slide 12 of 27
  • 13. Polymorphism Overloading Simple Prefix Unary Operators (Contd.) void MyInt ::accept(int x, int y) { a=x;b=y; } void MyInt ::print() { couta=aendl;coutb=bendl; } int main() { MyInt i1; i1.accept(15, -25); i1.print();-i1; i1.print(); return 0; } ©NIIT OOPS/Lesson 8/Slide 13 of 27
  • 14. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 14 of 27
  • 15. Polymorphism Overloading Pre and Post Increment and Decrement Operators Example: #includeiostream 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. Polymorphism 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 } ©NIIT OOPS/Lesson 8/Slide 16 of 27
  • 17. Polymorphism 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 } ©NIIT OOPS/Lesson 8/Slide 17 of 27
  • 18. Polymorphism Overloading Binary Operators Example: #includeiostream class MyNum { private: int number; public: MyNum(); MyNum(int); /* Operator */ MyNum operator +(MyNum); ... }; ©NIIT OOPS/Lesson 8/Slide 18 of 27
  • 19. Polymorphism Overloading 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. Polymorphism 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(eObj1eObj2) cout “Employee 1 draws less salary than Employee 2”; ©NIIT OOPS/Lesson 8/Slide 20 of 27
  • 21. Polymorphism Just a Minute…(Contd.) else cout “Employee 2 draws less salary than Employee 1”; } ©NIIT OOPS/Lesson 8/Slide 21 of 27
  • 22. Polymorphism Problem Statement 6.P.3 Consider the following class declaration: #includeiostream 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. ©NIIT OOPS/Lesson 8/Slide 22 of 27
  • 23. Polymorphism 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; } ©NIIT OOPS/Lesson 8/Slide 23 of 27
  • 24. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 24 of 27
  • 25. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 25 of 27
  • 26. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 26 of 27
  • 27. Polymorphism 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 ©NIIT OOPS/Lesson 8/Slide 27 of 27