0% found this document useful (0 votes)
5 views

Lab 5 - Classes & Objects Part 3

Uploaded by

mayadanasr996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 5 - Classes & Objects Part 3

Uploaded by

mayadanasr996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 5

Classes & Objects 2

1
Table of Contents

• Copy Constructor
• Default Copy Constructor
• User Defined Copy Constructor
• Try it Yourself

2
 Copy Constructor
• Default Copy Constructor
• Like any constructor, but it takes another object of the same class as a parameter
• Each class has a default copy constructor.
• Copy constructor can be overloaded.

int main()
• The default copy constructor
class Student { takes an object of the same Class
{ Student s1 = Student();
public: s1.id = 100;
int id; s1.name = "Ahmed";
string name;
}; Student s2 = Student(s1);

cout << s2.id << endl << s2.name << endl;


}

• The default copy constructor will


initialize the members of s2 by
the values of s1 member’s
• The output will be :
• 100
• Ahmed 3
 Copy Constructor
• User Defined Copy Constructor
• You can override the copy constructor, and customize it.
• Copy constructor takes a reference to an object of the same class as a const parameter to ensure that
the parameter can’t be modified.
• class
When youStudent
override the copy constructor, you should overload the default constructor.
{ • To initialize an object from the int main()
public: same class (like s1 in this sample {
code ) without the copy
int id; constructor, you should override Student
s1 = Student();
string name; the default constructor s1.id =
1;
s1.name = "Ahmed";
//Default constructor
Student() { Student s2 = Student(s1);
id = 0;
name = ""; cout << s2.id << endl << s2.name << endl;
} }

// Copy constructor
Student(const Student& s) { • Our copy constructor will initialize
id = s.id; the members of s2 by the values
• The user defined copy constructor of s1 member’s
name = s.name; • The output will be :
takes a const referenced object of
} the same Class • 100
}; • Ahmed 4
TRY IT YOURSELF

• Create class called Rectangle with the following requirements:


• Make 2 private int variable called int height, int width
• Override the default constructor to set height and width values.
• Rectangle(int height, int width)
• Write the copy constructor:
• Rectangle(const Rectangle& r)
• Create public method called GetHeight() that returns height’s value
• Create public method called GetWidth() that returns width’s value
• In main function
• Create 2 objects from Rectangle class r1 and r2.
• Initialize r1 from the normal constructor.
• Initialize r2 from the copy constructor with r1 value.
• Display height and width for r1 and r2.

5
TRY IT YOURSELF
• Solution
class Rectangle
{
private:
int height, width; int main()
public: {
Rectangle(int height, int width) { Rectangle r1 = Rectangle(10,15);
this->height = height;
this->width= width ; cout << r1.GetHeight() << endl <<
} r1.GetWidth() << endl;

Rectangle(const Rectangle& r) { Rectangle r2 = Rectangle(r1);


height = r.height;
width = r.width; cout << r2.GetHeight() << endl <<
} r2.GetWidth() << endl;
}
int GetHeight() {
return height;
}

int GetWidth() {
return width;
}
}; 6
THANK YOU

You might also like