RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

Similar documents
RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

C:\Temp\Templates. Download This PDF From The Web Site

2 2

OOP- 5 Stacks Individual Assignment 35 Points

Functions & Memory Maps Review C Programming Language

Function Terminology

G52CPP C++ Programming Lecture 9

PIC 10A. Final Review: Part I

Fast Introduction to Object Oriented Programming and C++

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Lecture 7. Log into Linux New documents posted to course webpage

Operator overloading

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Topic 10: Introduction to OO analysis and design

OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1

Server Component of the Chat Room Lab

III. Classes (Chap. 3)

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Ch. 12: Operator Overloading

Function Overloading

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CSE 333 Midterm Exam July 24, Name UW ID#

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Lab 2: ADT Design & Implementation

Circle all of the following which would make sense as the function prototype.

C/C++ Text File Functions

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

pointers & references

Fundamentals of Programming Session 24

CS 103 Unit 10 Slides

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Friend Functions and Friend Classes

Lab: Supplying Inputs to Programs

CE221 Programming in C++ Part 1 Introduction

Due Date: See Blackboard

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Common Misunderstandings from Exam 1 Material

Installing and Using Dev-C++

Intermediate Programming & Design (C++) Classes in C++

CS3157: Advanced Programming. Outline

CS 103 Unit 10 Slides

Cpt S 122 Data Structures. Introduction to C++ Part II

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

OBJECT ORIENTED PROGRAMMING USING C++

Call The Project Dynamic-Memory

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Lab 12 Object Oriented Programming Dr. John Abraham

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Introduction to Programming session 24

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Chapter 1 Getting Started

Final CSE 131B Spring 2004

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

AIMS Embedded Systems Programming MT 2017

Introduction to Programming using C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Lecture 2, September 4

An Introduction to C++

Topics. Constructor. Constructors

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CSC111 Computer Science II

CSE 5A Introduction to Programming I (C) Homework 4

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

CMSC 341 Lecture 7 Lists

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

OOP-1-Review-HW-(Part B).docx CSCI 2320 Initials P a g e 1. Print Name Time Required =. Hrs. Signature (pledged) Static Array Review

dynamically allocated memory char* x = new char; int* x = new int[n]; ???...?

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

CS 247: Software Engineering Principles. ADT Design

Fundamentals of Programming CS-110. Lecture 2

COMP 2355 Introduction to Systems Programming

CS304 Object Oriented Programming Final Term

Introduction to Classes

Scope. Scope is such an important thing that we ll review what we know about scope now:

Review Questions for Final Exam

377 Student Guide to C++

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

CS201- Introduction to Programming Current Quizzes

Spring 2008 Data Structures (CS301) LAB

Transcription:

1

RECOMMENDATION Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 2

3

Copy Project Folder There will be a number of times when you submit Projects in Phases. If so, you will be ask to submit the labs separately. At this point you should have a working TomH-Student-1 project folder that demonstrates all that was illustrated in the slides of OOP-02-Classes-Slides-A.pptx [1] Place a copy of this lab the "To Be Graded Folder" of your Network Drop Box [2] Make a copy of this folder. Name the folder TomH-Student-2 Using your First Name & Last Name Initial If The Lab Is Broken Up Into Phases, You Are To Include Both Folders In Your Drop Box As Requested I Will Be Grading Them Separately! 4

5

# define STUDENT_DIAGNOSTIC_LEVEL 2 int main (int argc, char * argv[]) { puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("++ Class Student Added To Main - Phase 2 ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); puts ("--------------------- Start Of Main ---------------------\n"); TestStudent(); Make The Following Changes To main COMPILE THE PROGRAM! } puts ("----------------------- HitCarriageReturnToContinue(); return (0); End Of Main ----------------------\n"); Change the Diagnostic Level & the Phase 6

Diagnostic Level = 2 Note that the old testing no longer appears # define STUDENT_DIAGNOSTIC_LEVEL 2 COMPILE THE PROGRAM! 7

Make The Following Changes To main Diagnostic Level 1 was supposed to Create the Constructor and Destructor and to make sure that they compiled and fired accordingly. Once we have successfully completed this task, we do not want to see this output over and over again; but we would like to be able to revisit the testing code if we change/modify this class later during the maintenance life cycle. 8

9

Add This Starting Pragma Block Code Before The Student Documentation # pragma region STUDENT_DIAGNOSTIC_LEVEL_1 Constructor_Destructor //======== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Student // // // // Purpose : Constructors for Class Student. Set Name to NewName and No to // // NewNo and Gender to NewGender. Default NewName = blank. // // Default NewNo = 0. Default Sex = FEMALE = false. // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// Student::Student(char NewName[], long NewNo, bool NewGender) 10

Add This Ending Pragma Block Code Immediately After The Destructor Student::~Student(void) { # ifdef STUDENT_DIAGNOSTIC_LEVEL //--------------------------- if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Destructor ~Student(void)"); # endif // STUDENT_DIAGNOSTIC_LEVEL -------------------------- } # pragma endregion I Often Use Pragma Sets To Organize My Functions! Programs & Files Can Get Long - The Pragma Sets Will Help Me Expose Those Functions That I Am Coding & Testing I Will Hide Those That I Think Are Tested & Working OK 11

# pragma region STUDENT_DIAGNOSTIC_LEVEL_1 Constructor_Destructor //=========== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Student // // // // Purpose : Constructors for Class Student. Set Name to NewName and No to // // NewNo and Gender to NewGender. Default NewName = blank. // // Default NewNo = 0. Default Sex = FEMALE = false. // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// Student::Student(char NewName[], long NewNo, bool NewGender) { # ifdef STUDENT_DIAGNOSTIC_LEVEL //--------------------------------------------------- if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student (NewName[], NewNo, NewGender)"); # endif // STUDENT_DIAGNOSTIC_LEVEL -------------------------------------------------- } strcpy_s(name, NewName); No = NewNo; Gender = NewGender; Student::Student(long NewNo, char NewName[], bool NewGender) { # ifdef STUDENT_DIAGNOSTIC_LEVEL //------------------------------- if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student (NewNo, NewName[], NewGender)"); # endif // STUDENT_DIAGNOSTIC_LEVEL ------------------------------ Using the mouse, click on the - sign beside the starting pragma See How All Of This Code Can Be (+)Opened & (-) Closed As A Group } strcpy_s(name, NewName); No = NewNo; Gender = NewGender; ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // ~Student // // // // Purpose : Do all that is necessary to destroy an object. In this class we // // are only going to insert a diagnostic display to verify the the // // destructor is firing properly. // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// Student::~Student(void) { # ifdef STUDENT_DIAGNOSTIC_LEVEL //-------------------------------------- if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Destructor ~Student(void)"); # endif // STUDENT_DIAGNOSTIC_LEVEL ------------------------------------- } # pragma endregion

Add These Same Pragma Around The Test Function In TestStudent # pragma region STUDENT_DIAGNOSTIC_LEVEL_1 Constructor_Destructor //======== if (STUDENT_DIAGNOSTIC_LEVEL <= 1) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Constructor & Destructor =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 1 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Student2("Pete", 2222, MALE), Student3("Sandra", 3333), Student4("Amber"), Student5(5555, "Nathan", MALE), Student6(6666, "Betty"), Student7(7777), Class[4]; HitCarriageReturnToContinue(); } # pragma endregion This Will Allow Us To Hide The Test Code For Diagnostic Test Level 1 So That We Can Work On Diagnostic Test Level 2 13

14

Add A Prototype For Display // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); private: char Name[20]; long No; bool Gender; }; Message Will Be An Optional Argument - Display Is Overloaded 15

Add The Documentation & Display Method maybe below destructor? # pragma region STUDENT_DIAGNOSTIC_LEVEL_2 Display //========================== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Display // // // // Purpose : If there is a message, display it --> Return. // // Then display each of the private data members, one to a line. The // // output should look like: // // // // Optional Message... // // Name...: Tom Hicks // // No...: 1207 // // Gender.: Male // // ---- Skip a line ----. // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// void Student::Display(char Message[]) { } # pragma endregion Without Looking Ahead, Write The Code For This Method/Function 16

Add The Display Method maybe below destructor? # pragma region STUDENT_DIAGNOSTIC_LEVEL_2 Display //====================== void Student::Display(char Message[]) { if (strlen(message) > 0) puts(message); printf ("Name...: %-s\n", Name); printf ("No...: %ld\n", No); if (Gender == FEMALE) puts("gender.: Female\n\n"); else puts("gender.: Male \n\n"); } # pragma endregion If There Is No Optional Message, Print Nothing! 17

18

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_2 Display //======================= if (STUDENT_DIAGNOSTIC_LEVEL <= 2) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Display =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 2 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Student2("Pete", 2222, MALE), Student3("Sandra", 3333), Student4("Amber"), Student5(5555, "Nathan", MALE), Student6(6666, "Betty"), Student7(7777), Class[4]; Diagnostic Level 1 Tested All 8 Of The Ways To Test Our Two Constructors The Constructor Is Overloaded! } HitCarriageReturnToContinue(); 19

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_2 Display //======================= if (STUDENT_DIAGNOSTIC_LEVEL <= 2) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Display =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 2 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Student2("Pete", 2222, MALE), Student3("Sandra", 3333), Student4("Amber"), Student5(5555, "Nathan", MALE), Student6(6666, "Betty"), Student7(7777), Class[4]; Student1.Display ("This Is Student1"); Student2.Display (); Student3.Display ("This Is Student3"); Student4.Display ("This Is Student4"); Student5.Display ("This Is Student5"); Student6.Display ("This Is Student6"); Student7.Display ("This Is Student7"); for (int ClassNo = 0; ClassNo <= 3; ClassNo ++) Class[ClassNo].Display(); Diagnostic Level 2 Is Going To Verify That All 8 Of The Possible Constructors Were Successful! The Display Is Overloaded! } HitCarriageReturnToContinue(); 20

Examine Output Carefully To Make Sure It Works Don t Assume 1 Code That Generated Output Student1.Display ("This Is Student1"); How The Object Was Created Student Student1, Look At The Constructor Definition *Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); In the beginning of the class, I am going to provide you lots of test code. At the end, you will do your own. Several Folks, last semester, received B's & C's on their labs because they did not bother to examine the output correctly & make obvious corrections. 21

Examine Output Carefully To Make Sure It Works Don t Assume 2 Code That Generated Output Student2.Display ( ); Tested Passing Nothing! How The Object Was Created Student Student2("Pete", 2222, MALE), Look At The Constructor Definition *Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); CHECK THEM ALL OUT CAREFULLY! We Want To Test Our Classes Carefully! Add Them To Our Library! Reuse Them Like Crazy!

Examine Output Carefully To Make Sure It Works Don t Assume 3 Code That Generated Output for (int ClassNo = 0; ClassNo <= 3; ClassNo++) Class[ClassNo].Display(); How The Object Was Created Student Class[4]; Look At The Constructor Definition *Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); It Works For Arrays!

Now That I Am Confident That The Display Works I Can Collapse The Display Function And Prepare To Do Diagnostic Level 3 Function Set I Can Also Collapse The Testing Code For Diagnostic Level 2 24

25

# define STUDENT_DIAGNOSTIC_LEVEL 3 int main (int argc, char * argv[]) { puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("++ Class Student Added To Main - Phase 2 ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); puts ("--------------------- Start Of Main ---------------------\n"); TestStudent(); Make The Following Changes To main COMPILE THE PROGRAM! } puts ("----------------------- HitCarriageReturnToContinue(); return (0); End Of Main ----------------------\n"); Change the Diagnostic Level 26

27

Add A Prototype For Set // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); private: char Name[20]; long No; bool Gender; }; There Are 3 Optional Arguments - Method Set Is Overloaded! 28

Add The Documentation & Set Method maybe below Display? # pragma region STUDENT_DIAGNOSTIC_LEVEL_3 Set//=============================== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Set // // // // Purpose : Assign the following: // // // // Name = NewName // // No = NewNo // // Gender = NewGender // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// void Student:: Set (char NewName[], long int NewNo, bool NewGender) { } # pragma endregion Remember: No Default Arguments In Function/Method Itself Without Looking Ahead, Write The Code For This Method/Function 29

Add The Set Method maybe below Display? # pragma region STUDENT_DIAGNOSTIC_LEVEL_3 Set//============================= void Student:: Set (char NewName[], long int NewNo, bool NewGender) { strcpy_s(name, NewName); No = NewNo; Gender = NewGender; } # pragma endregion We Need To Test It 30

31

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_3 Set//=========================== if (STUDENT_DIAGNOSTIC_LEVEL <= 3) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Set =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 3 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Student2("Pete", 2222, MALE), Class[4]; HitCarriageReturnToContinue(); } # pragma endregion Set Is Overloaded We Need Test Code To Test All Of The Possibilities 32

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes Student Student1, Student2("Pete", 1234, MALE), Class[4]; Student1.Set("Alpha", 111, FEMALE); Student1.Display("Student1 Set With Alpha, 111, FEMALE"); Class[0].Set("Beta", 222); Class[0].Display("Class[0] Set With Beta, 222"); Class[1].Set("Gamma"); Class[1].Display("Class[1] Set With Gamma"); Student1.Set(); Student1.Display("Student1"); HitCarriageReturnToContinue(); } # pragma endregion 33

Examine Output Carefully To Make Sure It Works Don t Assume Student Student1, Student2("Pete", 2222, MALE), Class[4]; Student1.Set("Alpha", 111, FEMALE); Class[0].Set("Beta", 222); Class[1].Set("Gamma"); Student1.Set(); 34

35

Accessors Accessor - A method that examines, but not change the state of an object. // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); }; void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Display Is An Accessor 36 36

Mutators Mutator- A method that changes the state of an object. // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); }; void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Set Is An Mutator 37 37

38

Overloaded Functions/Methods Overloaded Functions are functions/methods that accept more than one signature. // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student (long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); }; void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Display, Set, & Both Constructors Are Overloaded 39 39

Optional & Default Arguments Default Arguments are Optional Arguments One Way To Overload A Function Is To Use Optional Arguments // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student (long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); }; void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Display, Set, & Both Constructors Contain Optional Arguments 40 40

41

Revisit Set Suppose We Would Like To Pass Method Set Any Combination Of A Character String, A Long & A Boolean Mundane Problem 42 42

We Could Do16 Different Functions? void Set (char NewName[], long NewNo, bool NewGender); (s,l,b) void Set (char NewName[], bool NewGender, long NewNo); (s,b,l) void Set (long NewNo, bool NewGender, char NewName[]); (l,b,s) void Set (long NewNo, char NewName[], bool NewGender); (l,s,b) void Set (bool NewGender, char NewName[], long NewNo); (b,s,l) void Set (bool NewGender, long NewNo, char NewName[]); (b,l,s) void Set (char NewName[], long NewNo); (s,l) void Set (char NewName[], bool NewGender); (s,b) void Set (long NewNo, bool NewGender); (l,b) void Set (long NewNo, char NewName[]); (l,s) void Set (bool NewGender, char NewName[]); (b,s) void Set (bool NewGender, long NewNo); (b,l) void Set (char NewName[]); (s) void Set (long NewNo); (l) void Set (bool NewGender); (b) void Set (); (-) 43

What Is The Least Number Of Functions That Would Be Required? Solve This Later Without Looking At The Solution void Set (char NewName[]= "", long NewNo = 0, bool NewGender = MALE); (s,l,b) (s,l) (s) ( ) void Set (char NewName[], bool NewGender, long NewNo = 0); (s,b,l) (s,b) void Set (long NewNo, bool NewGender = MALE, char NewName[] = ""); (l,b,s) (l,b) (l) void Set (long NewNo, char NewName[], bool NewGender = MALE); (l,s,b) (l,s) void Set (bool NewGender, char NewName[] = "", long NewNo = 0); (b,s,l) (b,s) (b) void Set (bool NewGender, long NewNo, char NewName[] = ""); (b,l, s) (b,l) You Would Need 6 Set Functions More Than One Set Of Solutions I Am Not Going To Code &Test This But You May If You Like? 44

45

main() { int x = 3; Student John, Sarah("Sarah", 111, FEMALE); } About Scope if (x <= 5) { Student Fred("Fred", 222, MALE"); Fred.Display(); x ++; } x --; John & Sarah Created In This Scope Fred Created In This Scope Fred Destructor Called Here! John & Sarah Created In This Scope 46

47

# define STUDENT_DIAGNOSTIC_LEVEL 4 int main (int argc, char * argv[]) { puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("++ Class Student Added To Main - Phase 2 ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); puts ("--------------------- Start Of Main ---------------------\n"); TestStudent(); Make The Following Changes To main COMPILE THE PROGRAM! } puts ("----------------------- HitCarriageReturnToContinue(); return (0); End Of Main ----------------------\n"); Change the Diagnostic Level 48

49

Add A Prototype For Get // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); bool Get (void); private: char Name[20]; long No; bool Gender; }; 50

Add The Documentation & Get Method maybe below Set? # pragma region STUDENT_DIAGNOSTIC_LEVEL_4 Get//=============================== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Get // // // // Purpose : Set is an interactive function in which we will allow the user // // enter data from keyboard. // // The prompt for the First Data Member will be: // // // // Enter Name [Hit Return/Enter To Exit]: // // // // This provides the user with an opportunity to exit the function // // without entering all of the data members; after all, they may // // have entered this function by mistake. // // // // This function will explicitly returns INVALID if user chooses to // // opt out by hitting the return key; otherwise return VALID after // // the user has entered all of the data members. Your processing // // must be exactly as shown below: // // // // Enter Name [Hit Return/Enter To Exit]: Jane Doe // // Enter No.............. : 1234 // // Enter Gender [M/F]......... : F // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 51

# pragma region STUDENT_DIAGNOSTIC_LEVEL_4 Get//================= bool Student::Get(void) { char Temp[200], GenderChar; Code The Get Method 1/2 maybe below Set? Reading Into Temp & Copying Into Name Prevents The User From Overflowing The Name flush_stream(stdin); printf("\nenter Name [Hit Return To Exit].: "); gets_s(temp); if (strlen(temp) == 0) return (INVALID); strncpy_s(name, Temp, 19); Name[19] = 0; flush_stream(stdin); printf("\nenter No...: "); scanf("%d", &No); Without Looking Ahead, Write The Code For This Method/Function 52

Code The Get Method 2/2 maybe below Set? flush_stream(stdin); printf("\nenter Sex [M/F]...: "); scanf("%c", &GenderChar); if (GenderChar == 'M') Gender = MALE; else Gender = FEMALE; flush_stream(stdin); return (VALID); } # pragma endregion 53

54

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_4 Set//=========================== if (STUDENT_DIAGNOSTIC_LEVEL <= 4) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Get =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 4 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Class[4]; HitCarriageReturnToContinue(); } # pragma endregion 55

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes Student Student1, Student2("Pete", 1234, MALE), Class[4]; if (Student1.Get()) Student1.Display("\nStudent1:"); else puts("\nyou Chose Not To Enter Data For Student1\n"); if (Class[3].Get()) Class[3].Display("\nClass[3]:"); else puts("\nyou Chose Not To Enter Data For Class[3]\n"); HitCarriageReturnToContinue(); } # pragma endregion 56

Examine Output Carefully To Make Sure It Works Don t Assume String Overflow Was Prevented! -- Enter Key To Exit Worked! 57

58

# define STUDENT_DIAGNOSTIC_LEVEL 5 int main (int argc, char * argv[]) { puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("++ Class Student Added To Main - Phase 2 ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); puts ("--------------------- Start Of Main ---------------------\n"); TestStudent(); Make The Following Changes To main COMPILE THE PROGRAM! } puts ("----------------------- HitCarriageReturnToContinue(); return (0); End Of Main ----------------------\n"); Change the Diagnostic Level 59

60

// ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); bool Get (void); void Display50(void); private: char Name[20]; long No; bool Gender; }; Add A Prototype For Display50 61

Add The Documentation & Display50 Method maybe below Get? # pragma region STUDENT_DIAGNOSTIC_LEVEL_5 Display50//========================== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Display50 // // // // Purpose : This is nothing more than a diagnostic display utility that will // // display the most important 50 characters of the class. We will // // develop several such utilities. Selecting the items to display // // will often be tough in a class with 100 data members, but it is // // quite easy for this class. This function uses puts & printf. // // // // There will be no line feed. The format is to be [32 char for // // Name, 2 blanks, 8 char for No, 2 blanks, and the specified // // Gender {i.e. Male or Female}. // // // // 12345678901234567890123456789012345678901234567890 // // -------------------------------------------------- // // Jane Doe 12345678 Female // // // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// ostream & operator << (ostream & OutputStream, Student S) { } # pragma endregion 62

Code The << Overload Method maybe below Set? ostream & operator << (ostream & OutputStream, Student S) { char TempName[50]; strncpy(tempname, P.Name, 31); TempName[31] = 0; flush_stream(stdout); OutputStream << setw(32) << left << TempName; OutputStream << " " << setw(8) << P.No << " "; if (P.Gender == FEMALE) OutputStream << "Female "; else OutputStream << "Male "; return (OutputStream); } # pragma endregion 63

64

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_5 Set//=========================== if (STUDENT_DIAGNOSTIC_LEVEL <= 5) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Display50 =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 5 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student2("Pete", 123456, MALE), Student5(5555, "Jane", FEMALE); HitCarriageReturnToContinue(); } # pragma endregion 65

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes Student Student2("Pete", 123456, MALE), Student5(5555, "Jane", FEMALE); puts("------------------------------------------------------"); puts (" 12345678901234567890123456789012345678901234567890 "); puts ("------------------------------------------------------"); printf (" "); Student2.Display50(); puts (" "); puts ("------------------------------------------------------"); printf (" "); Students5.Display50(); puts (" "); HitCarriageReturnToContinue(); } # pragma endregion 66

Examine Output Carefully To Make Sure It Works Don t Assume 67

68

# define STUDENT_DIAGNOSTIC_LEVEL 6 int main (int argc, char * argv[]) { puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("++ Class Student Added To Main - Phase 2 ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); puts ("--------------------- Start Of Main ---------------------\n"); TestStudent(); Make The Following Changes To main COMPILE THE PROGRAM! } puts ("----------------------- HitCarriageReturnToContinue(); return (0); End Of Main ----------------------\n"); Change the Diagnostic Level 69

70

// ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); bool Get (void); void Display50(void); friend ostream & operator << (ostream & OutputStream, Student S); private: char Name[20]; long No; bool Gender; }; Add A Prototype For Overload Of << Note The Friend 71

Add The Documentation & << Overload maybe below Get? # pragma region STUDENT_DIAGNOSTIC_LEVEL_6 cout//=============================== ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // << Overload // // // // Purpose : This is nothing more than a diagnostic display utility that will // // display the most important 50 characters of the class. We will // // develop several such utilities. Selecting the items to display // // will often be tough in a class with 100 data members, but it is // // quite easy for this class. This function uses cout. // // // // There will be no line feed. The format is to be [32 char for // // Name, 2 blanks, 8 char for No, 2 blanks, and the specified // // Gender {i.e. Male or Female}. // // // // 12345678901234567890123456789012345678901234567890 // // -------------------------------------------------- // // Jane Doe 12345678 Female // // // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// void Student::Display50(void) { } # pragma endregion 72

Code The << Overload maybe below Display50? # pragma region STUDENT_DIAGNOSTIC_LEVEL_6 cout//================== ostream & operator << (ostream & OutputStream, Student S) { char TempName[50]; strncpy_s(tempname, S.Name, 31); TempName[31] = 0; flush_stream(stdout); OutputStream << setw(32) << left << TempName; OutputStream << " " << setw(8) << S.No << " "; if (S.Gender == FEMALE) OutputStream << "Female "; else OutputStream << "Male "; return (OutputStream); } # pragma endregion# pragma endregion Be Able To Do But Do Not Memorize - Not On Exam 73

74

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 5 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_6 Cout//========================= if (STUDENT_DIAGNOSTIC_LEVEL <= 6) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing << Overload =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 6 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student2("Pete", 123456, MALE), Student5(5555, "Jane", FEMALE); HitCarriageReturnToContinue(); } # pragma endregion 75

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 1 Make These Changes Student Student2("Pete", 123456, MALE), Student5(5555, "Jane", FEMALE); puts("------------------------------------------------------"); puts(" 12345678901234567890123456789012345678901234567890 "); puts("------------------------------------------------------"); cout << " " << Student2 << " " << endl; puts("------------------------------------------------------"); cout << " " << Student5 << " " << endl; puts("------------------------------------------------------"); HitCarriageReturnToContinue(); } # pragma endregion 76

Examine Output Carefully To Make Sure It Works Don t Assume 77

78

# define STUDENT_DIAGNOSTIC_LEVEL 7 int main (int argc, char * argv[]) { puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("++ Class Student Added To Main - Phase 2 ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); puts ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); puts ("--------------------- Start Of Main ---------------------\n"); TestStudent(); Make The Following Changes To main COMPILE THE PROGRAM! } puts ("----------------------- HitCarriageReturnToContinue(); return (0); End Of Main ----------------------\n"); Change the Diagnostic Level 79

80

// ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set (char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); bool Get (void); void Display50(void); long int Key(void); Add A Prototype For Key friend ostream & operator << (ostream & OutputStream, Student S); private: char Name[20]; long No; bool Gender; }; Write A Function Key That Explicitly Returns A Long Integer 81

Add The Documentation & Code Key maybe below Get? # pragma region STUDENT_DIAGNOSTIC_LEVEL_7 Key//================================ ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Key // // // // Purpose : Explicitly Return a long integer that represents this student. // // In some classes it will be difficult to determine which long // // integer best represents the object; in this class we have but // // one. Explicitly return the No. // // // // Since the data members are generally private, it is not at all // // to have several accessor methods // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// long Student::Key(void) { return (No); } # pragma endregion 82

83

Let Us Start By Making A Copy Of The Test Code For Diagnostic Level 6 Make These Changes # pragma region STUDENT_DIAGNOSTIC_LEVEL_7 Cout//========================= if (STUDENT_DIAGNOSTIC_LEVEL <= 7) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Key =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 7 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student2("Pete", 123456, MALE), Student5(5555, "Jane", FEMALE); printf("student2.key() = %ld\n", Student2.Key()); printf("student5.key() = %ld\n", Student5.Key()); HitCarriageReturnToContinue(); } # pragma endregion 84

Examine Output Carefully To Make Sure It Works Don t Assume 85

86

Accessors Accessor - A method that examines, but not change the state of an object. // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); bool Get(void); void Display50(void); long Key(void); friend ostream & operator << (ostream & OutputStream, Student S); 87 87

Mutators Mutator- A method that changes the state of an object. // ---------------------- Classes & Structs -------------------------- class Student { public: Student(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); Student(long NewNo, char NewName[] = "", bool NewGender = MALE); ~Student(void); void Display(char Message[] = ""); void Set(char NewName[] = "", long int NewNo = 0, bool NewGender = MALE); bool Get(void); void Display50(void); long Key(void); friend ostream & operator << (ostream & OutputStream, Student S); 88 88