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

Embed Size (px)

Citation preview

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

    1/35

    Object Oriented Programming

    (OOP)Lecture No. 12

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

    2/35

    Review

    Constant data members

    Constant objects

    Static data members

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

    3/35

    Static Data Member

    Definition

    A variable that is part of a class, yet is notpart of an object of that class, is calledstatic data member

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

    4/35

    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 12

    5/35

    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 12

    6/35

    Static Data Member (Syntax)

    Keyword static is used to make a datamember static

    class ClassName{

    static DataType VariableName;

    };

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

    7/35

    Defining Static Data Member

    Static data member is declared inside theclass

    But they are defined outside the class

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

    8/35

    Defining Static Data Member

    class ClassName{

    static DataType VariableName;

    };

    DataType ClassName::VariableName;

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

    9/35

    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 12

    10/35

    Example

    class Student{

    private:

    static int noOfStudents;

    public:

    };

    int Student::noOfStudents= 0;

    /*private static member cannot be accessed outside theclass except for initialization*/

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

    11/35

    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 12

    12/35

    Example

    int Student::noOfStudents;

    is equivalent to

    int Student::noOfStudents=0;

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

    13/35

    Accessing Static Data Member

    To access a static data member there aretwo ways

    Access like a normal data memberAccess using a scope resolution operator ::

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

    14/35

    Example

    class Student{

    public:

    static int noOfStudents;

    };

    int Student::noOfStudents;

    int main(){

    Student aStudent;

    aStudent.noOfStudents = 1;

    Student::noOfStudents = 1;

    }

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

    15/35

    Life of Static Data Member

    They are created even when there is noobject of a class

    They remain in memory even when allobjects of a class are destroyed

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

    16/35

    Example

    class Student{

    public:

    static int noOfStudents;

    };int Student::noOfStudents;

    int main(){

    Student::noOfStudents = 1;

    }

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

    17/35

    Example

    class Student{

    public:

    static int noOfStudents;

    };

    int Student::noOfStudents;

    int main(){

    {

    Student aStudent;

    aStudent.noOfStudents = 1;

    }

    Student::noOfStudents = 1;

    }

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

    18/35

    Uses

    They can be used to storeinformation that is required by

    all objects, like global variables

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

    19/35

    Example

    Modify the class Student suchthat one can know the number

    of student created in a system

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

    20/35

    Example

    class Student{

    public:

    static int noOfStudents;

    Student();~Student();

    };

    int Student::noOfStudents = 0;

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

    21/35

    Example

    Student::Student(){

    noOfStudents++;

    }Student::~Student(){

    noOfStudents--;

    }

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

    22/35

    Exampleint Student::noOfStudents = 0;

    int main(){cout

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

    23/35

    Problem

    noOfStudents is accessible outside the class

    Bad design as the local data member is keptpublic

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

    24/35

    Static Member Function

    Definition:

    The function that needs access to the

    members of a class, yet does not needto be invoked by a particular object, iscalled static member function

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

    25/35

    Static Member Function

    They are used to access static datamembers

    Access mechanism for static member

    functions is same as that of static datamembers

    They cannot access any non-static members

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

    26/35

    Example

    class Student{

    static int noOfStudents;

    int rollNo;

    public:

    static int getTotalStudent(){

    return noOfStudents;}

    };

    int main(){

    int i = Student::getTotalStudents();

    }

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

    27/35

    Accessing non static data members

    int Student::getTotalStudents(){

    return rollNo;

    }

    int main(){

    int i = Student::getTotalStudents();

    /*Error: There is no instance of Student,

    rollNo cannot be accessed*/

    }

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

    28/35

    this Pointer

    thispointer is passed implicitly to memberfunctions

    thispointer is not passed to static memberfunctions

    Reason is static member functions cannotaccess non static data members

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

    29/35

    Global Variable vs. Static Members

    Alternative to static member is to useglobal variable

    Global variables are accessible to allentities of the program

    Against information hiding

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

    30/35

    Array of Objects

    Array of objects can only be created if anobject can be created without supplying anexplicit initializer

    There must always be a default constructorif we want to create array of objects

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

    31/35

    Example

    class Test{

    public:

    };

    int main(){

    Test array[2]; // OK

    }

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

    32/35

    Example

    class Test{

    public:

    Test();

    };int main(){

    Test array[2]; // OK

    }

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

    33/35

    Example

    class Test{

    public:

    Test(int i);

    };int main(){

    Test array[2]; // Error

    }

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

    34/35

    Example

    class Test{public:

    Test(int i);

    }

    int main(){

    Test array[2] ={Test(0),Test(0)};

    }

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

    35/35