The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Similar documents
The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Implementing Linked Lists

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Dynamic Allocation of Memory

The Class Construct Part 1

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

The Critical-Path Algorithm

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Dynamic Allocation of Memory

Minimal Spanning Trees

Scope and Parameter Passing

Inheritance: The Fundamental Functions

Scope and Parameter Passing

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Recursive Linked Lists

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Inheritance: The Fundamental Functions

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

LR Parsing - Conflicts

The Decreasing-Time Algorithm

Friends and Unary Operators

Building the Abstract Syntax Trees

Binary Tree Applications

Recursive Descent Parsers

Sampling Distribution Examples Sections 15.4, 15.5

Rotations and Translations

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Solving Recursive Sequences by Iteration

Programming Languages

Programming Languages

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

List Iterator Implementation

Binary Tree Implementation

Binary Tree Implementation

The Class Construct Part 2

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Stacks and their Applications

The Graphics Pipeline

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Scheduling and Digraphs

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Density Curves Sections

The Pairwise-Comparison Method

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

The Traveling Salesman Problem Brute Force Method

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Recognition of Tokens

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

PHP Queries and HTML Forms Lecture 23

Displaying Distributions - Quantitative Variables

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Introduction to Compiler Design

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Lecture 29 Section Wed, Apr 1, 2009

The Graphics Pipeline

Nondeterministic Programming in C++

Magnification and Minification

Street-Routing Problems

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The Normal Distribution

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Ambient and Diffuse Light

The Coefficient of Determination

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

The Plurality-with-Elimination Method

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Function Definition Syntax Tree

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

The Projection Matrix

Functional Dependencies and Normal Forms

Basic PHP Lecture 17

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Functional Dependencies and Normal Forms

Basic CSS Lecture 17

Fundamental Data Types

The CYK Parsing Algorithm

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

The Traveling Salesman Problem Cheapest-Link Algorithm

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

JFlex Regular Expressions

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The x86 Architecture

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Introduction to Databases

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

The x86 Instruction Set

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Transcription:

The Constructors Lecture 7 Sections 11.4-11.5 Robb T. Koether Hampden-Sydney College Wed, Feb 1, 2017 Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 1 / 25

1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 2 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 3 / 25

The Four Fundamental Member Functions The four fundamental functions The default constructor The copy constructor The destructor The assignment operator These four member functions are essential to the functioning of any class. In each case, if you fail to write your own version, the compiler will create the automatic version for you. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 4 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 5 / 25

The Default Constructor The Default Constructor Type::Type(); // Prototype Type Object; // Usage The default constructor constructs an object for which no initial value is given. The default constructor should initialize the data members to neutral values that are appropriate for that type. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 6 / 25

Vectr Default Constructor Example (Vectr Default Constructor) Vectr() : m_size(0), m_element(null) {} Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 7 / 25

Purposes of the Default Constructor The Default Constructor Vectr v; Vectr* ptr = new Vectr[10]; Vectr v_arr[10] = {Vectr(4, 123), Vectr(8, 567)}; The default constructor is used when An object is created with no initial value specified. The new operator is used to create an array. A static array is partially initialized. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 8 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 9 / 25

The Automatic Default Constructor The automatic default constructor is provided automatically if we write no constructor. It Allocates memory for the data members. Invokes each data member s own default constructor. However, if we write any constructor, then the automatic default constructor is not provided. In that case, we must write the default constructor, if we want one. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 10 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 11 / 25

The Copy Constructor The Copy Constructor Type::Type(const Type&); // Prototype Type ObjectA = ObjectB; // Usage 1 Type ObjectA(ObjectB); // Usage 2 The copy constructor constructs an object which will be a copy of an existing object. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 12 / 25

Vectr Copy Constructor Example (Vectr Copy Constructor) Vectr(const Vectr& v) { m_size = v.m_size; if (m_size == 0) m_element = NULL; else m_element = new double[m_size]; } for (int i = 0; i < m_size; i++) m_element[i] = v.m_element[i]; return; Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 13 / 25

Purposes of the Copy Constructor The Copy Constructor Vectr f(vectr v); int main() { Vectr v; Vectr u = v; Vectr w(u); u = f(v); }. The copy constructor is used when An object is created and initialized to the value of an existing object of the same type. A local copy of a value parameter is created during a function call. A function returns a value. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 14 / 25

Point of Style The Copy Constructor Vectr u = v; // Good style Vectr u(v); // Good style Vectr u; // Pooru = v; // style Vectr u = Vectr(3, 123); // Poor style The first and second use the copy constructor. The third and fourth lines use the default constructor followed by the assignment operator. The fifth uses another constructor followed by the copy constructor. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 15 / 25

makecopy Example (makecopy) void makecopy(const Vectr& v) { m_size = v.m_size; if (m_size == 0) m_element = NULL; else m_element = new double[m_size]; } for (int i = 0; i < m_size; i++) m_element[i] = v.m_element[i]; return; A handy technique is to write a function makecopy() and simply call on it to do the work of the copy constructor. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 16 / 25

Vectr Copy Constructor Example (Vectr Copy Constructor) Vectr(const Vectr& v) { makecopy(v); return; } Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 17 / 25

Points of Style The Copy Constructor Type::Type(const Type& obj) { makecopy(obj); return; } void Type::makeCopy(const Type& obj) { // Make a copy } Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 18 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 19 / 25

The Automatic Copy Constructor The automatic copy constructor Allocates memory for the data members. Invokes each data member s copy constructor to copy values from the existing object. copy as is mema memb memc ObjectA mema memb memc ObjectB Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 20 / 25

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 same memory 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 copy of a vector? Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 21 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 22 / 25

Constructors and the new Operator The new Operator and Constructors Vectr* ptr; ptr = new Vectr; ptr = new Vectr(v); ptr = new Vectr(10); ptr = new Vectr(10, 123); ptr = new Vectr[10]; The new operator is designed to work in conjunction with the constructors. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 23 / 25

Outline 1 The Four Fundamental Member Functions 2 The Default Constructor The Automatic Default Constructor 3 The Copy Constructor The Automatic Copy Constructor 4 Constructors and the new Operator 5 Assignment Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 24 / 25

Assignment Assignment Read Sections 11.4-11.5. Robb T. Koether (Hampden-Sydney College) The Constructors Wed, Feb 1, 2017 25 / 25