Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Copy Constructors: Shallow vs Deep Copying in C++, Slides of Computer Science

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

2012/2013

Uploaded on 03/20/2013

dharmanand
dharmanand 🇮🇳

3.3

(3)

61 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Classes
Copy Constructors
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Understanding Copy Constructors: Shallow vs Deep Copying in C++ and more Slides Computer Science in PDF only on Docsity!

Classes

Copy Constructors

Copy Constructor

  • Constructor with a Reference to THAT CLASS as an ARGUMENT
  • X(X &) Format
  • Initialization of a New Object by Another Object of that Class
  • An Object is Passed by Value to a Function
  • An Object is Returned from a Function
  • Shallow and Deep Copying -- Involvement of Pointers
  • Implicit Copy Constructor is Created by the Compiler to Perform Member-wise Initialization

Copy Constructor – Example 1 ….

  • // date.cpp #include “date.h” Date::date() //default constructor {day = 0; month = 0; year = 0; string_date = 0;}

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);}

Copy Constructor – Example 1 ….

  • // client.cpp

#include “date.h”

main() {

date today("June 21, 1995");

//Use of shallow copy constructor

date extra_day = today;

  • “Shallow copy” simply means that all the

data member values are copied.

Copy Constructor – Example 2 ….

  • // date.cpp #include “date.h” Date::date() //default constructor {day = 0; month = 0; year = 0; string_date = 0;}

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);}

Copy Constructor – Example 2 ….

  • // client.cpp

#include “date.h” main() { date today("June 21, 1995"); //Deep copy constructor date extra_day = today; }

  • “Deep copy” means that objects pointed to by

data members are also copied. Deep copying may be required whenever you have data members that are pointers.

Copy Constructor – Example 3 ….

  • // student.cpp #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;} //Shallow copy constructor Student::Student (Student &s){ name = s.name; id = s.id; email = s.email; }

Copy Constructor – Example 3 ….

  • // client.cpp

#include “student.h”

/* “csStudent" is an instance of “Student" */

main(){

Student csStudent1("Susie Creamchese“,

"screamch@iupui.edu");

Student csStudent2 = csStudent1;

What happens when csStudent2 changes its name?

• // student.cppCopy Constructor – Example 4 ….

#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);}

Copy Constructor – Example 4 ….

  • // client.cpp

#include “student.h”

main(){

Student csStudent1 (“Susie Creamchese”,

“screamch@iupui.edu”);

Student csStudent2 = csStudent1;

What happens when csStudent2 changes its name?