c++day-5

Upload: eshamu

Post on 04-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 C++Day-5

    1/15

  • 7/31/2019 C++Day-5

    2/15

    Explain Overloading

    Explain Member Function Overloading

    member Function Overloading with default Args.

  • 7/31/2019 C++Day-5

    3/15

    Another powerful and useful concept of object

    oriented programming is overloading

    which allows operation with the same name but

    different semantics and different ways of

    implementation to be invoked for object of differnt

    types.

    For instance,binary operation can be overloaded for

    complex number arrays,set or list

    Overloading

  • 7/31/2019 C++Day-5

    4/15

    C++ enable two or more functions with the samename but with different types of arguments or different

    sequence of arguments or different number of arguments. It is

    also called as FunctionPolymorphism

    Function Overloading

  • 7/31/2019 C++Day-5

    5/15

    Two or more functions with the same

    name but with different types of arguments

  • 7/31/2019 C++Day-5

    6/15

    MEMBER FUNCTION

    OVERLOADING#include#include

    void display(char,int);

    void display(int,int);void main()

    {

    clrscr();

    display('*',10);display(10,5);

    getch();

    }

    void display(char c, int n)

    {for (;n>0;n--)

    cout

  • 7/31/2019 C++Day-5

    7/15

    Two or more functions with the same

    name but with different Number of arguments

  • 7/31/2019 C++Day-5

    8/15

    // Write a C++ program to find

    the area of the triangle using

    Memeber Function Overloading

    #include

    class rectangle

    {

    private :

    int length,breadth,area;

    public :void getdata() {

    coutlength;

    cout

  • 7/31/2019 C++Day-5

    9/15

    Two or more functions with the same

    name but with different Sequence of arguments

  • 7/31/2019 C++Day-5

    10/15

    #include

    #include

    void line();

    void line(char);

    void line(char,int);void main()

    {

    int no_of_time;

    char c;

    clrscr();coutno_of_time;

    coutc;

    line();

    line(c);

    line(c,no_of_time);

    getch();

    }

    void line()

    {

    for(int i=0;i

  • 7/31/2019 C++Day-5

    11/15

    The default values are given in the

    function prototype declaration .whenever a call

    is made to the function without specifying on

    arguments the program will automatically

    Assign values to the parameter form the

    default function prototype declaration

    Member Function Overloading with

    Default Arguments

  • 7/31/2019 C++Day-5

    12/15

    Member function Overloading with default arguments

    #include

    #include

    int sum(int x=10,int y=20,int z=30);void main()

    {

    clrscr();

    cout

  • 7/31/2019 C++Day-5

    13/15

    Difference between Overloading and

    Overriding

    Overloading

    Overriding

    Allows a function to be given more

    than one definition

    The types of arguments or numberof arguments which the function

    takes determines which definition

    will be used

    It is the ability to change the

    definition of the base class method

    by writing a code for them in the

    sub-class of an inheritance

  • 7/31/2019 C++Day-5

    14/15

    Function Overloading is the ability to write more than on

    function with the same name with different number ,type and

    sequence of arguments. It is also referred as Function

    polymorphism

  • 7/31/2019 C++Day-5

    15/15