More about Classes and Object Oriented Programming More

  • Slides: 11
Download presentation
More about Classes and Object Oriented Programming

More about Classes and Object Oriented Programming

More C++ • Memberwise assignment • Copy constructors

More C++ • Memberwise assignment • Copy constructors

11. 4 Memberwise Assignment • Can use = to assign one object to another,

11. 4 Memberwise Assignment • Can use = to assign one object to another, or to initialize an object with an object’s data • Examples (assuming class V): V v 1, v 2; … // statements that assign … // values to members of v 1 v 2 = v 1; // assignment V v 3 = v 2; // initialization 11 -3

11. 5 Copy Constructors • Special constructor used when a newly created object is

11. 5 Copy Constructors • Special constructor used when a newly created object is initialized to the data of another object of same class • Default copy constructor copies field-to-field, using memberwise assignment • The default copy constructor works fine in most cases 11 -4

Copy Constructors Problems occur when objects contain pointers to dynamic storage: class Cp. Class

Copy Constructors Problems occur when objects contain pointers to dynamic storage: class Cp. Class { public: Cp. Class(int v=0) { p = new int; *p = v; } ~Cp. Class(){delete p; } private: int *p; }; 11 -5

Default Constructor Causes Sharing of Storage Cp. Class c 1(5); if (true) { Cp.

Default Constructor Causes Sharing of Storage Cp. Class c 1(5); if (true) { Cp. Class c 2 = c 1; } // // // 11 -6 c 1 is corrupted when c 2 goes out of scope and its destructor executes c 1 5 c 2

Problems of Sharing Dynamic Storage • Destructor of one object deletes memory still in

Problems of Sharing Dynamic Storage • Destructor of one object deletes memory still in use by other objects • Modification of memory by one object affects other objects sharing that memory 11 -7

Programmer-Defined Copy Constructors • A copy constructor is one that takes a reference parameter

Programmer-Defined Copy Constructors • A copy constructor is one that takes a reference parameter to another object of the same class • The copy constructor uses the data in the object passed as parameter to initialize the object being created • Reference parameter should be const to avoid potential for data corruption 11 -8

Programmer-Defined Copy Constructors • The copy constructor avoids problems caused by memory sharing •

Programmer-Defined Copy Constructors • The copy constructor avoids problems caused by memory sharing • Can allocate separate memory to hold new object’s dynamic member data • Can make new object’s pointer point to this memory • Copies the data, not the pointer, from the original object to the new object 11 -9

Copy Constructor Example class Cp. Class { public: Cp. Class(const Cp. Class &obj) {

Copy Constructor Example class Cp. Class { public: Cp. Class(const Cp. Class &obj) { p = new int; *p = *obj. p; } Cp. Class(int v=0) { p = new int; *p = v; } ~Cp. Class() {delete p; } }; public: print *p; 11 -10

Copy Constructor – When Is It Used? A copy constructor is called when •

Copy Constructor – When Is It Used? A copy constructor is called when • An object is initialized from an object of the same class • An object is passed by value to a function • An object is returned using a return statement from a function (also discussed in Chapter 7) 11 -11