C Classes C Interlude 1 Data Structures and

  • Slides: 9
Download presentation
C++ Classes C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and

C++ Classes C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Contents • • • A Problem to Solve Implementing a Solution Templates Inheritance Virtual

Contents • • • A Problem to Solve Implementing a Solution Templates Inheritance Virtual Methods and Abstract Classes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A Problem to Solve • Creating a video game with a group of classes

A Problem to Solve • Creating a video game with a group of classes to represent three types of boxes § Plain box § Toy box § Magic box . htm code listing files must be in the same folder as the. ppt files for these links to work • Begin by designing an ADT plain box § Use aspects of its implementation for the other two boxes • View header file, Listing C 1 -1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Class Declarations • Private Data Fields • Constructors and Destructors • Methods § Accessor

Class Declarations • Private Data Fields • Constructors and Destructors • Methods § Accessor methods § Mutator methods § Passing parameters by constant reference. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Class Declarations Guidelines for safe and secure programming • Declare data fields as private

Class Declarations Guidelines for safe and secure programming • Declare data fields as private • Declare as const any method that does not change the object • Declare any parameter passed by reference as const § Unless you are certain it must be modified by the method • Preventing Compiler Errors Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing a Solution • View Listing C 1 -2 Implementation file for the Plain.

Implementing a Solution • View Listing C 1 -2 Implementation file for the Plain. Box class • Note a template version, Listing C 1 -3 • Implementation file for the Plain. Box template class, Listing C 1 -4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Inheritance • Base Classes and Derived Classes • View Listing C 1 -5, Template

Inheritance • Base Classes and Derived Classes • View Listing C 1 -5, Template header fi le for the class Toy. Box • And the implementation file, Listing C 1 -6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Overriding Base-Class Methods • Redefine inherited methods • Example, Listing C 1 -7 Header

Overriding Base-Class Methods • Redefine inherited methods • Example, Listing C 1 -7 Header file for the class Magic. Box • And the implementation, Listing C 1 -8 • Consider abstract class, an interface for the ADT Box, Listing C 1 -9 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

End Chapter C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and

End Chapter C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013