C++ Classes and Data Structures Jeffrey S. Childs

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
True or false A variable of type char can hold the value 301. ( F )
Chapter 10.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Computer Science 1620 Programming & Problem Solving.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Introduction to C++.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
C++ Classes and Data Structures Jeffrey S. Childs
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.
CPS120: Introduction to Computer Science Functions.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
1 C++ Classes and Data Structures Course link…..
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 1.2 Introduction to C++ Programming
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Structures Lesson xx In this module, we’ll introduce you to structures.
C++ Classes and Data Structures Jeffrey S. Childs
Functions.
6 Chapter Functions.
Programming Introduction to C++.
Presentation transcript:

C++ Classes and Data Structures Jeffrey S. Childs Chapter 1 Structs and Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

Structs A struct holds data, like an array Each unit of data in a struct is called a data member (or member) they are called “elements” in arrays In a struct, each data member can have a different data type in arrays, the data type of each element is the same

Example Using a Struct 1 #include <iostream> 2 #include <iomanip> 3 #include <string> 4 5 using namespace std; 6 7 struct CarType { 8 string maker; 9 int year; 10 float price; 11 }; 12 13 void getYourCar( CarType & car ); 14 Don’t forget this semicolon.

Example Using a Struct (cont.) 15 int main( ) 16 { 17 CarType myCar, yourCar; 18 19 myCar.maker = "Mercedes"; // I wish 20 myCar.year = 2005; 21 myCar.price = 45567.75; 22

Example Using a Struct (cont.) 23 getYourCar( yourCar ); 24 25 cout << "Your car is a: " << yourCar.maker << endl; 26 cout << fixed << showpoint << setprecision( 2 ) << 27 "I'll offer $" << yourCar.price - 100 << 28 " for your car." << endl; 29 30 return 0; 31 } 32

Example Using a Struct (cont.) 33 void getYourCar( CarType & car ) 34 { 35 cout << "Enter your maker: "; 36 cin >> car.maker; 37 cout << "Enter the year: "; 38 cin >> car.year; 39 cout << "Enter the price: $"; 40 cin >> car.price; 41 }

Object Assignment An object of a struct can be assigned to another object of the same struct type: myCar = yourCar; This assigns each data member in yourCar to the corresponding data member of myCar Also assigns any array data members

Classes A class is similar to a struct A class contains data members, but it also contains function members Objects are made from classes, similarly to the way that objects are made from structs The main program communicates with the objects data is passed from main program to object and from object back to the main program

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

Main Program Using Objects Object A Main Program Object C Object B

How the Main Program Uses A Class Object The main program does not access the data within a class object The main program only accesses the functions of a class object communication occurs by passing data as parameters into the object’s function the object passes data to the main program through its return type

Main Program and Object public: functions private: data Main Program

Main Program Calls a Function in the Object public: functions private: data Main Program

The Function Accesses Data Object public: functions private: data Main Program

Function Returns a Value Back to the Main Program Object public: functions private: data Main Program

Main Program Calls a Different Function Object public: functions private: data Main Program

Function Calls Another Function Object public: functions private: data Main Program

Second Function Accesses Data Object public: functions private: data Main Program

Second Function Returns Back to First Function Object public: functions private: data Main Program

First Function Accesses Data Object public: functions private: data Main Program

Function Returns Back to Main Program Object public: functions private: data Main Program

Example of a Class 1 class Checkbook 2 { 3 public: 2 { 3 public: 4 void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; This class definition is placed into its own file, called the class specification file, named checkbook.h (by convention)

Example of a Class (cont.) 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; The writeCheck function returns false if the amount of the check is greater than the balance; returns true otherwise.

Example of a Class (cont.) 1 class Checkbook 2 { 3 public: 4 void setBalance( float amount ); bool writeCheck( float amount ); void deposit( float amount ); 7 float getBalance( ); 8 float getLastCheck( ); 9 float getLastDeposit( ); 10 private: 11 float balance; 12 float lastCheck; 13 float lastDeposit; 14 }; Don’t forget the semicolon.

Example of a Class (cont.) 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19 balance = amount; 20 } The function definitions are placed into a separate file called the class implementation file. This file would be called checkbook.cpp (by convention).

Example of a Class (cont.) 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19 balance = amount; 20 } The balance variable is declared in the private section of the class definition.

Example of a Class (cont.) 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19 balance = amount; 20 } Special notation for class function definitions

Example of a Class (cont.) 21 bool Checkbook::writeCheck( float amount ) 22 { 23 if ( amount > balance ) 24 return false; 25 balance -= amount; 26 lastCheck = amount; 27 return true; 28 }

Example of a Class (cont.) 29 void Checkbook::deposit( float amount ) 30 { 31 balance += amount; 32 lastDeposit = amount; 33 }

Example of a Class (cont.) 34 float Checkbook::getBalance( ) 35 { 36 return balance; 37 } 38 39 float Checkbook::getLastCheck( ) 40 { 41 return lastCheck; 42 } end of checkbook.cpp

A Program that Uses the Checkbook Class 1 #include <iostream> 2 #include <iomanip> 3 #include "checkbook.h" 4 5 using namespace std; 6 7 int menu( ); 8 9 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; 10 11 int main( ) 12 { 13 Checkbook cb; 14 float balance, amount; 15 int choice; A main program that uses the Checkbook class is placed into a separate .cpp file

A Program that Uses the Checkbook Class (cont.) 16 cout << "Enter the initial balance: $"; 17 cin >> balance; 18 cb.setBalance( balance ); 19 20 cout << fixed << showpoint << setprecision( 2 );

A Program that Uses the Checkbook Class (cont.) 21 choice = menu( ); 22 while ( choice != QUIT ) { 23 if ( choice == CHECK ) { 24 cout << "Enter check amount: $"; 25 cin >> amount; 26 if ( cb.writeCheck( amount ) ) 27 cout << "Check accepted." << endl; 28 else { 29 cout << "Your balance is not high "; 30 cout << "enough for that check." << endl; 31 } 32 } body of the while loop continues

A Program that Uses the Checkbook Class (cont.) 33 else if ( choice == DEPOSIT ) { 34 cout << "Enter deposit amount: $"; 35 cin >> amount; 36 cb.deposit( amount ); 37 cout << "Deposit accepted." << endl; 38 } body of the while loop continues

A Program that Uses the Checkbook Class (cont.) 39 else { // must be a balance request 40 amount = cb.getBalance( ); 41 cout << "Your balance is: $" << amount << endl; 42 } 43 44 choice = menu( ); 45 } 46 47 return 0; 48 } 49 end of while loop

A Program that Uses the Checkbook Class (cont.) 50 int menu( ) 51 { 52 int choice; 53 54 cout << endl; 55 cout << "1 Write a check" << endl; 56 cout << "2 Make a deposit" << endl; 57 cout << "3 Get the balance" << endl; 58 cout << "4 Quit" << endl << endl; 59 cout << "Enter a number between 1 and 4: "; 60 cin >> choice; 61 return choice; 62 }

Keep In Mind The data members of a class cannot be accessed by a main program. The object always retains the current values of its data members, even when object code is no longer executing. Each function of a class can use the data members of the class as though they have been declared within the function.

Maintenance Maintenance refers to any work done on a program after it is put into operation The world constantly changes Programs have to be maintained (modified) to keep up with the changes When programs are maintained, the data members sometimes have to change Private data members make it easier to maintain a program

If Data Members were Accessed Directly… Class Main Program int a; int b; int c; . Suppose the variables of a class were accessed by 100 places in a program

If Data Members were Accessed Directly… (cont.) Class Main Program int arr[10] . Then we need to change the way the data is repre-sented (for maintenance)

If Data Members were Accessed Directly… (cont.) Class Main Program int arr[10] . We need to change 100 lines of code in the main program!

Data Members Should Be Private Class Main Program int foo( int x ) private: int a; int b; int c; . Here, the main program calls foo from 100 places.

Data Members Should Be Private (cont.) Class Main Program int foo( int x ) private: int a; int b; int c; . Then foo accesses the private data members.

Data Members Should Be Private (cont.) Class Main Program int foo( int x ) private: int arr[10] . If the data needs to change, the body of foo will need to be rewritten.

Data Members Should Be Private (cont.) Class Main Program int foo( int x ) private: int arr[10] . However, the function call of foo and return type will stay the same.

Data Members Should Be Private (cont.) Class Main Program int foo( int x ) private: int arr[10] . No changes need to be made in the main program!

Data Members Should Be Private (cont.) Class Main Program int foo( int x ) private: int arr[10] . Program maintenance is easier this way…

Data Members Should Be Private (cont.) Class Main Program int foo( int x ) private: int arr[10] . especially if there is more than one program using the class.

When Writing a Class Your class may be used by hundreds or even thousands of clients (main programmers) Write your class for clients, not for computer users Do not put code in your class which gives messages to users or asks users for information – let clients handle this the way they want

Debugging a Program with Classes First technique – make the classes and the main program and test everything all at once inefficient: takes a long time to track down each bug Second technique – test each class as it is made by writing a main program just to test it (called a test driver or driver)

First Technique Class 1 bugs in the classes Class 2 Main Program Let’s assume it takes 2 hours, on average, to fix each problem. Class 4

First Technique (cont.) Class 1 Runtime Errors: 20 Time: 0:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 19 Time: 2:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 18 Time: 4:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 17 Time: 6:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 16 Time: 8:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 15 Time: 10:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 14 Time: 12:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 13 Time: 14:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 12 Time: 16:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 11 Time: 18:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 10 Time: 20:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 9 Time: 22:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 8 Time: 24:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 7 Time: 26:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 6 Time: 28:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 5 Time: 30:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 4 Time: 32:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 3 Time: 34:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 2 Time: 36:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 1 Time: 38:00 Class 2 Main Program Class 3 Class 4

First Technique (cont.) Class 1 Runtime Errors: 0 Time: 40:00 Class 2 Main Program Class 3 Class 4

Using Drivers Class 1 We’ll try the second technique – write a driver to test each class. Assume it takes one hour to write a driver. Fixing a runtime error will be faster – let’s say it takes a half an hour. Class 2 Class 3 Class 4 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 20 Time: 0:00 Class 2 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 20 Write a Driver Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 20 Driver Time: 1:00 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 19 Driver Time: 1:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 18 Driver Time: 2:00 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 17 Driver Time: 2:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 16 Driver Time: 3:00 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Runtime Errors: 15 Driver Time: 3:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 15 Time: 3:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 15 Time: 3:30 Write a Driver Class 3 Class 4 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 15 Time: 4:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 14 Time: 5:00 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 13 Time: 5:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 12 Time: 6:00 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 11 Time: 6:30 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Runtime Errors: 10 Time: 7:00 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 10 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 10 Write a Driver Class 4 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 10 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 9 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 8 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 7 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 6 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Class 2 Class 3 Runtime Errors: 5 Time for first technique: 40:00

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 5 Time: 10:30 Class 4

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 5 Time: 10:30 Class 4 Write a Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 5 Time: 11:30 Class 4 Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 4 Time: 12:00 Class 4 Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 3 Time: 12:30 Class 4 Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 2 Time: 13:00 Class 4 Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 1 Time: 13:30 Class 4 Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 0 Time: 14:00 Class 4 Driver

Using Drivers (cont.) Class 1 Time for first technique: 40:00 Class 2 Runtime Errors: 0 Time: 14:00 Class 4 Driver

Function Definitions in the Class Specification The following slides show that function definitions can be placed into the class specification. In general, we should avoid doing this because it is a hindrance to program maintenance. It is shown here because you will see code written this way during your career.

Function Definitions in the Class Specification (cont.) 1 // checkbook.h – A class for a checkbook 2 class Checkbook 3 { 4 public: 5 void setBalance( float amount ) 6 { balance = amount; } 7 bool writeCheck( float amount ); // returns false if 8 // amount is greater than balance; 9 // otherwise returns true 10 void deposit( float amount ) { balance += amount; 11 lastDeposit = amount; }

Function Definitions in the Class Specification (cont.) 12 float getBalance( ) { return balance; } 13 float getLastCheck( ) { return lastCheck; } 14 float getLastDeposit( ) { return lastDeposit; } 15 private: 16 float balance; 17 float lastCheck; 18 float lastDeposit; 19 };

Function Definitions in the Class Specification (cont.) 20 // checkbook.cpp – function definitions for the 21 // Checkbook class 22 #include “checkbook.h” 23 24 bool Checkbook::writeCheck( float amount ) 25 { 26 if ( amount > balance ) 27 return false; 28 balance -= amount; 29 lastCheck = amount; 30 return true; 31 }

Struct vs. Class Functions can be placed in a struct, but only when necessary The public and private keywords can be left out of a class (rare). The public and private keywords can be placed into a struct (rare). So what is the difference between a struct and a class?

Struct vs. Class (cont.) If public and private are not used in a struct, all data members are public by default. If public and private are not used in a class, all data members are private by default.

Conventions By convention, we use structs when we want all data members to be public structs are typically defined and used within the client’s program, not in a separate file typically used for records of information By convention, we use classes when we want all data members to be private (for maintenance purposes)