








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An explanation of copy constructors in c++ with examples of shallow and deep copying. It covers the concept of copy constructors, their syntax, and the difference between shallow and deep copying. The document also includes code examples of copy constructors for a 'date' and 'student' class, and discusses what happens when an object created by copy constructor is modified.
Typology: Slides
1 / 14
This page cannot be seen from the preview
Don't miss anything!
date::date(char *ip_date){ //Constructor day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date); }
//Shallow Copy Constructor date(const date &ip_date) {string_date = ip_date.string_date;}
//Destructor date::~date() {delete[] (string_date);}
date::date(char *ip_date){ //Constructor day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date); } //Deep Copy Constructor date(const date &ip_date) { string_date = new char[size]; strcpy(string_date, ip_date.string_date); } //Destructor date::~date() {delete[] (string_date);}
#include “date.h” main() { date today("June 21, 1995"); //Deep copy constructor date extra_day = today; }
data members are also copied. Deep copying may be required whenever you have data members that are pointers.
#include “student.h” /* Member functions of the “Student" class */ Student::Student(char *studentName, char *studentId, char *studentEmail){ name = new char[size]; strcpy(name, studentName); id = new char[size]; strcpy(id, studentId); email = new char[size]; strcpy(email, studentEmail);} Student::~Student(){ delete [] name; delete [] id; delete [] email;} // Deep copy constructor Student::Student(Student &s){ name = new char[size]; strcpy(name, s.name); id = new char[size]; strcpy(id, s.id); email = new char[size]; strcpy(email, s.email);}