day3 & day4

Upload: harsh-chhabra

Post on 07-Apr-2018

266 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Day3 & Day4

    1/82

    Learning and Knowledge

    2006 IBM Corporation

    Inheritance

  • 8/6/2019 Day3 & Day4

    2/82

    Learning & Knowledge

    2006 IBM Corporation2 Programming in C++ 06/09/11

    Inheritance

    Objectives

    Explaining the inheritance

    Explaining how to derive inherited classes

    Using constructors in inheritance

    Using protected access specifier

    contd on the next slide..

  • 8/6/2019 Day3 & Day4

    3/82

    Learning & Knowledge

    2006 IBM Corporation3 Programming in C++ 06/09/11

    Explaining public inheritance

    Explaining private inheritance

    Using method overriding

    Using multiple inheritance

    Explaining virtual function

  • 8/6/2019 Day3 & Day4

    4/82

    Learning & Knowledge

    2006 IBM Corporation4 Programming in C++ 06/09/11

    Introduction

    Inheritance is a powerful feature of OOP and its mostimportant use is in software reusability.

    It is the process of creating new classes from alreadyexisting classes.

    The new class is called derived class and the alreadyexisting class is called the base class.

    The derived class inherits all the capabilities of the baseclass and can have some additional capabilities.

  • 8/6/2019 Day3 & Day4

    5/82

    Learning & Knowledge

    2006 IBM Corporation5 Programming in C++ 06/09/11

    Single Inheritance

    The derivation of a class from only one base class

    is called single inheritance and it is the ability of

    derived class to inherit member functions and

    member variables of the existing base class.

  • 8/6/2019 Day3 & Day4

    6/82

    Learning & Knowledge

    2006 IBM Corporation6 Programming in C++ 06/09/11

    Single Inheritance - Example

    class employee

    {

    private :

    int empno;

    char empname[20];

    char desig[20];

    public :

    void getinfo();

    void printinfo();

    };

    class worker

    {

    private :

    int empno;

    char empname[20];

    char desig[20];

    int bp, da, hra, netpay;

    public :

    void getinfo();

    void printinfo();

    void getpaydetails();

    void printpaydetails();

    };

  • 8/6/2019 Day3 & Day4

    7/82

    Learning & Knowledge

    2006 IBM Corporation7 Programming in C++ 06/09/11

    Single Inheritance

    Notice that in both employee and worker classes

    the fields empno, empname, desig and methods

    getinfo, printinfo() are the same.

    We can declare worker class we can inherit thefeatures of employee class

  • 8/6/2019 Day3 & Day4

    8/82

    Learning & Knowledge

    2006 IBM Corporation8 Programming in C++ 06/09/11

    Single Inheritance

    A Derived class is defined by specifying its relationship with thebase class in addition to its own details.

    General form of defining a derived class is

    Class derived_class : visibility_mode base_class

    Derived_class name of the derived class

    Visibility_mode access permission for the members of thederived class, it may either be private, public or protected

    Default access specifier is private

    Base_class is the name of the base_class from where we wantto draw the features

  • 8/6/2019 Day3 & Day4

    9/82

    Learning & Knowledge

    2006 IBM Corporation9 Programming in C++ 06/09/11

    Single Inheritance - Example

    class employee

    {

    private :

    int empno;

    char empname[20];

    char desig[20];

    public :

    void getinfo();

    void printinfo();

    };

    class worker : public employee

    {

    private :

    int bp, da, hra, netpay;

    public :void getpaydetails();

    void printpaydetails();

    };

    class employee

    {

    private :

    int empno;

    char empname[20];

    char desig[20];

    public :

    void getinfo();

    void printinfo();

    };

    class worker

    {

    private :

    int empno;

    char empname[20];

    char desig[20];

    int bp, da, hra, netpay;

    public :

    void getinfo();

    void printinfo();

    void getpaydetails();

    void printpaydetails();

    };

  • 8/6/2019 Day3 & Day4

    10/82

    Learning & Knowledge

    2006 IBM Corporation10 Programming in C++ 06/09/11

    Derived class

    The colon (:) indicates that we are inheriting

    something.

    It indicates the derivation of derived class (worker)

    from the base class (employee)

    The visibility mode is public.

    i.e when a base class is publicly inherited, all public

    members of the base class becomes public members ofthe derived class.

  • 8/6/2019 Day3 & Day4

    11/82

    Learning & Knowledge

    2006 IBM Corporation11 Programming in C++ 06/09/11

    Single Inheritance - Example

    Referworker.cpp

    http://../Documents%20and%20Settings/Administrator/Desktop/Relationships/second/second.cpphttp://../Documents%20and%20Settings/Administrator/Desktop/Relationships/second/second.cpphttp://../Documents%20and%20Settings/Administrator/Desktop/Relationships/second/second.cpphttp://../Documents%20and%20Settings/Administrator/Desktop/Relationships/second/second.cpp
  • 8/6/2019 Day3 & Day4

    12/82

    Learning & Knowledge

    2006 IBM Corporation12 Programming in C++ 06/09/11

    The Access Specifier

    An access specifier defines the accessibility of variablesand functions at different parts in the body of a program.

    Access specifiers are of three types:

    1. Private

    2. Protected

    3. public

  • 8/6/2019 Day3 & Day4

    13/82

    Learning & Knowledge

    2006 IBM Corporation13 Programming in C++ 06/09/11

    For a class:

    private members (both data and methods) are accessiblewithin that class only.

    protected members are accessible within that class andits derived sub_classes as well.

    public members are accessible everywhere and anywherein the program using that class.

  • 8/6/2019 Day3 & Day4

    14/82

    Learning & Knowledge

    2006 IBM Corporation14 Programming in C++ 06/09/11

    Table summarizing different accessibility relationships:

    access specifier accessible within accessible fromthe class outside the class

    private yes Noprotected yes Nopublic yes yes

    Table: Accessibility relationship in a class

  • 8/6/2019 Day3 & Day4

    15/82

    Learning & Knowledge

    2006 IBM Corporation15 Programming in C++ 06/09/11

    Public Inheritance

    In public inheritance

    base class properties are inherited in the derived classusing the public keyword.

    all the derived class member functions can be accessprotected and public members of the base class.

    the derived class functions cannot access the privateclass members of the base class.

  • 8/6/2019 Day3 & Day4

    16/82

    Learning & Knowledge

    2006 IBM Corporation16 Programming in C++ 06/09/11

    In public inheritance

    the protected and public class members of the base classare derived as protected and public members respectivelyin the derived class.

    the derived class definition begins as shown below:

    class derived_class : public base_class{------------------------------}

    This accessibility relationship is shown diagrammatically in thenext figure.

  • 8/6/2019 Day3 & Day4

    17/82

    Learning & Knowledge

    2006 IBM Corporation17 Programming in C++ 06/09/11

    Figure: Accessibility relationship in public inheritance

  • 8/6/2019 Day3 & Day4

    18/82

    Learning & Knowledge

    2006 IBM Corporation18 Programming in C++ 06/09/11

    Summary of accessibility relationships in public inheritance.

    base class

    access

    specifier

    accessible

    from own

    class

    accessible

    from

    derived

    class

    accessible

    from outside

    the class

    privateprotected

    public

    yesyes

    yes

    noyes

    yes

    nono

    yes

    Table:Accessibility relationships in public inheritance

  • 8/6/2019 Day3 & Day4

    19/82

    Learning & Knowledge

    2006 IBM Corporation19 Programming in C++ 06/09/11

    The following table shows aaccessibility relation in publi

    base class member

    visibilityprivate

    protected L i & K l d

  • 8/6/2019 Day3 & Day4

    20/82

    Learning & Knowledge

    2006 IBM Corporation20 Programming in C++ 06/09/11

    Private Inheritance

    In private inheritance

    base class properties are inherited in the derived classusing the private keyword.

    the derived class member functions can access only theprotected and public class members but not the privatemembers of the base class.

    the protected and public members of the base class arederived as private members in the derived class.

    L i & K l d

  • 8/6/2019 Day3 & Day4

    21/82

    Learning & Knowledge

    2006 IBM Corporation21 Programming in C++ 06/09/11

    In private inheritance

    objects of the derived class cannot access private, protectedand even public members of the base class.

    the derived class definition begins using the private keywordas shown below:

    class derived_class_name : private base_class_name{----------------------

    }

    This accessibility relationship is shown diagrammatically inthe following figure.

    L i & K l d

  • 8/6/2019 Day3 & Day4

    22/82

    Learning & Knowledge

    2006 IBM Corporation22 Programming in C++ 06/09/11

    Figure: Accessibility relationship in private inheritance

    L i & K l d

  • 8/6/2019 Day3 & Day4

    23/82

    Learning & Knowledge

    2006 IBM Corporation23 Programming in C++ 06/09/11

    The table below summarises the accessibility relationship in

    private inheritancein a tabular form.

    access

    specifier

    accessible from

    own class

    accessible from

    derived class

    accessible from

    outside

    Private yes no no

    protected yes yes no

    public yes yes no

    Table:Accessibility relationship in private inheritance

    Learning & Kno ledge

  • 8/6/2019 Day3 & Day4

    24/82

    Learning & Knowledge

    2006 IBM Corporation24 Programming in C++ 06/09/11

    The following table showsrelationship in private inhe

    base class membe

    visibilityPrivate

    Protected Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    25/82

    Learning & Knowledge

    2006 IBM Corporation25 Programming in C++ 06/09/11

    Constructors In Inheritance

    In inheritance the base-class default constructor is calledevery time when we invoke a derived class constructor inorder to create an object of the derived class.

    As a result, the derived class constructors may be redoingsome of the things done by the base-class default constructor.

    In order to avoid this repetition of the same job,the derived

    class constructors should invoke corresponding base classconstructors.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    26/82

    Learning & Knowledge

    2006 IBM Corporation26 Programming in C++ 06/09/11

    In principle, a derived class inherits every member of a base class except:

    its constructor and its destructor

    its operator=() members

    its friends

    Although the constructors and destructors of the base class are not inherited

    themselves, its default constructor (i.e., its constructor with no parameters) and

    its destructor are always called when a new object of a derived class is created

    or destroyed.

    What is inherited from the base class ?

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    27/82

    Learning & Knowledge

    2006 IBM Corporation27 Programming in C++ 06/09/11

    Derived-class default constructor should invoke base-class

    default constructor.

    For derived-class default constructor, the function definitionheading should become

    derived_class :: derived_class(): base_class(){-----------

    -----------}

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    28/82

    Learning & Knowledge

    2006 IBM Corporation28 Programming in C++ 06/09/11

    Derived constructor withconstructor with the samrelevant parameters into

    The derived constructor sh

    derived_class :: derived_cl

    : base class (x1,..,xi) Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    29/82

    Learning & Knowledge

    2006 IBM Corporation29 Programming in C++ 06/09/11

    Example of Constructors and Destructors invocation

    // constructors and derived classes

    #include

    using namespace std;

    class mother {

    public:

    mother ()

    { cout

  • 8/6/2019 Day3 & Day4

    30/82

    Learning & Knowledge

    2006 IBM Corporation30 Programming in C++ 06/09/11

    Destructors In Inheritance

    It is not necessary to define a destructor in the derived

    class unless there is some special task to be performed.

    When a derived-class object goes out of scope, thederived class destructor is called first and then the base

    class destructor.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    31/82

    Learning & Knowledge

    2006 IBM Corporation31 Programming in C++ 06/09/11

    M

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    32/82

    Learning & Knowledge

    2006 IBM Corporation32 Programming in C++ 06/09/11

    Overriding the Baseclass method

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    33/82

    Learning & Knowledge

    2006 IBM Corporation33 Programming in C++ 06/09/11

    Multiple Inheritance

    In multiple inheritance

    a new class is derived from more than one base classes.

    the derived class definition begins like

    class employ : public person, public array{- - - - -

    - - - - -}

    The base classes are listed after the colon (:) separated bycomas.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    34/82

    Learning & Knowledge

    2006 IBM Corporation34 Programming in C++ 06/09/11

    Example of Multiple Inheritance

    #include class CPolygon {

    protected:

    int width, height;

    public:

    void set_values (int a, int b){ width=a; height=b;}

    };

    class COutput {

    public:

    void output (int i);};

    void COutput::output (int i) {

    cout

  • 8/6/2019 Day3 & Day4

    35/82

    g & g

    2006 IBM Corporation35 Programming in C++ 06/09/11

    Virtual Base Class

    How can we avoid duplication elements passed to

    derived class when multiple inheritance is

    involved.

    stdinfo

    studmarksLangmarks

    totmarks

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    36/82

    g g

    2006 IBM Corporation36 Programming in C++ 06/09/11

    Virtual Base Class

    Class stdinfo is inherited by two classes namely langmarks

    and submarks. The class totmark then inherits from classes

    langmarks and submarks.

    In this process both multiple and multilevel inheritance are

    introduced.

    Now the totmarks has two base classes and these two base

    classes have a common base class stdinfo

    Totmarks has duplication of inherited members. How

    should these duplication be eliminated ?

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    37/82

    g g

    2006 IBM Corporation37 Programming in C++ 06/09/11

    Virtual Base Class

    The confusion created by multiple inheritance

    from the same class can be avoided by using

    virtual base class.

    A virtual base class causes a program to haveonly one copy of that class into a derived class.

    We can make a virtual base class just by adding

    the keyword virtual when declaring the base

    class in a class definition.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    38/82

    g g

    2006 IBM Corporation38 Programming in C++ 06/09/11

    Example of Virtual Base Class

    class stdinfo { /* ... */ }; // indirect base class

    class langmarks : virtual public stdinfo { /* ... */ };

    class studmarks : virtual public stdinfo { /* ... */ };

    class totmarks : public langmarks, public studmarks { /* ... */ }; // valid

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    39/82

    g g

    2006 IBM Corporation39 Programming in C++ 06/09/11

    Ambiguous base classes

    When you derive classes, ambiguities can result if base andderived classes have members with the same names.

    Access to a base class member is ambiguous if you use a name orqualified name that does not refer to a unique function or object.

    The declaration of a member with an ambiguous name in a derivedclass is not an error. The ambiguity is only flagged as an error ifyou use the ambiguous member name.

    For example, suppose that two classes named A and B both have amember named x, and a class named C inherits from both A and B.An attempt to access x from class C would be ambiguous.

    You can resolve ambiguity by qualifying a member with its classname using the scope resolution (::) operator.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    40/82

    2006 IBM Corporation40 Programming in C++ 06/09/11

    Ambigious base classclass B1 {

    public:

    int i;

    int j;

    void g(int) { }

    };

    class B2 {

    public:

    int j;

    void g() { }

    };

    class D : public B1, public B2 {

    public:

    int i;

    };

    int main()

    {

    D dobj;

    D *dptr = &dobj;

    dptr->i = 5;

    // dptr->j = 10; //ambigious error

    dptr->B1::j = 10;

    // dobj.g();

    dobj.B2::g();

    }

  • 8/6/2019 Day3 & Day4

    41/82

    Learning and Knowledge

    2006 IBM Corporation

    Polymorphism

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    42/82

    2006 IBM Corporation42 Programming in C++ 06/09/11

    Polymorphism

    Polymorphism triggers different reactions in

    objects in response to the same message.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    43/82

    2006 IBM Corporation43 Programming in C++ 06/09/11

    Binding

    Binding : It is the process of resolving a function call during

    the execution time or compile time.

    OR Binding is the process of associating a function to a class

    through the class's object or pointer (i.e resolving the

    function identity ) is called Binding

    Person p1;

    P1.acceptdetails()

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    44/82

    2006 IBM Corporation44 Programming in C++ 06/09/11

    Types of Binding

    Static or Early Binding

    Dynamic or late Binding

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    45/82

    2006 IBM Corporation45 Programming in C++ 06/09/11

    Static Binding

    Binding that happens during compile time is called Static

    Binding.

    Eg :

    main(){

    Person p1;

    P1.acceptdetails(); //binding happens during compile time

    }

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    46/82

    2006 IBM Corporation46 Programming in C++ 06/09/11

    Dynamic or Late Binding

    Binding that happens during runtime is called Dynamic

    Binding.

    Eg :

    main(){

    Person *p1;

    P1=new person;

    P1->acceptdetails(); //binding happens during run time

    }

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    47/82

    2006 IBM Corporation47 Programming in C++ 06/09/11

    Normal member functions accessed with pointersclass Base

    {

    public:void Base_Message()

    {

    cout

  • 8/6/2019 Day3 & Day4

    48/82

    2006 IBM Corporation48 Programming in C++ 06/09/11

    Special Case

    When the Base and derived class use the same

    name to one of their member functions, and a

    pointer to the Base class is used to invoke the

    member functions of both the base and derived

    classes

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    49/82

    2006 IBM Corporation49 Programming in C++ 06/09/11

    Example :

    class Base

    { public:void show_Message()

    {

    cout

  • 8/6/2019 Day3 & Day4

    50/82

    2006 IBM Corporation50 Programming in C++ 06/09/11

    In situations where one or more derived classes

    have a function with the same name and one of

    these functions is invoked, the identity of the

    function needs to be resolved.

    The compiler associates the member function with

    a class by identifying the type of the object or type

    of the pointer that is used to invoke the member

    function

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    51/82

    2006 IBM Corporation51 Programming in C++ 06/09/11

    How to implement Dynamic Binding

    Dynamic Binding is implemented by the use of

    Virtual Functions.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    52/82

    2006 IBM Corporation52 Programming in C++ 06/09/11

    Virtual Functions

    When the Base class and the Derived class use

    the same name to their member functions to

    resolve the identity of these functions (To ensure

    Late Binding) these functions are to be declared

    (with in the classes) preceded by the keyword

    'Virtual' and they are called 'Virtual Functions'

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    53/82

    2006 IBM Corporation53 Programming in C++ 06/09/11

    Example :

    class Base

    { public:virtual void show_Message()

    {

    cout

  • 8/6/2019 Day3 & Day4

    54/82

    2006 IBM Corporation54 Programming in C++ 06/09/11

    Rules for virtual functions

    1. Virtual functions must have identical declarations in the Base and Derivedclasses

    2. Virtual functions cannot be used as Friend Functions

    3. Virtual functions cannot be static

    4. A Virtual function in a Base class must be defined, even though it is notused.

    5. Constructors cannot be declared as virtual.

    6. Destructors can be declared as virtual.

    Learning & Knowledge

    Polymorphism

  • 8/6/2019 Day3 & Day4

    55/82

    2006 IBM Corporation55 Programming in C++ 06/09/11

    Polymorphism

    Static( Compile Time )

    Dynamic( Run-time )

    Opera

    torO

    verloadin

    g

    Virtual Functions

    Funct io

    nOverlo

    ading

    Method

    Overrid

    ing

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    56/82

    2006 IBM Corporation56 Programming in C++ 06/09/11

    Pure Virtual Functions

    A Virtual Function with no body is called as PureVirtual Function

    This is done by adding the notation "=0" to thevirtual function declaration

    eg

    Virtual void show_Message() = 0;

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    57/82

    2006 IBM Corporation57 Programming in C++ 06/09/11

    Polymorphic class

    The class which contains atleast one of more

    virtual functions / pure virtual functions is called

    'polymorphic class'.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    58/82

    2006 IBM Corporation58 Programming in C++ 06/09/11

    Abstract Base Class

    The class containing one or more pure virtual functions

    is called an 'Abstract Base Class'

    An Abstract class can only be used as a Base class forderiving sub classes.

    instances(objects) of Abstract classes CANNOT be

    created

  • 8/6/2019 Day3 & Day4

    59/82

    Vehicle

  • 8/6/2019 Day3 & Day4

    60/82

    Flyer

  • 8/6/2019 Day3 & Day4

    61/82

    Learning and Knowledge

    2006 IBM Corporation

    Streams

  • 8/6/2019 Day3 & Day4

    62/82

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    63/82

    2006 IBM Corporation63 Programming in C++ 06/09/11

    Character reading and Writing

    #include

    #include

    #include

    int main()

    {

    ofstream outfile("c:\\test2.txt");

    outfile

  • 8/6/2019 Day3 & Day4

    64/82

    2006 IBM Corporation64 Programming in C++ 06/09/11

    Writing characters to a fileWriting the characters to a file

    #include

    #include

    #include

    //Write to the file

    int main()

    {

    const int MAX=80;

    char buffer[MAX];

    ifstream infile("c:\\test2.txt");

    while(infile)

    {

    infile.getline(buffer,MAX);

    cout

  • 8/6/2019 Day3 & Day4

    65/82

    2006 IBM Corporation65 Programming in C++ 06/09/11

    Reading and Writing to a file using get and put

    #include #include

    #include

    #include

    int main()

    {

    ofstream outfile("c:\\test3.txt");

    //char str[]="Time is a great teacher";

    char s[30];

    cout

  • 8/6/2019 Day3 & Day4

    66/82

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    67/82

    2006 IBM Corporation67 Programming in C++ 06/09/11

    Reading from an Object

    #include "stdafx.h"

    #include

    class person{

    protected :

    char name[40];

    int age;

    public:

    void showdata()

    {

    cout

  • 8/6/2019 Day3 & Day4

    68/82

    2006 IBM Corporation68 Programming in C++ 06/09/11

    Reading and Writing from an object#include "stdafx.h"

    #include

    class person

    {

    protected : char name[40];

    int age;

    public:

    void getdata()

    { coutname;

    coutage;

    }

    void showdata()

    {

    cout

  • 8/6/2019 Day3 & Day4

    69/82

    Learning and Knowledge

    2006 IBM Corporation

    Operator Overloading

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    70/82

    2006 IBM Corporation70 Programming in C++ 06/09/11

    Operator Overloading

    Operator Overloading refers to giving additionalmeaning to the normal C++ operators when they

    are applied to the user-defined datatypes

    The overloaded operator is defined as a function

    inside a class.

    The function for the overloaded operator is

    declared using the keyword 'Operator'

  • 8/6/2019 Day3 & Day4

    71/82

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    72/82

    2006 IBM Corporation72 Programming in C++ 06/09/11

    Syntax for operator overloading

    General form of an operator overloading function

    is

    return type class-name::operator operator_to_overload(arg list)

    {

    }

    Example :

    void sample::operator ++()

    {}

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    73/82

    2006 IBM Corporation73 Programming in C++ 06/09/11

    Function overloading

    C++ allows you to use the same name for different

    functions. As long as they have different

    parameter type lists, the compiler will regards

    them as different functions.

    Eg : int max(int, int);

    int max(int, int, int);

    double max(double, double);

    ReferFunctionOverloading.cpp

    Learning & Knowledge

    http://../Documents%20and%20Settings/Administrator/Desktop/Relationships/FunctionOverloading.txthttp://../Documents%20and%20Settings/Administrator/Desktop/Relationships/FunctionOverloading.txthttp://../Documents%20and%20Settings/Administrator/Desktop/Relationships/FunctionOverloading.txthttp://../Documents%20and%20Settings/Administrator/Desktop/Relationships/FunctionOverloading.txt
  • 8/6/2019 Day3 & Day4

    74/82

    2006 IBM Corporation74 Programming in C++ 06/09/11

    Why Operator Overloading ?

    The Operator + is used to add integer or float.

    We cannot use this operator to add two objects.

    For example we need to add 2 time objects and

    return the sum of the same to the user.

    This cannot be done the + operator, we need to

    use the user defined function to perform this type

    of calculation

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    75/82

    2006 IBM Corporation75 Programming in C++ 06/09/11

    Operator Overloading

    Operator overloading enable us to apply standard

    operators such as +, -, *, < and many more, to

    objects of our own data types.

    We need to write a function that redefines aparticular operator so that it performs a particular

    action every time it is applied to objects of our

    class.

    An operator overloading function is created usingthe keyword operator.

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    76/82

    2006 IBM Corporation76 Programming in C++ 06/09/11

    c++ operators that can be overloaded are

    + - * / % ^ | ~ !

    == < > += -= *= /= %=

    ^= &= != > >>=

    == = && || ++ -- []

    () new delete

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    77/82

    2006 IBM Corporation77 Programming in C++ 06/09/11

    Operators which cannot be overloaded

    :: scope resolution operator

    Sizeof operator

    ?: conditional operator

    .* de-reference pointer to class member operator

    . Direct member operator

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    78/82

    2006 IBM Corporation78 Programming in C++ 06/09/11

    Classification of Operator Overloading

    Unary Operator Overloading : There should be no arguments forunary operator overloading.

    Eg : i ++, j

    ++, -- are unary operators

    Binary Operator Overloading : There should be one parameter forbinary operator overloading.

    Eg : i + j

    m n

    m * n

    Where +, -, * are called binary operators

    Learning & Knowledge

    Overloading of Binary Operator +

  • 8/6/2019 Day3 & Day4

    79/82

    2006 IBM Corporation79 Programming in C++ 06/09/11

    class fps

    {

    private :

    int feet; float inch;

    public :

    fps(int=0, float=0.0);

    fps operator+(fps &k)

    {

    feet+= k.feet;

    inch+=k.inch;

    while(inch>12.0)

    {

    inch=inch - 12;

    feet++;

    return *this;

    }

    void print() { }

    };

    fps::fps(int f=0,float i=0.0)

    { feet=f; inch=i; }

    void fps::print(){

    cout

  • 8/6/2019 Day3 & Day4

    80/82

    2006 IBM Corporation80 Programming in C++ 06/09/11

    class fps

    {

    private :

    int feet; float inch;

    public :

    fps(int=0, float=0.0);

    void operator++()

    {

    feet+= 1;

    inch+= 1;

    if(inch>=12.0)

    {

    inch-=12;

    feet++;

    }

    void print() { }

    };

    fps::fps(int f=0,float i=0.0)

    { feet=f; inch=i; }

    void fps::print(){

    cout

  • 8/6/2019 Day3 & Day4

    81/82

    2006 IBM Corporation81 Programming in C++ 06/09/11

    Example of Post incrementing

    fps operator++(int)

    {

    fps temp;

    temp.feet = feet++;

    temp.inch=inch++;

    if(inch >= 12.0)

    {inch-=12;

    feet+=1;

    }

    return temp;

    }

    void main()

    {

    fps obj1(10,10);

    fps obj2;

    obj2=obj1++;}

    while overloading the unary operators ++ and -- to distinguish between

    prefixing and postfixing a dummy parameter is used in case of ostfixing

    Learning & Knowledge

  • 8/6/2019 Day3 & Day4

    82/82

    References

    Programming With C++ - Schaum Series John

    Hubbar

    Programming in C++ - Radha Ganesh

    www.Codeguru.com

    www.cplusplus.com

    http://www.codeguru.com/http://www.cplusplus.com/http://www.cplusplus.com/http://www.codeguru.com/