Classes Copy Constructors

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.
CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Object-Oriented Programming (OOP) Lecture No. 24.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Object Oriented Programming using Java - Composition
Object-Oriented Programming (OOP) Lecture No. 15
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Object Oriented Programming (OOP) Lecture No. 8
Functions Examples CSCI 230
Abstract Data Types Polynomials CSCI 240
Object Oriented Programming using Java - Class Instance Variables
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
Classes Access Specifiers
Negative Integer Representation
Dale Roberts, Lecturer IUPUI
Object Oriented Programming (OOP) Lecture No. 9
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Introduction of Programming
Classes Static Members
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
Introduction to Programming
A Deeper Look at Classes
Object oriented programming (OOP) Lecture No. 6
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Classes Introduction CSCI 240
CS 144 Advanced C++ Programming April 30 Class Meeting
Classes Class Data Members & Initializers
Classes Member Qualifiers
Presentation transcript:

Classes Copy Constructors Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Copy Constructors Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

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.h #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); //Constructor date(const date &ip_date); // Copy Constructor ~date(); //Destructor void print_date(); //Member Function };

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.h #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); //Constructor date(const date &ip_date); // Copy Constructor ~date(); //Destructor void print_date(); //Member Function };

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.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char *email;   public: Student(char *, char *, char *); Student(Student &); //Copy constructor ~Student(); };

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“, "123456789“, "screamch@iupui.edu");     Student csStudent2 = csStudent1; } What happens when csStudent2 changes its name?

Copy Constructor - Example 4 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char *email;   public: Student(char *, char *, char *); ~Student(); Student(Student &); //Copy constructor };

Copy Constructor – Example 4 …. // 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;} // 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”, “123456789”, “screamch@iupui.edu”);     Student csStudent2 = csStudent1; } What happens when csStudent2 changes its name?

Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.