lesson 9 OOP.pdf - Lesson 9 Function and operator...

This preview shows page 1 - 3 out of 5 pages.

Lesson 9: Function and operator overloading 9.1. Introduction In our previous lesson, you learnt about functions: function declaration, definitions and function calls. You also learnt about inline functions and recursive functions. Today, you will learn about overloading of functions and operators. 9.2. Lesson objectives By the end of this lesson, you will be able: Describe function overloading Discuss the limitations and benefits of function overloading Discuss operator overloading 9.3. Lesson outline. This lesson is organized as follows: 9.1. Introduction 9.2. Lesson objectives 9.3. Lesson outline 9.4. Function overloading 9.5. Operator overloading 9.6. Function overriding 9.7. Revision questions 9.8. Summary 9.9. Suggested reading 9.4. Function overloading Function overloading is a feature of C++ used to create multiple functions with the same name, so long as they have different formal parameters. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type. Example: A program to use overloading to calculate area.
#include<iostream> using namespace std; float area(float l, float w ) //the formal parameters are l and w { return (l*w); } float area(float r ) //the formal parameter is r { return (3.142*r*r); } int main() { float length,width,radius; cout<<"Enter the length"<<endl; cin>>length; cout<<"Enter the width"<<endl; cin>>width; cout<<"Enter radius"<<endl; cin>>radius;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture