Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16COSC 181 – Foundations of Computer ProgrammingClass 6Defining the GradeBook ClassLine 9 – 17//GradeBook class definitionclass GradeBook{public://function that displays a messagevoid displayMessage(){cout << “Welcome to the Grade Book!” << endl;}};Classes have identifiers, just like variables Follow same rulesBy convention, class identifiers begin with a capital letter.Uses “camel case” notation FirstSecondEasier to readEvery class is enclosed in { }, with a ; after the closing bracketpublic: indicates that the affected part of the class (members and functions) are accessible by functions outside the class (i.e. – main())Using the GradeBook class in a programWant to “call” displayMessage() to display the message on the screenCan’t call a member function until you create an instance of the classLines 22 and 23 in Fig 3.1GradeBook myGradeBook;“GradeBook” type is defined because we included the GradeBook class in the codemyGradeBook.displayMessage();“dot operator”UML: Class DiagramsClasses represented as a rectangle with 3 compartmentsClasses NameClasses Attributes C++ Data Members (more on this later)Classes OperationsDefining Member Function w/ Parameters#include <string> //allows use of string typeusing std::stringusing std::getlineFig 3.3 – Lines 16 -22public:// function displays a welcome messagevoid displayMessage(string courseName){cout << “Welcome to the grade book for \n” << courseName << “!” << endl; }main() function – lines 26-40int main(){string nameOfCourse;GradeBook myGradeBook;cout << “Please enter the course name:” <<endl;getline( cin, nameOfCourse );cout << endl;myGradeBook.displayMessage( nameOfCourse)return 0;}getline(cin, nameOfCourse);Can’t use cin >> nameOfCourse;Output and Input Please enter the course name:COSC181 C++ ProgrammingWelcome to the grade book forCOSC181 C++ ProgrammingFunction Definition and CallsNote:Function Definitionvoid displayMessage(string courseName)Function CallmyGradeBook.displayMessage( nameOfCourse)Good Programming PracticesUse different names for passed arguments and corresponding variables in function definitionUse meaningful function and parameter identifiersUML: Class Diagram (Updated)• Remember• Class Name• Class Attributes• Class MethodsData Members : set and get FunctionsFig 3.5 – lines 15 -40class GradeBook {public:void setCourseName(string name){courseName = name; } string getCourseName(){return courseName;}void displayMessage(){cout << “Welcome to the grade book for\n” << getCourseName() << “!” << endl; }private:string courseName;};New main() functionint main(){string nameOfCourse;GradeBook myGradeBook;cout << “Initial course name is: “ << myGradeBook.getCourseName()<<endl;cout << “\nPlease enter the course name:” << endl;getline( cin, nameOfCourse );myGradeBook.setCourseName(nameOfCourse);cout << endl;myGradeBook.displayMessage();return 0;}New: Output and InputInitial course name is:Please enter the course name:COSC181 C++ ProgrammingWelcome to the grade book forCOSC181 C++ ProgrammingAdditional Main() Functionint main(){string nameOfCourse;GradeBook myGradeBook181;GradeBook myGradeBook381;cout << “\nPlease enter the course name of 181:” << endl;getline( cin, nameOfCourse );myGradeBook181.setCourseName(nameOfCourse);cout << “\nPlease enter the course name of 381:” << endl;getline( cin, nameOfCourse );myGradeBook381.setCourseName(nameOfCourse);cout << endl;myGradeBook181.displayMessage();myGradeBook381.displayMessage();return 0;}OutputWhat is the output of the previous program?Please enter the course name of 181:COSC181 C++ ProgrammingPlease enter the course name of 381:COSC381 C++ ProgrammingWelcome to the grade book forCOSC181 C++ ProgrammingWelcome to the grade book forCOSC381 C++ ProgrammingGradeBook ClassFig 3.5 (pg. 92)private: vs. public:fundamental approach for Object-Oriented design important for Software Engineering purposesCreates a layer of abstraction that protects an objects data from outside codeObject clients may have bugs that could be destructiveIf we don’t use objects with private members then clients have total access to the valuesAlways localize affects if possibleClients need not know how data is being storedGradeBook – Updated Class DiagramNameClass Attributes- means its privateClass Member Functions+ means its public() – include parameters here: - include type (if any) after
View Full Document