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

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

of 38

Transcript of Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 08

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

    1/38

    Object Oriented Programming(OOP)

    Lecture No. 8

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

    2/38

    Review

    Class

    Concept

    Definition

    Data members

    Member Functions

    Access specifier

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

    3/38

    Member Functions

    Member functions are the functions thatoperate on the data encapsulated in theclass

    Public member functions are the interface tothe class

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

    4/38

    Member Functions (contd.)

    Define member function inside the classdefinition

    OR

    Define member function outside the classdefinition

    But they must be declared inside class definition

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

    5/38

    Function Inside Class Body

    class ClassName {

    public:ReturnType FunctionName() {

    }};

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

    6/38

    Example

    Define a class of student that

    has a roll number. This classshould have a function thatcan be used to set the rollnumber

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

    7/38

    Example

    class Student{

    int rollNo;

    public:void setRollNo(int aRollNo){

    rollNo = aRollNo;

    }};

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

    8/38

    Function Outside Class Body

    class ClassName{

    public:

    ReturnTypeFunctionName();};

    ReturnTypeClassName::FunctionName()

    {

    }Scope

    resolution

    operator

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

    9/38

    Example

    class Student{

    int rollNo;

    public:void setRollNo(int aRollNo);

    };

    void Student::setRollNo(int aRollNo){

    rollNo = aRollNo;

    }

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

    10/38

    Inline Functions

    Instead of calling an inline function compilerreplaces the code at the function call point

    Keyword inline is used to request compilerto make a function inline

    It is a request and not a command

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

    11/38

    Example

    inline int Area(int len, int hi)

    {

    return len * hi;

    }

    int main()

    {

    cout

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

    12/38

    Inline Functions

    If we define the function inside the classbody then the function is by default aninline function

    In case function is defined outside the classbody then we must use the keyword inlineto make a function inline

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

    13/38

    Example

    class Student{

    int rollNo;

    public:

    void setRollNo(int aRollNo){

    rollNo = aRollNo;

    }

    };

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

    14/38

    Example

    class Student{

    public:

    inline void setRollNo(int aRollNo);};

    void Student::setRollNo(int aRollNo){

    rollNo = aRollNo;

    }

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

    15/38

    Example

    class Student{

    public:

    void setRollNo(int aRollNo);};

    inline void Student::setRollNo(intaRollNo){

    rollNo = aRollNo;

    }

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

    16/38

    Example

    class Student{

    public:

    inline void setRollNo(int aRollNo);};

    inline void Student::setRollNo(intaRollNo){

    rollNo = aRollNo;

    }

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

    17/38

    Constructor

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

    18/38

    Constructor

    Constructor is used to initialize the objectsof a class

    Constructor is used to ensure that object isin well defined state at the time of creation

    Constructor is automatically called when theobject is created

    Constructor are not usually called explicitly

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

    19/38

    Constructor (contd.)

    Constructor is a special function havingsame name as the class name

    Constructor does not have return type

    Constructors are commonly public members

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

    20/38

    Example

    class Student{

    public:

    Student(){

    rollNo = 0;

    }

    };

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

    21/38

    Example

    int main()

    {

    Student aStudent;/*constructor is implicitly

    called at this point*/

    }

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

    22/38

    Default Constructor

    Constructor without any argument is calleddefault constructor

    If we do not define a default constructor thecompiler will generate a default constructor

    This compiler generated default constructorinitialize the data members to their defaultvalues

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

    23/38

    Example

    class Student

    {

    int rollNo;

    char *name;

    float GPA;

    public:

    //no constructors

    };

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

    24/38

    Example

    Compiler generated default constructor

    {

    rollNo = 0;GPA = 0.0;

    name = NULL;

    }

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

    25/38

    Constructor Overloading

    Constructors can have parameters

    These parameters are used to initialize thedata members with user supplied data

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

    26/38

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

    27/38

    Example

    Student::Student(int aRollNo,

    char * aName){

    if(aRollNo < 0){

    rollNo = 0;}

    else {

    rollNo = aRollNo;

    }

    }

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

    28/38

    Example

    int main()

    {

    Student student1;

    Student student2(Name);

    Student student3(Name, 1);

    Student student4(Name,1,4.0);}

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

    29/38

    Constructor Overloading

    Use default parameter value to reduce thewriting effort

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

    30/38

    Example

    Student::Student( char * aName = NULL,

    int aRollNo= 0,

    float aGPA = 0.0){

    }

    Is equivalent to

    Student();

    Student(char * aName);

    Student(char * aName, int aRollNo);

    Student(char * Name, int aRollNo, floataGPA);

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

    31/38

    Copy Constructor

    Copy constructor are used when:

    Initializing an object at the time of creation

    When an object is passed by value to a function

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

    32/38

    Example

    void func1(Student student){

    }

    int main(){

    Student studentA;

    Student studentB = studentA;

    func1(studentA);

    }

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

    33/38

    Copy Constructor (Syntax)

    Student::Student(

    const Student &obj){

    rollNo = obj.rollNo;

    name = obj.name;GPA = obj.GPA;

    }

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

    34/38

    Shallow Copy

    When we initialize one object with anotherthen the compiler copies state of one objectto the other

    This kind of copying is called shallowcopying

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

    35/38

    Example

    Student studentA;

    Student studentB = studentA;

    Name

    GPA

    RollNo

    studentA

    Name

    GPA

    RollNo

    studentBA

    H

    MA

    D

    Memory

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

    36/38

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

    37/38

    Copy Constructor (contd.)

    Copy constructor is normally used toperform deep copy

    If we do not make a copy constructor thenthe compiler performs shallow copy

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

    38/38

    Example

    Name

    GPARollNo

    A

    Name

    GPARollNo

    B

    A

    H

    MA

    D

    Memory

    A

    H

    M

    A

    D

    Student studentA;

    Student studentB = studentA;