Computer Notes - Class II

0

No comments posted yet

Comments

Slide 1

Class II http://ecomputernotes.com

Slide 2

Review this Pointer Separation of interface and implementation Constant member functions http://ecomputernotes.com

Slide 3

Problem Change the class Student such that a student is given a roll number when the object is created and cannot be changed afterwards http://ecomputernotes.com

Slide 4

Student Class class Student{ … int rollNo; public: Student(int aNo); int getRollNo(); void setRollNo(int aNo); … }; http://ecomputernotes.com

Slide 5

Modified Student Class class Student{ … const int rollNo; public: Student(int aNo); int getRollNo(); void setRollNo(int aNo); … }; http://ecomputernotes.com

Slide 6

Example Student::Student(int aRollNo) { rollNo = aRollNo; /*error: cannot modify a constant data member*/ } http://ecomputernotes.com

Slide 7

Example void Student::SetRollNo(int i) { rollNo = i; /*error: cannot modify a constant data member*/ } http://ecomputernotes.com

Slide 8

Member Initializer List A member initializer list is a mechanism to initialize data members It is given after closing parenthesis of parameter list of constructor In case of more then one member use comma separated list http://ecomputernotes.com

Slide 9

Example class Student{ const int rollNo; char *name; float GPA; public: Student(int aRollNo) : rollNo(aRollNo), name(Null), GPA(0.0){ … } … }; http://ecomputernotes.com

Slide 10

Order of Initialization Data member are initialized in order they are declared Order in member initializer list is not significant at all http://ecomputernotes.com

Slide 11

Example class ABC{ int x; int y; int z; public: ABC(); }; http://ecomputernotes.com

Slide 12

Example ABC::ABC():y(10),x(y),z(y) { … } /* x = Junk value y = 10 z = 10 */ http://ecomputernotes.com

Slide 13

const Objects Objects can be declared constant with the use of const keyword Constant objects cannot change their state http://ecomputernotes.com

Slide 14

Example int main() { const Student aStudent; return 0; } http://ecomputernotes.com

Slide 15

Example class Student{ … int rollNo; public: … int getRollNo(){ return rollNo; } }; http://ecomputernotes.com

Slide 16

Example int main(){ const Student aStudent; int a = aStudent.getRollNo(); //error } http://ecomputernotes.com

Slide 17

const Objects const objects cannot access “non const” member function Chances of unintentional modification are eliminated http://ecomputernotes.com

Slide 18

Example class Student{ … int rollNo; public: … int getRollNo()const{ return rollNo; } }; http://ecomputernotes.com

Slide 19

Example int main(){ const Student aStudent; int a = aStudent.getRollNo(); } http://ecomputernotes.com

Slide 20

Constant data members Make all functions that don’t change the state of the object constant This will enable constant objects to access more member functions http://ecomputernotes.com

Slide 21

Static Variables Lifetime of static variable is throughout the program life If static variables are not explicitly initialized then they are initialized to 0 of appropriate type http://ecomputernotes.com

Slide 22

Example void func1(int i){ static int staticInt = i; cout << staticInt << endl; } int main(){ func1(1); func1(2); } Output: 1 1 http://ecomputernotes.com

Slide 23

Static Data Member Definition “A variable that is part of a class, yet is not part of an object of that class, is called static data member” http://ecomputernotes.com

Slide 24

Static Data Member They are shared by all instances of the class They do not belong to any particular instance of a class http://ecomputernotes.com

Slide 25

Class vs. Instance Variable Student s1, s2, s3; Instance Variable Class Variable

Slide 26

Static Data Member (Syntax) Keyword static is used to make a data member static class ClassName{ … static DataType VariableName; }; http://ecomputernotes.com

Slide 27

Defining Static Data Member Static data member is declared inside the class But they are defined outside the class http://ecomputernotes.com

Slide 28

Defining Static Data Member class ClassName{ … static DataType VariableName; }; DataType ClassName::VariableName; http://ecomputernotes.com

Slide 29

Initializing Static Data Member Static data members should be initialized once at file scope They are initialized at the time of definition http://ecomputernotes.com

Slide 30

Example class Student{ private: static int noOfStudents; public: … }; int Student::noOfStudents = 0; /*private static member cannot be accessed outside the class except for initialization*/ http://ecomputernotes.com

Slide 31

Initializing Static Data Member If static data members are not explicitly initialized at the time of definition then they are initialized to 0 http://ecomputernotes.com

Slide 32

Example int Student::noOfStudents; is equivalent to int Student::noOfStudents=0; http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Class in Object oriented Programming what is Class OOPs Explain about it in detail .explain it with example

Tags: computer notes - class ii

URL:
More by this User
Most Viewed