Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

Embed Size (px)

Citation preview

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    1/28

    Object Oriented Programming

    (OOP)Lecture No. 10

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    2/28

    Review

    Copy constructors

    Destructor

    Accessor Functions

    this Pointer

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    3/28

    this Pointer

    There are situations wheredesigner wants to returnreference to current objectfrom a function

    In such cases reference is

    taken from this pointer like(*this)

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    4/28

    Example

    Student Student::setRollNo(int aNo)

    {

    return *this;}

    Student Student::setName(char *aName)

    {

    return *this;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    5/28

    Example

    int main()

    {

    Student aStudent;

    Student bStudent;

    bStudent = aStudent.setName(Ahmad);

    bStudent = aStudent.setName(Ali).setRollNo(2);

    return 0;}

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    6/28

    Separation of interface andimplementation

    Public member function exposed by aclass is called interface

    Separation of implementation fromthe interface is good softwareengineering

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    7/28

    Complex Number

    There are two representations ofcomplex number

    Euler formz = x + iy

    Phasor form

    z = |z| (cos + isin )z is known as the complex modulusand is known as the complex

    argument or phase

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    8/28

    Example

    float getX()

    float getY()void setNumber

    (float i, float j)

    float xfloat y

    Complex

    Old implementation

    float getX()

    float getY()void setNumber

    (float i, float j)

    float zfloat theta

    Complex

    Newimplementation

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    9/28

    Example

    class Complex{ //oldfloat x;

    float y;

    public:

    void setNumber(float i, float j){

    x = i;

    y = j;

    }

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    10/28

    Example

    class Complex{ //newfloat z;

    float theta;

    public:

    void setNumber(float i, float j){

    theta = arctan(j/i);

    }

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    11/28

    Advantages

    User is only concerned about waysof accessing data (interface)

    User has no concern about theinternal representation andimplementation of the class

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    12/28

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    13/28

    Student.h

    class Student{

    int rollNo;

    public:

    void setRollNo(int aRollNo);int getRollNo();

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    14/28

    Student.cpp

    #include student.h

    void Student::setRollNo(int aNo){

    }

    int Student::getRollNo(){

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    15/28

    Driver.cpp

    #include student.h

    int main(){

    Student aStudent;}

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    16/28

    There are functions that aremeant to be read only

    There must exist a mechanismto detect error if such functions

    accidentally change the datamember

    constMember Functions

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    17/28

    Keyword constis placed at the

    end of the parameter list

    constMember Functions

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    18/28

    constMember Functions

    Declaration:

    class ClassName{

    ReturnVal Function() const;

    };

    Definition:

    ReturnVal ClassName::Function() const{

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    19/28

    Example

    class Student{

    public:

    int getRollNo() const{return rollNo;

    }

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    20/28

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    21/28

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    22/28

    Example

    bool Student::isRollNo(int aNo){

    /*undetected typing mistake*/

    if(rollNo = aNo){return true;

    }

    return false;}

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    23/28

    Example

    bool Student::isRollNo

    (int aNo)const{

    /*compiler error*/

    if(rollNo = aNo){

    return true;

    }

    return false;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    24/28

    constFunctions

    Constructors and Destructorscannot be const

    Constructor and destructor areused to modify the object to a welldefined state

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    25/28

    Example

    class Time{

    public:

    Time() const {} //error~Time() const {} //error

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    26/28

    constFunction

    Constant member functioncannot change data member

    Constant member functioncannot access non-constant

    member functions

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    27/28

    Example

    class Student{

    char * name;

    public:

    char *getName();void setName(char * aName);

    int ConstFunc() const{

    name = getName(); //error

    setName(Ahmad);//error}

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 10

    28/28

    this Pointer and const MemberFunction

    this pointer is passed as constantpointer to const data in case of

    constant member functionsconst Student *const this;

    instead ofStudent * const this;