the constructors - lecture 5 sections 4.4, 4.5, 6

22
The Constructors Lecture 5 Sections 4.4, 4.5, 6.3 Robb T. Koether Hampden-Sydney College Fri, Jan 22, 2010 Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 1 / 22

Upload: others

Post on 03-Feb-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The ConstructorsLecture 5

Sections 4.4, 4.5, 6.3

Robb T. Koether

Hampden-Sydney College

Fri, Jan 22, 2010

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 1 / 22

Page 2: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 2 / 22

Page 3: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 3 / 22

Page 4: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Four Fundamental Member Functions

The four fundamental functionsI The default constructorI The copy constructorI The destructorI The assignment operator

These four member functions are essential to the functioning ofany class.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 4 / 22

Page 5: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 5 / 22

Page 6: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Default Constructor

The Default ConstructorType::Type(); // PrototypeType Object; // Usage

The default constructor constructs an object for which no initialvalue is given.The default constructor should initialize the data members toneutral values that are appropriate for that type.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 6 / 22

Page 7: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Purposes of the Default Constructor

The Default ConstructorVectr v;Vectr* vptr = new Vectr[10];Vectr vptr2[10] = {Vectr(4, 123), Vectr(8, 567)};

The default constructor is used whenI An object is created with no initial value specified.I The new operator is used to create an array.I A static array is partially initialized.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 7 / 22

Page 8: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Vectr Class

Example (The Vectr Class)The code.

I The header file vectr.h.I The implementation file vectr.cpp.I The test program Vectr Test.cpp.

The executable.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 8 / 22

Page 9: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 9 / 22

Page 10: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Automatic Default Constructor

The automatic default constructor is provided automatically if wewrite no constructor.It

I Allocates memory for the data members.I Invokes each data member’s own default constructor.

It is not provided automatically if we write any constructor; wemust then write the default constructor, if we want one.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 10 / 22

Page 11: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 11 / 22

Page 12: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Copy Constructor

The Copy ConstructorType::Type(const Type&); // PrototypeType ObjectA = ObjectB; // Usage 1Type ObjectA(ObjectB); // Usage 2

The copy constructor constructs an object which will be a copy ofan existing object.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 12 / 22

Page 13: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Purposes of the Copy Constructor

The Copy ConstructorVectr f(Vectr v);int main(){

Vectr v;Vectr u = v;Vectr w(u);u = f(v);

...}

The copy constructor is used whenI An object is created and initialized to the value of an existing object

of the same type.I A local copy of a value parameter is created during a function call.I A function returns a value.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 13 / 22

Page 14: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Point of Style

The Copy ConstructorVectr u = v; // Good styleVectr w; // Poor stylew = u;

The first uses the copy constructor.The second uses the default constructor followed by theassignment operator.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 14 / 22

Page 15: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Points of Style

The Copy ConstructorVectr(const Vectr& v){

makeCopy(v);return;

}

void makeCopy(const Vectr& v){// Make a copy}

A handy technique is to write a function makeCopy() and simplycall on it to do the work of the copy constructor.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 15 / 22

Page 16: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 16 / 22

Page 17: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Automatic Copy Constructor

The automatic copy constructorI Allocates memory for the data members.I Invokes each data member’s copy constructor to copy values from

the existing object.

memAmemBmemC

memAmemBmemC

ObjectA ObjectB

copyas is

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 17 / 22

Page 18: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

The Automatic Copy Constructor

This is called a shallow copy.Pointers get copied with no change in value.Therefore, the pointer in the new object will point to the very samememory as the pointer in the old object.Generally, this is not good. Instead, we want a deep copy.What would happen in the Vectr class if we made a shallow copyof a vector?

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 18 / 22

Page 19: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 19 / 22

Page 20: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Constructors and the new Operator

The new Operator and ConstructorsVectr* vptr;vptr = new Vectr;vptr = new Vectr(v);vptr = new Vectr(10);vptr = new Vectr(10, 123);vptr = new Vectr[10];

The new operator works in conjunction with the constructors.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 20 / 22

Page 21: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Outline

1 The Four Fundamental Member Functions

2 The Default ConstructorThe Automatic Default Constructor

3 The Copy ConstructorThe Automatic Copy Constructor

4 Constructors and the new Operator

5 Assignment

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 21 / 22

Page 22: The Constructors - Lecture 5 Sections 4.4, 4.5, 6

Assignment

HomeworkRead Section 4.4, pages 158 - 165.Read Section 4.5, pages 165 - 169.Read Section 6.3, pages 269 - 286.

Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 22 / 22