Computer Notes - Class II

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Class II

    1/32

    Class IIClass II

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    2/32

    ReviewReview

    this Pointerthis Pointer

    Separation of interface and implementationSeparation of interface and implementation

    Constant member functionsConstant member functions

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    3/32

    ProblemProblem

    Change the class Student suchChange the class Student suchthat a student is given a rollthat a student is given a roll

    number when the object is creatednumber when the object is createdand cannot be changed afterwardsand cannot be changed afterwards

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    4/32

    Student ClassStudent Classclass Student{class Student{

    intint rollNorollNo;;

    public:public:

    Student(intStudent(int aNoaNo););intint getRollNogetRollNo();();

    voidvoidsetRollNo(intsetRollNo(int aNoaNo););

    };};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    5/32

    Modified Student ClassModified Student Classclass Student{class Student{

    constconst intint rollNorollNo;;

    public:public:

    Student(intStudent(int aNoaNo););intint getRollNogetRollNo();();

    voidvoidsetRollNo(intsetRollNo(int aNoaNo););

    };};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    6/32

    ExampleExample

    Student::Student(intStudent::Student(int aRollNoaRollNo))

    {{

    rollNorollNo == aRollNoaRollNo;;

    /*error: cannot modify a/*error: cannot modify a

    constant data member*/constant data member*/

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    7/32

    ExampleExamplevoidvoidStudent::SetRollNo(intStudent::SetRollNo(int i)i)

    {{

    rollNorollNo = i;= i;

    /*error: cannot modify a constant/*error: cannot modify a constantdata member*/data member*/

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    8/32

    Member Initializer ListMember Initializer List

    A member initializer list is a mechanism toA member initializer list is a mechanism to

    initialize data membersinitialize data members

    It is given after closing parenthesis ofIt is given after closing parenthesis of

    parameter list of constructorparameter list of constructorIn case of more then one member useIn case of more then one member use

    comma separated listcomma separated list

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    9/32

    ExampleExampleclass Student{class Student{

    const intconst int rollNorollNo;;char *name;char *name;

    float GPA;float GPA;

    public:public:

    Student(intStudent(int aRollNoaRollNo))

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

    }}

    };};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    10/32

    Order of InitializationOrder of Initialization

    Data member are initialized in order they areData member are initialized in order they are

    declareddeclared

    Order in member initializer list is not significantOrder in member initializer list is not significant

    at allat all

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    11/32

    ExampleExampleclass ABC{class ABC{

    intint x;x;

    intint y;y;

    intint z;z;public:public:

    ABC();ABC();};};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    12/32

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

    {{

    }}/*/* x = Junk valuex = Junk value

    y = 10y = 10z = 10z = 10 */*/

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    13/32

    constconst ObjectsObjects

    Objects can be declared constantObjects can be declared constantwith the use of const keywordwith the use of const keyword

    Constant objects cannot changeConstant objects cannot changetheir statetheir state

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    14/32

    ExampleExampleintintmain()main()

    {{const Studentconst Student aStudentaStudent;;

    return 0;return 0;

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    15/32

    ExampleExampleclass Student{class Student{

    intint rollNorollNo;;

    public:public:

    intint getRollNogetRollNo(){(){

    returnreturn rollNorollNo;;

    }}};};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    16/32

    ExampleExampleintintmain(){main(){

    const Studentconst Student aStudentaStudent;;intint a =a = aStudent.getRollNoaStudent.getRollNo();();

    //error//error

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    17/32

    constconst ObjectsObjects

    constconst objects cannot accessobjects cannot accessnonnon

    constconstmember functionmember function

    Chances of unintentionalChances of unintentionalmodification are eliminatedmodification are eliminated

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    18/32

    ExampleExampleclass Student{class Student{

    intint rollNorollNo;;

    public:public:

    intint getRollNo()constgetRollNo()const{{

    returnreturn rollNorollNo;;

    }}};};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    19/32

    ExampleExampleintintmain(){main(){

    const Studentconst Student aStudentaStudent;;

    intint a =a = aStudent.getRollNoaStudent.getRollNo();();

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    20/32

    Constant data membersConstant data members

    Make all functions that donMake all functions that dont change the state oft change the state of

    the object constantthe object constantThis will enable constant objects to access moreThis will enable constant objects to access more

    member functionsmember functions

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    21/32

    Static VariablesStatic Variables

    Lifetime of static variable isLifetime of static variable is

    throughout the program lifethroughout the program life

    If static variables are not explicitlyIf static variables are not explicitly

    initialized then they are initialized to 0initialized then they are initialized to 0

    of appropriate typeof appropriate type

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    22/32

    ExampleExamplevoid func1(int i){void func1(int i){

    static intstatic int staticIntstaticInt = i;= i;cout

  • 8/3/2019 Computer Notes - Class II

    23/32

    Static Data MemberStatic Data Member

    DefinitionDefinition

    A variable that is part of a class,A variable that is part of a class,

    yet is not part of an object of thatyet is not part of an object of thatclass, is called static data memberclass, is called static data member

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    24/32

    Static Data MemberStatic Data Member

    They are shared by all instances ofThey are shared by all instances of

    the classthe class

    They do not belong to anyThey do not belong to anyparticular instance of a classparticular instance of a class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    25/32

    Class vs. Instance VariableClass vs. Instance Variable

    Student sStudent s11,, s2s2,, s3s3;;

    Class Space

    s1(rollNo,)

    s2(rollNo,)

    s3(rollNo,)

    Instance VariableClass

    Variable

  • 8/3/2019 Computer Notes - Class II

    26/32

    Static Data Member (Syntax)Static Data Member (Syntax)

    Keyword static is used to make aKeyword static is used to make adata member staticdata member static

    classclass ClassNameClassName{{

    staticstatic DataTypeDataTypeVariableNameVariableName;;

    };};http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    27/32

    Defining Static Data MemberDefining Static Data Member

    Static data member is declaredStatic data member is declared

    inside the classinside the class

    But they are defined outside theBut they are defined outside the

    classclass

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    28/32

    Defining Static Data MemberDefining Static Data Memberclassclass ClassNameClassName{{

    staticstatic DataTypeDataTypeVariableNameVariableName;;

    };};

    DataTypeDataType ClassName::VariableNameClassName::VariableName;;

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    29/32

    Initializing Static Data MemberInitializing Static Data Member

    Static data members should beStatic data members should be

    initialized once at file scopeinitialized once at file scope

    They are initialized at the time ofThey are initialized at the time ofdefinitiondefinition

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    30/32

    ExampleExampleclass Student{class Student{

    private:private:static intstatic int noOfStudentsnoOfStudents;;

    public:public:

    };};

    intint Student::noOfStudentsStudent::noOfStudents = 0;= 0;

    /*private static member cannot be accessed/*private static member cannot be accessedoutside the class except foroutside the class except forinitialization*/initialization*/

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    31/32

    Initializing Static Data MemberInitializing Static Data Member

    If static data members are notIf static data members are not

    explicitly initialized at the time ofexplicitly initialized at the time of

    definition then they are initializeddefinition then they are initialized

    to 0to 0

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Class II

    32/32

    ExampleExampleintint Student::noOfStudentsStudent::noOfStudents;;

    is equivalent tois equivalent to

    intint Student::noOfStudentsStudent::noOfStudents=0;=0;

    http://ecomputernotes.com