



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























Review
Member functions implementation
Constructors
Constructors overloading
Copy constructors
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;
}
Example
Student studentA(“Ahmad”);
Heap
A H M A D
Student studentB = studentA;
int main(){
Student studentA(“Ahmad”,1); {
Student studentB = studentA;
Heap
A H M A D
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/
}
int main(){
Student studentA(“Ahmad”,1); {
Student studentB = studentA;
A H M A D
A H M A D
Copy Constructor (contd.)
Copy constructor is normally used to perform deep copy
If we do not make a copy constructor then the compilerperforms shallow copy
Destructor
Destructor is used to free memory that is allocated through dynamicallocation
Destructor is used to perform house keeping operations
Example
Overloading
►
Destructors cannot be
overloaded
Example
Example
int
main()
{
Student
studentB(“Ali”);
Student
studentA(“Ahmad”);
return
0;
}