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