Computer Notes - Copy Constructor

0

No comments posted yet

Comments

Slide 1

Copy Constructor http://ecomputernotes.com

Slide 2

Example class Person{ char * name; public: Person(char * = NULL); const char * GetName() const; ~Person(); }; http://ecomputernotes.com

Slide 3

Example class Student: public Person{ char* major; public: Student(char *, char *); void Print() const; ~Student(); }; http://ecomputernotes.com

Slide 4

Example Student::Student(char *_name, char *_maj) : Person(_name), major(NULL) { if (_maj != NULL) { major = new char [strlen(_maj)+1]; strcpy(major,_maj); } } http://ecomputernotes.com

Slide 5

Example void Student::Print() const{ cout << “Name: ”<< GetName() <<endl; cout << “Major: “ << major << endl; } http://ecomputernotes.com

Slide 6

Example int main(){ Student sobj1(“Smith”, “Computer Science”); { Student sobj2 = sobj1; // Student sobj2(sobj1); sobj2.Print(); } return 0; } http://ecomputernotes.com

Slide 7

Example The output is as follows: Name: Smith Major: Computer Science http://ecomputernotes.com

Slide 8

Copy Constructor Compiler generates copy constructor for base and derived classes, if needed Derived class Copy constructor is invoked which in turn calls the Copy constructor of the base class The base part is copied first and then the derived part http://ecomputernotes.com

Slide 9

Shallow Copy

Slide 10

Example Person::Person(const Person& rhs){ // Code for deep copy } int main(){ Student sobj1(“Smith”, “Computer Science”); Student sobj2 = sobj1; sobj2.Print(); return 0; } http://ecomputernotes.com

Slide 11

Example The output is as follows: Name: Smith Major: Computer Science http://ecomputernotes.com

Slide 12

Copy Constructor Compiler generates copy constructor for derived class, calls the copy constructor of the base class and then performs the shallow copy of the derived class’s data members http://ecomputernotes.com

Slide 13

Shallow Copy

Slide 14

Example Person::Person(const Person& rhs) { // Code for deep copy } Student::Student (const Student& rhs) { // Code for deep copy } http://ecomputernotes.com

Slide 15

Example int main(){ Student sobj1(“Smith”, “Computer Science”); Student sobj2 = sobj1; sobj2.Print(); return 0; } http://ecomputernotes.com

Slide 16

Copy Constructor The output will be as follows: Name: Major: Computer Science Name of sobj2 was not copied from sobj1 http://ecomputernotes.com

Slide 17

Copy

Slide 18

Modified Default Constructor Person::Person(char * aName){ if(aName == NULL) cout << “Person Constructor”; ... } int main(){ Student s (“Smith”,“Computer Science”); … } http://ecomputernotes.com

Slide 19

Copy Constructor The output of previous code will be as follows: Person Constructor Name: Major: Computer Science http://ecomputernotes.com

Slide 20

Copy Constructor Programmer must explicitly call the base class copy constructor from the copy constructor of derived class http://ecomputernotes.com

Slide 21

Example Person::Person(const Person& prhs) { // Code for deep copy } Student::Student(const Student &srhs) :Person(srhs) { // Code for deep copy } http://ecomputernotes.com

Slide 22

Example main function shown previously will give following output Name: Smith Major: Computer Science http://ecomputernotes.com

Slide 23

Copy

Slide 24

Copy Constructors Person::Person(const Person &rhs) : name(NULL) { //code for deep copy } Student::Student(const Student & rhs) : major(NULL), Person(rhs){ //code for deep copy } 3 4 5 1 6 2 7 http://ecomputernotes.com

Slide 25

Example int main() { Student sobj1, sboj2(“Smith”, “CS”); sobj1 = sobj2; return 0; } http://ecomputernotes.com

Slide 26

Assignment Operator Compiler generates copy assignment operator for base and derived classes, if needed Derived class copy assignment operator is invoked which in turn calls the assignment operator of the base class The base part is assigned first and then the derived part

Slide 27

Assignment Operator Programmer has to call operator of base class, if he is writing assignment operator of derived class http://ecomputernotes.com

Slide 28

Example class Person{ public: Person & operator = (const Person & rhs){ cout << “Person Assignment”; // Code for deep copy assignment } }; http://ecomputernotes.com

Slide 29

Example class Student: Public Person{ public: Student & operator = (const Student & rhs){ cout<< “Student Assignment”; // Code for deep copy assignment } }; http://ecomputernotes.com

Slide 30

Example int main() { Student sobj1, sboj2(“Smith”, “CS”); sobj1 = sobj2; return 0; } http://ecomputernotes.com

Slide 31

Example The assignment operator of base class is not called Output Student Assignment http://ecomputernotes.com

Slide 32

Assignment Operator There are two ways of writing assignment operator in derived class Calling assignment operator of base class explicitly Calling assignment operator of base class implicitly http://ecomputernotes.com

Slide 33

Calling Base Class Member Function Base class functions can be explicitly called with reference to base class itself //const char* Person::GetName() {...}; void Student::Print() { cout << GetName(); cout << Person::GetName(); } http://ecomputernotes.com

Slide 34

Explicitly Calling operator = Person & Person::operator = (const Person & prhs); Student & Student ::operator = (const Student & srhs){ Person::operator = (srhs); … return *this; } http://ecomputernotes.com

Slide 35

Implicitly Calling operator = Student & Student ::operator = (const Student & srhs) { static_cast<Person &>*this=srhs; // Person(*this) = srhs; // (Person)*this = srhs; … } http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Copy Constructor in Object oriented Programming what is Copy Constructor Explain about it in detail .explain it with example

Tags: computer notes copy constructor

URL:
More by this User
Most Viewed