csc241 object-oriented programming (oop) lecture no. 6
TRANSCRIPT
![Page 1: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/1.jpg)
CSC241 Object-Oriented Programming (OOP)
Lecture No. 6
![Page 2: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/2.jpg)
Review
this Pointer
Separation of interface and implementation
Constant member functions
![Page 3: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/3.jpg)
Problem
Change the class Student such that a student is given a roll number when the object is created and cannot be changed afterwards
![Page 4: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/4.jpg)
Student Class
class Student{
…
int rollNo;
public:
Student(int aNo);
int getRollNo();
void setRollNo(int aNo);
…
};
![Page 5: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/5.jpg)
Modified Student Class
class Student{
…
const int rollNo;
public:
Student(int aNo);
int getRollNo();
void setRollNo(int aNo);
…
};
![Page 6: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/6.jpg)
Example
Student::Student(int aRollNo)
{
rollNo = aRollNo;
/*error: cannot modify a constant data member*/
}
![Page 7: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/7.jpg)
Example
void Student::setRollNo(int i)
{
rollNo = i;
/*error: cannot modify a constant data member*/
}
![Page 8: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/8.jpg)
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
![Page 9: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/9.jpg)
Exampleclass Student{
const int rollNo;
char *name;
float GPA;
public:
Student(int aRollNo): rollNo(aRollNo), name(Null), GPA(0.0){
…
}
…
};
![Page 10: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/10.jpg)
Order of Initialization
Data member are initialized in order they are declared
Order in member initializer list is not significant at all
![Page 11: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/11.jpg)
Example
class ABC{
int x;
int y;
int z;
public:
ABC();
};
![Page 12: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/12.jpg)
Example
ABC::ABC() :y(10), x(y), z(y)
{
…
}
/*x = Junk value
y = 10
z = 10*/
![Page 13: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/13.jpg)
const Objects
Objects can be declared constant with the use of const keyword
Constant objects cannot change their state
![Page 14: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/14.jpg)
Example
int main()
{
const Student aStudent;
return 0;
}
![Page 15: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/15.jpg)
Example
class Student{
…
int rollNo;
public:
…
int getRollNo(){
return rollNo;
}
};
![Page 16: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/16.jpg)
Example
int main(){
const Student aStudent;
int a = aStudent.getRollNo();
//error
}
![Page 17: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/17.jpg)
const Objects
const objects cannot access “non const” member function
Chances of unintentional modification are eliminated
![Page 18: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/18.jpg)
Example
class Student{
…
int rollNo;
public:
…
int getRollNo()const{
return rollNo;
}
};
![Page 19: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/19.jpg)
Example
int main(){
const Student aStudent;
int a = aStudent.getRollNo();
}
![Page 20: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/20.jpg)
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
![Page 21: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/21.jpg)
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
![Page 22: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/22.jpg)
Example
void func1(int i){
static int staticInt = i;
cout << staticInt << endl;
}
int main(){
func1(1);
func1(2);
}
Output:11
![Page 23: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/23.jpg)
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”
![Page 24: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/24.jpg)
Static Data Member
They are shared by all instances of the class
They do not belong to any particular instance of a class
![Page 25: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/25.jpg)
Class vs. Instance Variable
Student s1, s2, s3;
Class Space
s1(rollNo,…)
s2(rollNo,…)
s3(rollNo,…)
Instance VariableClass Variable
![Page 26: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/26.jpg)
Static Data Member (Syntax)
Keyword static is used to make a data member static
class ClassName{
…
static DataType VariableName;
};
![Page 27: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/27.jpg)
Defining Static Data Member
Static data member is declared inside the class
But they are defined outside the class
![Page 28: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/28.jpg)
Defining Static Data Member
class ClassName{
…
static DataType VariableName;
};
DataType ClassName::VariableName;
![Page 29: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/29.jpg)
Initializing Static Data Member
Static data members should be initialized once at file scope
They are initialized at the time of definition
![Page 30: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/30.jpg)
Example
class Student{
private:
static int noOfStudents;
public:
…
};
int Student::noOfStudents = 0;
/*private static member cannot be accessed outside the class except for initialization*/
![Page 31: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/31.jpg)
Initializing Static Data Member
If static data members are not explicitly initialized at the time of definition then they are initialized to 0
![Page 32: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/32.jpg)
Example
int Student::noOfStudents;is equivalent to
int Student::noOfStudents = 0;
![Page 33: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/33.jpg)
Review
Constant data members
Constant objects
Static data members
![Page 34: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/34.jpg)
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”
![Page 35: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/35.jpg)
Static Data Member
They are shared by all instances of the class
They do not belong to any particular instance of a class
![Page 36: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/36.jpg)
Class vs. Instance Variable
Student s1, s2, s3;
Class Space
s1(rollNo,…)
s2(rollNo,…)
s3(rollNo,…)
Instance VariableClass Variable
![Page 37: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/37.jpg)
Static Data Member (Syntax)
Keyword static is used to make a data member static
class ClassName{
…
static DataType VariableName;
};
![Page 38: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/38.jpg)
Defining Static Data Member
Static data member is declared inside the class
But they are defined outside the class
![Page 39: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/39.jpg)
Defining Static Data Member
class ClassName{
…
static DataType VariableName;
};
DataType ClassName::VariableName;
![Page 40: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/40.jpg)
Initializing Static Data Member
Static data members should be initialized once at file scope
They are initialized at the time of definition
![Page 41: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/41.jpg)
Exampleclass Student{
private:
static int noOfStudents;
public:
…
};
int Student::noOfStudents = 0;
/*private static member cannot be accessed outside the class except for initialization*/
![Page 42: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/42.jpg)
Initializing Static Data Member
If static data members are not explicitly initialized at the time of definition then they are initialized to 0
![Page 43: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/43.jpg)
Example
int Student::noOfStudents;
is equivalent to
int Student::noOfStudents = 0;
![Page 44: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/44.jpg)
Accessing Static Data Member
To access a static data member there are two ways
Access like a normal data member
Access using a scope resolution operator ‘::’
![Page 45: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/45.jpg)
Example
class Student{
public:
static int noOfStudents;
};
int Student::noOfStudents;
int main(){
Student aStudent;
aStudent.noOfStudents = 1;
Student::noOfStudents = 1;
}
![Page 46: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/46.jpg)
Life of Static Data Member
They are created even when there is no object of a class
They remain in memory even when all objects of a class are destroyed
![Page 47: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/47.jpg)
Example
class Student{
public:
static int noOfStudents;
};
int Student::noOfStudents;
int main(){
Student::noOfStudents = 1;
}
![Page 48: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/48.jpg)
Example
class Student{
public:
static int noOfStudents;
};
int Student::noOfStudents;
int main(){
{
Student aStudent;
aStudent.noOfStudents = 1;
}
Student::noOfStudents = 1;
}
![Page 49: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/49.jpg)
Uses
They can be used to store information that is required by all objects, like global variables
![Page 50: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/50.jpg)
Example
Modify the class Student such that one can know the number of student created in a system
![Page 51: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/51.jpg)
Example
class Student{
…
public:
static int noOfStudents;
Student();
~Student();
…
};
int Student::noOfStudents = 0;
![Page 52: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/52.jpg)
Example
Student::Student(){
noOfStudents++;
}
Student::~Student(){
noOfStudents--;
}
![Page 53: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/53.jpg)
Example
int Student::noOfStudents = 0;
int main(){
cout << Student::noOfStudents << endl;
Student studentA;
cout << Student::noOfStudents << endl;
Student studentB;
cout << Student::noOfStudents << endl;
}
Output:012
![Page 54: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/54.jpg)
Problem
noOfStudents is accessible outside the class
Bad design as the local data member is kept public
![Page 55: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/55.jpg)
Static Member Function
Definition:
“The function that needs access to the members of a class, yet does not need to be invoked by a particular object, is called static member function”
![Page 56: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/56.jpg)
Static Member Function
They are used to access static data members Access mechanism for static member functions is
same as that of static data members They cannot access any non-static members
![Page 57: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/57.jpg)
Example
class Student{
static int noOfStudents;
int rollNo;
public:
static int getTotalStudent(){
return noOfStudents;
}
};
int main(){
int i = Student::getTotalStudents();
}
![Page 58: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/58.jpg)
Accessing non static data membersint Student::getTotalStudents(){
return rollNo;
}
int main(){
int i = Student::getTotalStudents();
/*Error: There is no instance of Student, rollNo cannot be
accessed*/
}
![Page 59: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/59.jpg)
this Pointer
this pointer is passed implicitly to member functions
this pointer is not passed to static member functions
Reason is static member functions cannot access non static data members
![Page 60: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/60.jpg)
Global Variable vs. Static Members
Alternative to static member is to use global variable
Global variables are accessible to all entities of the programAgainst information hiding
![Page 61: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/61.jpg)
Array of Objects
Array of objects can only be created if an object can be created without supplying an explicit initializer
There must always be a default constructor if we want to create array of objects
![Page 62: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/62.jpg)
Example
class Test{
public:
};
int main(){
Test array[2]; // OK
}
![Page 63: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/63.jpg)
Example
class Test{
public:
Test();
};
int main(){
Test array[2]; // OK
}
![Page 64: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/64.jpg)
Example
class Test{
public:
Test(int i);
};
int main(){
Test array[2]; // Error
}
![Page 65: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/65.jpg)
Example
class Test{
public:
Test(int i);
}
int main(){
Test array[2] = {Test(0), Test(0)};
}
![Page 66: CSC241 Object-Oriented Programming (OOP) Lecture No. 6](https://reader035.vdocuments.net/reader035/viewer/2022070403/56649f2c5503460f94c46d6c/html5/thumbnails/66.jpg)
Example
class Test{
public:
Test(int i);
}
int main(){
Test a(1), b(2);
Test array[2] = { a, b };
}