The Class Construct Part 1

Similar documents
The Class Construct Part 2

Scope and Parameter Passing

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

Scope and Parameter Passing

Friends and Unary Operators

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

The Critical-Path Algorithm

Minimal Spanning Trees

Stacks and their Applications

The Traveling Salesman Problem Brute Force Method

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

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

LR Parsing - Conflicts

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

The Traveling Salesman Problem Nearest-Neighbor Algorithm

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

Inheritance: The Fundamental Functions

Magnification and Minification

Inheritance: The Fundamental Functions

Building the Abstract Syntax Trees

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

Scheduling and Digraphs

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

Binary Tree Implementation

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

The Plurality-with-Elimination Method

Sampling Distribution Examples Sections 15.4, 15.5

Solving Recursive Sequences by Iteration

Dynamic Allocation of Memory

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

Rotations and Translations

The Graphics Pipeline

Street-Routing Problems

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

List Iterator Implementation

The CYK Parsing Algorithm

Binary Tree Implementation

Recursive Descent Parsers

Programming Languages

Recursive Linked Lists

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

XQuery FLOWR Expressions Lecture 35

Density Curves Sections

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

Implementing Linked Lists

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

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

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

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

Ambient and Diffuse Light

The Graphics Pipeline

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

Binary Tree Applications

Function Definition Syntax Tree

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

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

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

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

Programming Languages

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

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

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

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

The Normal Distribution

Dynamic Allocation of Memory

The Coefficient of Determination

The Projection Matrix

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

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

LR Parsing - The Items

Recognition of Tokens

Displaying Distributions - Quantitative Variables

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

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

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

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

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

The Projection Matrix

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

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

Basic PHP Lecture 17

Introduction to Databases

PHP Queries and HTML Forms Lecture 23

Relational Databases Lecture 2

Relational Databases

The x86 Architecture

The x86 Instruction Set

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

DTDs and XML Attributes

Introduction to Compiler Design

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

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

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

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

Introduction to Databases

Nondeterministic Programming in C++

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

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

Transcription:

The Class Construct Part 1 Lecture 23 Sections 7.5-7.6 Robb T. Koether Hampden-Sydney College Fri, Oct 26, 2018 Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 1 / 18

1 The Class Construct 2 The Point Class Example 3 The Member Functions 4 Constructors 5 Assignment Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 2 / 18

Outline 1 The Class Construct 2 The Point Class Example 3 The Member Functions 4 Constructors 5 Assignment Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 3 / 18

The Class Construct The Class Construct class name { public: member-function-prototypes }; private: data-member-declarations The class construct has the above form. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 4 / 18

The Class Construct Each member function appears as a prototype. Each data member appears as a declaration. The public part of the class construct describes the user interface. Note that the whole thing ends with a semicolon. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 5 / 18

Outline 1 The Class Construct 2 The Point Class Example 3 The Member Functions 4 Constructors 5 Assignment Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 6 / 18

Point Class Example The Point Class class Point { // Data members }; private: double x; // x-coordinate double y; // y-coordinate Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 7 / 18

The Point0 Class Example Point0.h Point0Test.cpp Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 8 / 18

Outline 1 The Class Construct 2 The Point Class Example 3 The Member Functions 4 Constructors 5 Assignment Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 9 / 18

The Member Functions For convenience, we categorize member functions as follows. Constructors The destructor Inspectors Mutators Facilitators Operators Other member functions Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 10 / 18

The Member Functions A constructor is called when a new object is created. It initializes the object. The destructor is called when an object passes out of scope. An inspector returns an attribute of the object, but does not change the object. A mutator changes one or more data members of the object. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 11 / 18

The Member Functions A facilitator is used to help implement an operator. An operator is a function that can be invoked through a symbol such as + or *. The other member functions are the ones that do not fall into any of the previous categories. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 12 / 18

Outline 1 The Class Construct 2 The Point Class Example 3 The Member Functions 4 Constructors 5 Assignment Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 13 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. A constructor (automatically) allocates space in memory for the object s data members. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. A constructor (automatically) allocates space in memory for the object s data members. A constructor typically will initialize the object s data members. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. A constructor (automatically) allocates space in memory for the object s data members. A constructor typically will initialize the object s data members. A constructor s name must be the same as the class name. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. A constructor (automatically) allocates space in memory for the object s data members. A constructor typically will initialize the object s data members. A constructor s name must be the same as the class name. A class may have many constructors, each with a different signature. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. A constructor (automatically) allocates space in memory for the object s data members. A constructor typically will initialize the object s data members. A constructor s name must be the same as the class name. A class may have many constructors, each with a different signature. A constructor has no return type. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Constructors A constructor is a function that creates an instance (an object) of a class. A constructor (automatically) allocates space in memory for the object s data members. A constructor typically will initialize the object s data members. A constructor s name must be the same as the class name. A class may have many constructors, each with a different signature. A constructor has no return type. A constructor is invoked by declaring an object of that type. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 14 / 18

Point Class Example The Point Class class Point { public: // Constructors }; Point(); // Default point (0, 0) Point(double xval, double yval); // Point (xval, yval) Point(const Point& p); // Copy of Point p. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 15 / 18

The Point1 Class Example Point1.h Point1.cpp Point1Test.cpp Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 16 / 18

Outline 1 The Class Construct 2 The Point Class Example 3 The Member Functions 4 Constructors 5 Assignment Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 17 / 18

Assignment Assignment Read Sections 7.5-7.6. Robb T. Koether (Hampden-Sydney College) The Class Construct Part 1 Fri, Oct 26, 2018 18 / 18