object oriented programming (oop) - cs304 power point slides lecture 11

Upload: sameer-hane

Post on 04-Jun-2018

284 views

Category:

Documents


1 download

TRANSCRIPT

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

    1/32

    Object Oriented Programming

    (OOP)Lecture No. 11

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

    2/32

    Review

    this Pointer

    Separation of interface and implementation

    Constant member functions

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

    3/32

    Problem

    Change the class Student suchthat a student is given a roll

    number when the object iscreated and cannot be changed

    afterwards

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

    4/32

    Student Class

    class Student{

    int rollNo;

    public:Student(int aNo);

    int getRollNo();

    void setRollNo(int aNo);

    };

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

    5/32

    Modified Student Class

    class Student{

    const int rollNo;

    public:Student(int aNo);

    int getRollNo();

    void setRollNo(int aNo);

    };

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

    6/32

    Example

    Student::Student(int aRollNo)

    {

    rollNo = aRollNo;/*error: cannot modify a

    constant data member*/

    }

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

    7/32

    Example

    void Student::SetRollNo(int i)

    {

    rollNo = i;/*error: cannot modify a

    constant data member*/

    }

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

    8/32

    Member Initializer List

    A member initializer list is a mechanismto initialize data members

    It is given after closing parenthesis ofparameter list of constructor

    In case of more then one member use

    comma separated list

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

    9/32

    Example

    class Student{

    const int rollNo;

    char *name;

    float GPA;

    public:

    Student(int aRollNo)

    : rollNo(aRollNo), name(Null), GPA(0.0){

    }

    };

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

    10/32

    Order of Initialization

    Data member are initialized in order theyare declared

    Order in member initializer list is notsignificant at all

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

    11/32

    Example

    class ABC{

    int x;

    int y;int z;

    public:

    ABC();};

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

    12/32

    Example

    ABC::ABC():y(10),x(y),z(y)

    {

    }

    /* x = Junk value

    y = 10z = 10 */

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

    13/32

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

    14/32

    Example

    int main()

    {

    const Student aStudent;

    return 0;

    }

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

    15/32

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

    16/32

    Example

    int main(){

    const Student aStudent;

    int a = aStudent.getRollNo();

    //error

    }

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

    17/32

    constObjects

    constobjects cannot access

    non constmember function

    Chances of unintentionalmodification are eliminated

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

    18/32

    Example

    class Student{

    int rollNo;

    public:

    int getRollNo()const{

    return rollNo;

    }

    };

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

    19/32

    Example

    int main(){

    const Student aStudent;

    int a = aStudent.getRollNo();}

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

    20/32

    Constant data members

    Make all functions that dont change thestate of the object constant

    This will enable constant objects to accessmore member functions

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

    21/32

    Static Variables

    Lifetime of static variable isthroughout the program life

    If static variables are not explicitlyinitialized then they are initializedto 0 of appropriate type

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

    22/32

    Example

    void func1(int i){

    static int staticInt = i;

    cout

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

    23/32

    Static Data Member

    Definition

    A variable that is part of aclass, yet is not part of anobject of that class, is called

    static data member

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

    24/32

    Static Data Member

    They are shared by allinstances of the class

    They do not belong to anyparticular instance of a class

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

    25/32

    Class vs. Instance Variable

    Student s1, s2, s3;

    Class Space

    s1(rollNo,)

    s2(rollNo,)

    s3(rollNo,)

    Instance VariableClassVariable

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

    26/32

    Static Data Member (Syntax)

    Keyword static is used to make adata member static

    class ClassName{

    static DataType VariableName;

    };

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

    27/32

    Defining Static Data Member

    Static data member is declaredinside the class

    But they are defined outside theclass

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

    28/32

    Defining Static Data Member

    class ClassName{

    static DataType VariableName;

    };

    DataType ClassName::VariableName;

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

    29/32

    Initializing Static Data Member

    Static data members should beinitialized once at file scope

    They are initialized at the timeof definition

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

    30/32

    Example

    class Student{

    private:

    static int noOfStudents;

    public:

    };

    int Student::noOfStudents= 0;/*private static member cannot be

    accessed outside the class except forinitialization*/

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

    31/32

    Initializing Static Data Member

    If static data members are notexplicitly initialized at the time

    of definition then they areinitialized to 0

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

    32/32

    Example

    int Student::noOfStudents;

    is equivalent to

    int Student::noOfStudents=0;