Classes Copy Constructors - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Classes Copy Constructors

Description:

Constructor with a Reference to THAT CLASS as an ARGUMENT. X(X &) Format ... { Student csStudent1('Susie Creamchese', '123456789', 'screamch_at_iupui.edu' ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: dalero
Category:

less

Transcript and Presenter's Notes

Title: Classes Copy Constructors


1
Classes Copy Constructors
Department of Computer and Information
Science,School of Science, IUPUI
CSCI 240
Dale Roberts, Lecturer Computer Science,
IUPUI E-mail droberts_at_cs.iupui.edu
2
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

3
Copy Constructor Example 1
  • // date.hdefine 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

4
Copy Constructor Example 1 .
  • // date.cppinclude date.hDatedate() //defa
    ult constructor day 0 month 0 year 0
    string_date 0datedate(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 datedate() delete
    (string_date)

5
Copy Constructor Example 1 .
  • // client.cppinclude date.hmain()
    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.

6
Copy Constructor Example 2
  • // date.hdefine 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

7
Copy Constructor Example 2 .
  • // date.cppinclude date.hDatedate() //defa
    ult constructor day 0 month 0 year 0
    string_date 0datedate(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 charsize
    strcpy(string_date, ip_date.string_date)
    //Destructor datedate() delete
    (string_date)

8
Copy Constructor Example 2 .
  • // client.cppinclude date.hmain()
    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.

9
Copy Constructor - Example 3
  • // student.h/ A Simple Class to Represent a
    Student /define size 50class Student 
    private char name char id char email 
    public Student(char , char , char
    ) Student(Student ) //Copy
    constructor Student()

10
Copy Constructor Example 3 .
  • // student.cppinclude student.h/ Member
    functions of the Student" class /Student
    Student(char studentName, char studentId,
    char studentEmail) name new charsize
       strcpy(name, studentName) id new
    charsize    strcpy(id, studentId) email
    new charsize    strcpy(email,
    studentEmail)StudentStudent() delete
    name delete id delete
    email//Shallow copy constructorStudentStuden
    t (Student s) name s.name id
    s.id email s.email

11
Copy Constructor Example 3 .
  • // client.cppinclude student.h/ csStudent"
    is an instance of Student" /main()   
    Student csStudent1("Susie Creamchese,
    "123456789, "screamch_at_iupui.edu")   
    Student csStudent2 csStudent1

What happens when csStudent2 changes its name?
12
Copy Constructor - Example 4
  • // student.h/ A Simple Class to Represent a
    Student /define size 50class Student 
    private char name char id char email 
    public Student(char , char , char
    ) Student() Student(Student ) //Copy
    constructor

13
Copy Constructor Example 4 .
  • // student.cppinclude student.h/ Member
    functions of the Student" class
    /StudentStudent(char studentName, char
    studentId, char studentEmail) name new
    charsize    strcpy(name, studentName) id
    new charsize    strcpy(id, studentId)
    email new charsize    strcpy(email,
    studentEmail)StudentStudent() delete
    name delete id delete email//
    Deep copy constructorStudentStudent(Student
    s) name new charsize strcpy(name,
    s.name) id new charsize
    strcpy(id, s.id) email new charsize
    strcpy(email, s.email)  

14
Copy Constructor Example 4 .
  • // client.cppinclude student.hmain()   
    Student csStudent1 (Susie Creamchese,
    123456789, screamch_at_iupui.edu)   
    Student csStudent2 csStudent1

What happens when csStudent2 changes its name?
15
Acknowledgements
  • These slides were originally prepared by Rajeev
    Raje, modified by Dale Roberts.
Write a Comment
User Comments (0)
About PowerShow.com