Lecture overview -- Keyboard shortcut: 'u'  Previous page: Example of copying objects: Default copying -- Keyboard shortcut: 'p'  Next page: Preventing object copying -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Abstraction Mechanisms, Part 1 - slide 16 : 36

Example of copying objects: Programmed copying

We want to modify the meaning of copying during initialization and assignment

The point array of the line segment is copied by the copy constructor and by the assignment operator

The following versions of the program illustrates a typical pattern for copy constructors and assignment operators

point-lineseg.h
Class LineSegment WITH a copy constructor and and WITH an assignment operator.
point-lineseg.cc
The corresponding cc file with implementations of the copy constructor and the assignment operator.
prog.cc
A function that constructs, initializes and assigns LineSegments.
program-output
Program execution.

The copy constructor makes a copy of of the point array

The assignment deallocates the point array of the lhs (which is overwritten) and reallocates space for the points in the new copy

The programs above follow the pattern from Stroustrup §10.4.4.1 page 245