![Page 1: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/1.jpg)
CSC241 Object-Oriented Programming (OOP)
Lecture No. 5
![Page 2: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/2.jpg)
Review
Member functions implementation
ConstructorsConstructors overloadingCopy constructors
![Page 3: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/3.jpg)
Copy Constructor
Copy constructor are used when: Initializing an object at
the time of creation When an object is
passed by value to a function
![Page 4: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/4.jpg)
Example
void func1(Student student){
…
}
int main(){
Student studentA(“Ahmad”);
Student studentB = studentA;
func1(studentA);
}
![Page 5: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/5.jpg)
Copy Constructor (contd.)
Student::Student(const Student &obj){
rollNo = obj.rollNo;
name = obj.name;
GPA = obj.GPA;
}
![Page 6: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/6.jpg)
Shallow Copy
When we initialize one object with another then the compiler copies state of one object to the other
This kind of copying is called shallow copying
![Page 7: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/7.jpg)
Example: Shallow Copy
Student studentA(“Ahmad”);
GPA
Name
RollNo
studentB
Heap
AHMAD
GPA
Name
RollNo
studentA
Student studentB = studentA;
![Page 8: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/8.jpg)
Example: Shallow Copyint main(){
Student studentA(“Ahmad”, 1); {
Student studentB = studentA;
GPA
Name
RollNo
studentB
Heap
AHMAD
GPA
Name
RollNo
studentA
![Page 9: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/9.jpg)
int main(){
Student studentA(“Ahmad”, 1); {
Student studentB = studentA;}
}
HeapGPA
Name
RollNo
studentA
Example: Shallow Copy
![Page 10: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/10.jpg)
Student::Student(const Student &obj){
int len = strlen(obj.name);
name = new char[len + 1]
strcpy(name, obj.name);
…
/*copy rest of the data members*/
}
Example: Deep Copy
![Page 11: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/11.jpg)
int main(){
Student studentA(“Ahmad”, 1); {
Student studentB = studentA;
AHMAD
GPA
Name
RollNo
studentA
GPA
Name
RollNo
studentB
AHMAD
Example: Deep Copy
![Page 12: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/12.jpg)
int main(){Student studentA(“Ahmad”, 1);
{Student studentB = studentA;
}}
HeapGPA
Name
RollNo
studentA
AHMAD
Example: Deep Copy
![Page 13: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/13.jpg)
Copy Constructor (contd.)
Copy constructor is normally used to perform deep copy
If we do not make a copy constructor then the compiler performs shallow copy
![Page 14: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/14.jpg)
Destructor
Destructor is used to free memory that is allocated through dynamic allocation
Destructor is used to perform house keeping operations
![Page 15: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/15.jpg)
Destructor (contd.)
Destructor is a function with the same name as that of class, but preceded with a tilde ‘~’
![Page 16: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/16.jpg)
Example
class Student
{
…
public:
~Student(){
if (name){
delete[]name;
}
}
}
![Page 17: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/17.jpg)
Overloading
Destructors cannot be overloaded
![Page 18: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/18.jpg)
Sequence of Calls
Constructors and destructors are called automatically
Constructors are called in the sequence in which object is declared
Destructors are called in reverse order
![Page 19: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/19.jpg)
Example
Student::Student(char * aName){
…
cout << aName << “Cons\n”;
}
Student::~Student(){
cout << name << “Dest\n”;
}
![Page 20: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/20.jpg)
Example
int main()
{
Student studentB(“Ali”);
Student studentA(“Ahmad”);
return 0;
}
![Page 21: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/21.jpg)
Example
Output:Ali ConsAhmad ConsAhmad DestAli Dest
![Page 22: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/22.jpg)
Accessor Functions
Usually the data member are defined in private part of a class – information hiding
Accessor functions are functions that are used to access these private data members
Accessor functions also useful in reducing error
![Page 23: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/23.jpg)
Example – Accessing Data Memberclass Student{
…
int rollNo;
public:
void setRollNo(int aRollNo){
rollNo = aRollNo;
}
};
![Page 24: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/24.jpg)
Example – Avoiding Error
void Student::setRollNo(int aRollNo){
if (aRollNo < 0){
rollNo = 0;
}
else
{
rollNo = aRollNo;
}
}
![Page 25: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/25.jpg)
Example - Getter
class Student{
…
int rollNo;
public:
int getRollNo(){
return rollNo;
}
};
![Page 26: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/26.jpg)
this Pointerclass Student{
int rollNo;
char *name;
float GPA;
public:
int getRollNo();
void setRollNo(int aRollNo);
…
};
![Page 27: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/27.jpg)
this PointerThe compiler reserves
space for the functions defined in the class
Space for data is not allocated (since no object is yet created)
Function SpacegetRollNo(), …
![Page 28: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/28.jpg)
this PointerStudent s1, s2, s3;
Function SpacegetRollNo(), …
s1(rollNo,…)
s2(rollNo,…)
s3(rollNo,…)
![Page 29: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/29.jpg)
this PointerFunction space is common
for every variableWhenever a new object is
created:Memory is reserved for
variables onlyPreviously defined functions
are used over and over again
![Page 30: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/30.jpg)
this PointerMemory layout for objects
created:s1rollNo, …
Function SpacegetRollNo(), …
s2rollNo, …
s3rollNo, …
s4rollNo, …
• How does the functions know on which object to act?
![Page 31: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/31.jpg)
this Pointer Address of each object is passed to the calling
function
This address is deferenced by the functions and hence they act on correct objects
address
s1rollNo, …
s2rollNo, …
s3rollNo, …
s4rollNo, …
address address address
• The variable containing the “self-address” is called this pointer
![Page 32: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/32.jpg)
Passing this Pointer
Whenever a function is called the this pointer is passed as a parameter to that function
Function with n parameters is actually called with n+1 parameters
![Page 33: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/33.jpg)
Example
void Student::setName(char *)
is internally represented as
void Student::setName(char *, const Student *)
![Page 34: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/34.jpg)
Declaration of this
DataType * const this;
![Page 35: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/35.jpg)
Compiler Generated CodeStudent::Student(){
rollNo = 0;
}
Student::Student(){
this->rollNo = 0;
}
![Page 36: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/36.jpg)
Review
Copy constructorsDestructorAccessor Functionsthis Pointer
![Page 37: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/37.jpg)
this PointerThere are situations where designer wants to return reference to current object from a function
In such cases reference is taken from this pointer like (*this)
![Page 38: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/38.jpg)
Example
Student Student::setRollNo(int aNo)
{
…
return *this;
}
Student Student::setName(char *aName)
{
…
return *this;
}
![Page 39: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/39.jpg)
Example
int main()
{
Student aStudent;
Student bStudent;
bStudent = aStudent.setName(“Ahmad”);
…
bStudent = aStudent.setName(“Ali”).setRollNo(2);
return 0;
}
![Page 40: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/40.jpg)
Separation of interface and implementation
Public member function exposed by a class is called interface
Separation of implementation from the interface is good software engineering
![Page 41: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/41.jpg)
Complex Number
There are two representations of complex number Euler form
z = x + i y Phasor form
z = |z| (cos + i sin )z is known as the complex
modulus and is known as the complex argument or phase
![Page 42: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/42.jpg)
Example
float getX()float getY()void setNumber (float i, float j)…
float x float y
Complex
Old implementation
float getX()float getY()void setNumber (float i, float j)…
float z float theta
Complex
New implementation
![Page 43: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/43.jpg)
Example
class Complex{ //old
float x;
float y;
public:
void setNumber(float i, float j){
x = i;
y = j;
}
…
};
![Page 44: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/44.jpg)
Example
class Complex{ //new
float z;
float theta;
public:
void setNumber(float i, float j){
z = sqrt((i ^ 2) + (j ^ 2));
theta = arctan(j / i);
}
…
};
![Page 45: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/45.jpg)
Advantages
User is only concerned about ways of accessing data (interface)
User has no concern about the internal representation and implementation of the class
![Page 46: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/46.jpg)
Separation of interface and implementation
Usually functions are defined in implementation files (.cpp) while the class definition is given in header file (.h)
Some authors also consider this as separation of interface and implementation
![Page 47: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/47.jpg)
Student.h
class Student{
int rollNo;
public:
void setRollNo(int aRollNo);
int getRollNo();
…
};
![Page 48: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/48.jpg)
Student.cpp
#include “student.h”
void Student::setRollNo(int aNo){
…
}
int Student::getRollNo(){
…
}
![Page 49: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/49.jpg)
Driver.cpp
#include “student.h”
int main(){
Student aStudent;
}
![Page 50: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/50.jpg)
const Member Functions
There are functions that are meant to be read only
There must exist a mechanism to detect error if such functions accidentally change the data member
![Page 51: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/51.jpg)
const Member Functions
Keyword const is placed at the end of the parameter list
![Page 52: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/52.jpg)
const Member FunctionsDeclaration:
class ClassName{
ReturnVal Function() const;
};
Definition:
ReturnVal ClassName::Function() const{
…
}
![Page 53: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/53.jpg)
Example
class Student{
public:
int getRollNo() const{
return rollNo;
}
};
![Page 54: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/54.jpg)
const Functions
Constant member functions cannot modify the state of any object
They are just “read-only”Errors due to typing are
also caught at compile time
![Page 55: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/55.jpg)
Example
bool Student::isRollNo(int aNo){
if (rollNo == aNo){
return true;
}
return false;
}
![Page 56: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/56.jpg)
Example
bool Student::isRollNo(int aNo){
/*undetected typing mistake*/
if (rollNo = aNo){
return true;
}
return false;
}
![Page 57: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/57.jpg)
Example
bool Student::isRollNo(int aNo)const{
/*compiler error*/
if (rollNo = aNo){
return true;
}
return false;
}
![Page 58: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/58.jpg)
const Functions
Constructors and Destructors cannot be const
Constructor and destructor are used to modify the object to a well defined state
![Page 59: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/59.jpg)
Example
class Time{
public:
Time() const {} //error…
~Time() const {} //error…
};
![Page 60: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/60.jpg)
const Function
Constant member function cannot change data member
Constant member function cannot access non-constant member functions
![Page 61: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/61.jpg)
Example
class Student{
char * name;
public:
char *getName();
void setName(char * aName);
int ConstFunc() const{
name = getName();//error
setName(“Ahmad”);//error
}
};
![Page 62: CSC241 Object-Oriented Programming (OOP) Lecture No. 5](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697bff21a28abf838cbbeae/html5/thumbnails/62.jpg)
this Pointer and const Member Function
this pointer is passed as constant pointer to const data in case of constant member functions
const Student *const this;instead of
Student *const this;