1. 2 2 Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A...

download 1. 2 2 Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A Text Editor 3.

If you can't read please download the document

description

Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A Text Editor 3

Transcript of 1. 2 2 Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A...

1 2 2 Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A Text Editor 3 I Am Going To Use Programmers Notepad 2 (freeware) I Am Going To Grab Code From This File Throughout This Tutorial. I Would Be More Apt To Confuse The Files If Both Were Open In Visual Studio. 4 5 5 ////////////////////////////////////////////////////////////////////////////////// // // main.cpp // // // Purpose : Learn To Partition A Class Into A Good Interface -.hpp &.cpp // // // Written By : Dr. Thomas E. Hicks Environment : Windows 7 // // Date : xx/xx/xxxx Compiler : Visual C++ // ////////////////////////////////////////////////////////////////////////////////// Add The Following Documentation I Have Enabled You To Copy/Paste This, & Many Of The Other Documentation Boxes; Come Back & Do It After Lecture! 6 7 Right-Mouse Click On Project Select Add Select New Item 8 Select A Visual C++ Header File Call It Student.hpp 9 Add The Following Documentation Block (you may copy from next slide) 10 Add The Student.hpp Documentation Block ////////////////////////////////////////////////////////////////////////////////// // // Student.hpp // // // Purpose : Create the Interface for the Student Class. The interface // // shall include the includes, defines, class description and // // prototypes that the compiler needs in order to compile // // and execute Student objects. // // // Written By : Dr. Thomas E. Hicks Environment : Windows 7 // // Date : xx/xx/xxxx Compiler : Visual C++ // ////////////////////////////////////////////////////////////////////////////////// 11 12 Major Class Components Of.hpp (in order) 1] All Includes essential to the class 2] All Defines essential to the class 3] All Struct and Class definitions 4] Prototypes for all Non-Class Functions 5] All Template Functions 13 About Interfaces An Interface (.hpp &.cpp) Should Contain A Logical Collection Of Related Information. Creating An Interface For Students Is Both Logical & Appropriate. This Interface Will Include All Of The Data & Functions Related To Students. An Application Often Involves Combining Hundreds/Thousands Of Interface Files. 14 Potential Problem int x = 5, y = 2; if (x > 3) { int x = y * 2; This Code Breaks! WHY? Multiple Declaration Of Same Variable. Could Also Have A Problem If Wrote Code For The Same Function Twice! This Is Exactly What Can Happen With An Application That Is Comprised Of Many Interfaces. It Might Be The Case That Several Of The Interfaces Include Utilities.hpp (or Some Other Interface) ; We Have To Avoid The Duplication Of Variables & Functions! (or we can't compile!) 15 # ifndef PART_CLASS //================================================== # define PART_CLASS # endif // PART_CLASS ================================================= To Start The.hpp Interface For Class Part If I Were To Ask You Why Each.hpp File Should 1] begin the compiler directive # ifndef [if not defined] ? 2] & end with compiler directive # endif [end if] ? The #ifndef, #define, and #endif help to prevent multiple redefinitions of variables, classes, constants, & functions. 16 # ifndef STUDENT_CLASS //=============================================== # define STUDENT_CLASS # endif //STUDENT_CLASS ============================================= Start The.hpp Interface For Class Student 17 # ifndef STUDENT_CLASS //========================================================= // Includes # include "utilities.hpp # define STUDENT_CLASS # endif //STUDENT_CLASS //======================================================= Add All Of The Includes For Class Student 18 # ifndef STUDENT_CLASS //========================================================= // Includes # include "utilities.hpp // Defines # define STUDENT_CLASS # define MALE false # define FEMALE true # endif //STUDENT_CLASS //======================================================= Add All Of The Defines For Class Student # define STUDENT_DIAGNOSTIC_LEVEL 2 19 # ifndef STUDENT_CLASS //========================================================= // Includes # include "utilities.hpp // Defines # define STUDENT_CLASS # define STUDENT_DIAGNOSTIC_LEVEL 2 # define MALE false # define FEMALE true ////////////////////////////////////////////////////////////////////////////////// // Class Student // ////////////////////////////////////////////////////////////////////////////////// class Student { }; # endif //STUDENT_CLASS //======================================================= Add The Class Definition 20 # ifndef STUDENT_CLASS //========================================================= // Includes # include "utilities.hpp // Defines # define STUDENT_CLASS # define STUDENT_DIAGNOSTIC_LEVEL 2 # define MALE false # define FEMALE true ////////////////////////////////////////////////////////////////////////////////// // Class Student // ////////////////////////////////////////////////////////////////////////////////// class Student { }; # endif //STUDENT_CLASS //======================================================= There Are No Template Methods We Will Get To Template Methods Within The Next Week 21 What is in the.h or.hpp Interface? 1] All includes essential to the class 2] All defines essential to the class 3] All struct and class definitions 4] Prototypes for all Functions/Procedures 5] All Template Functions/ Procedures/ Methods 22 23 Right-Mouse Click On Project Select Add Select New Item 24 Select A Visual C++ File Call It Student.cpp 25 Add The Following Documentation Block (you may copy from next slide) 26 Add The Student.cpp Documentation Block ////////////////////////////////////////////////////////////////////////////////// // // Student.cpp // // // Purpose : Create the Student Class that will contain all Student // // methods and all non-template functions. // // // Written By : Dr. Thomas E. Hicks Environment : Windows 7 // // Date : xx/xx/xxxx Compiler : Visual C++ // ////////////////////////////////////////////////////////////////////////////////// 27 28 1] An Include Statement for the Interface File 2] All Non-Template Functions/Methods 29 Major Class Components Of.cpp (in order) Add An Include For This Interface (.hpp) # include "Student.hpp" 30 Add All Of The Student Methods # include "Student.hpp" more 31 Add All Of The Non-Template Functions # include "Student.hpp" more 32 What is in the.c or.cpp Interface? 1] Include the interface file 2] All Non-Template Functions/ Procedures/ Methods 33 Program Should Compile! 34 35 Add A Function Call To TestStudent Even Though Files In Project - Will Not Work! 36 Add The Include for Student.hpp Required For Every File That Uses Students! 37 38 39 Why Partition Classes Into.hpp &.cpp Files? When a program is interfaced properly into.hpp and.cpp files: 1] Compilation Is Faster compilers only compile those components that have been altered since the last compilation 2] Easier To Partition A Project For Teams 3] Reuse Opportunities Reduces Project Time/Cost - reuse opportunities increase when one develops and tests the class so well so general/generic that it can used in other projects 4] Assists In Better Design it is difficult, if not impossible, to keep all of the details of a large system in mind at one time by creating one self contained student class interface that has all of the functionality necessary for students, one can more easily abstract the Student object. 40 class Name { public: Name (char NewFirst[]="", char NewLast[]=""); private: char *First, *Last; long No; }; Indigenous Data - Data that is completely contained by the structure. (i.e. No) Exogenous Data - Data that is not part of the structure, but accessed through pointers that are in the structure. (i.e. First & Last) 41 42 Place A Copy Of Your Project Folder In C:\Temp Name The Folder Test! 43 This Should Be Your Main 44 Copy-Paste Copy ////////////////////////////////////////////////////////////////////////////////// // // File Main.cpp // // // Purpose : // // // Written By : Dr. Thomas E. Hicks Environment : Windows 7 // // Date : xx/xx/xxxx Compiler : Visual C++ // ////////////////////////////////////////////////////////////////////////////////// # include "Utilities.hpp" int main(int argc, char * argv[]) { int x = 5, y = 6; printf ("x = %d\n", x); printf ("y = %d\n", y); printf ("My Name Is %s\n\n", "Tom"); getchar(); return(0); } 45 Program Output - Not Real Impressive! 46 47 Open The Project Debug Folder Double-Click On The Executable Project.exe 48 Open The Project Debug Folder Double-Click On The Executable Project.exe 49 Program Output Outside Visual Studio Environment! - Not Real Impressive! 50 51 Copy The Executable File Location To The Clipboard CTRL-C 52 Launch A Command Window! 53 Enter cd (change directory) Right-Mouse-Click On Desktop - Select Paste cd Exactly Like Linux 54 We Are Now In Folder Housing Project.exe Type Project Hit Enter Key 55 You Could Path To The Executable From Any Directory Location On The Binary Executable, Entering The.exe Extension Is Optional! Hit Return Key A Couple Times! 56 We Are Now In Folder Housing Project.exe Type Project Hit Enter Key 57 58 Exactly Like Linux 59 Redirecting The Output Is Just Like We Do With C Open The Output File 60 There Will Be Times I Want You To Print The Output From A Program. 61 62 Change Your Main To Include I/O 63 Copy-Paste Copy int main(int argc, char * argv[]) { int x, y; printf ("\nEnter x: "); scanf("%d", &x); fflush(stdin); printf ("\nEnter y: "); scanf("%d", &y); fflush(stdin); printf("\n"); printf ("x = %d\n", x); printf ("y = %d\n", y); printf ("My Name Is %s\n\n", "Tom"); getchar(); return(0); } 64 I Start The Program But Where Is My Prompt for x? Going To The Output File Of Course! 65 Trickier! I have to know my program well enough to enter x & y without being asked! 66 Don't See Input & Output Together! "Type" is windows equivalent of Linux "cat" 67 Clearing The Console Window "cls" is windows equivalent of Linux "clear" 68 69 Matrix - Two dimensional array int X [5][2]; int x [NoRows] [NoCols]; STL - Standard Template Library - Has Libraries For String, Vector, Stacks, etc. 70 71