Object Oriented Programming (OOP) Lecture No. 9
Object Oriented Programming (OOP) Lecture No. 9
Object Oriented Programming (OOP) Lecture No. 9
(OOP)
Lecture No. 9
Review
►Member functions implementation
►Constructors
►Constructors overloading
►Copy constructors
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
Example
void func1(Student student){
…
}
int main(){
Student studentA(“Ahmad”);
Student studentB = studentA;
func1(studentA);
}
Copy Constructor (contd.)
Student::Student(
const Student &obj){
rollNo = obj.rollNo;
name = obj.name;
GPA = obj.GPA;
}
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
Example
Student studentA(“Ahmad”);
Student studentB = studentA;
A
H
studentA M studentB
A
RollNn D
RollNn
Name Name
Heap
GPA GPA
Example
int main(){
Student studentA(“Ahmad”,1);
{
Student studentB = studentA;
A
H
studentA M studentB
A
RollNn D
RollNn
Name Name
Heap
GPA GPA
Example
int main(){
Student studentA(“Ahmad”,1);
{
Student studentB = studentA;
}
}
studentA
RollNn
Name
Heap
GPA
Copy Constructor (contd.)
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
int main(){
Student studentA(“Ahmad”,1);
{
Student studentB = studentA;
A
H
M studentB
studentA A
D RollNn
RollNn
A Name
Name H GPA
GPA M
A
D
Example
int main(){
Student studentA(“Ahmad”,1);
{
Student studentB = studentA;
}
} A
H
M
studentA A
RollNn D
Name
Heap
GPA
Copy Constructor (contd.)
►Destructors cannot be
overloaded
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
Example
Student::Student(char * aName){
…
cout << aName << “Cons\n”;
}
Student::~Student(){
cout << name << “Dest\n”;
}
};
Example
int main()
{
Student studentB(“Ali”);
Student studentA(“Ahmad”);
return 0;
}
Example
Output:
Ali Cons
Ahmad Cons
Ahmad Dest
Ali Dest
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
Example – Accessing Data
Member
class Student{
…
int rollNo;
public:
void setRollNo(int aRollNo){
rollNo = aRollNo;
}
};
Example – Avoiding Error
void Student::setRollNo(int aRollNo){
if(aRollNo < 0){
rollNo = 0;
}
else
{
rollNo = aRollNo;
}
}
Example - Getter
class Student{
…
int rollNo;
public:
int getRollNo(){
return rollNo;
}
};
this
class Student{
Pointer
int rollNo;
char *name;
float GPA;
public:
int getRollNo();
void setRollNo(int aRollNo);
…
};
this Pointer
►The compiler reserves space for the
functions defined in the class
►Space for data is not allocated (since
no object is yet created)
Function Space
getRollNo(), …
this Pointer
►Student s1, s2, s3;
s2(rollNo,…)
Function Space
getRollNo(), … s3(rollNo,…)
s1(rollNo,…)
this Pointer
►Function space is common for
every variable
►Whenever a new object is created:
Memory is reserved for variables
only
Previously defined functions are
used over and over again
this Pointer
►Memory layout for objects
created:s1
rollNo, …
s2
rollNo, …
s3
rollNo, …
s4
rollNo, …
Function Space
getRollNo(), …
s1 s2 s3 s4
rollNo, … rollNo, … rollNo, … rollNo, …
address address address address
is internally represented as
void Student::setName(char *,
const Student *)
Declaration of this
DataType * const this;
Compiler Generated Code
Student::Student(){
rollNo = 0;
}
Student::Student(){
this->rollNo = 0;
}