Unformatted text preview:

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 ClassLine 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 rulesBy convention, class identifiers begin with a capital letter.Uses “camel case” notation  FirstSecondEasier to readEvery 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 programWant to “call” displayMessage() to display the message on the screenCan’t call a member function until you create an instance of the classLines 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 DiagramsClasses represented as a rectangle with 3 compartmentsClasses NameClasses Attributes C++ Data Members (more on this later)Classes OperationsDefining Member Function w/ Parameters#include <string> //allows use of string typeusing std::stringusing std::getlineFig 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 CallsNote:Function Definitionvoid displayMessage(string courseName)Function CallmyGradeBook.displayMessage( nameOfCourse)Good Programming PracticesUse different names for passed arguments and corresponding variables in function definitionUse meaningful function and parameter identifiersUML: Class Diagram (Updated)• Remember• Class Name• Class Attributes• Class MethodsData Members : set and get FunctionsFig 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;}OutputWhat 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 ClassFig 3.5 (pg. 92)private: vs. public:fundamental approach for Object-Oriented design important for Software Engineering purposesCreates a layer of abstraction that protects an objects data from outside codeObject clients may have bugs that could be destructiveIf we don’t use objects with private members then clients have total access to the valuesAlways localize affects if possibleClients need not know how data is being storedGradeBook – Updated Class DiagramNameClass Attributes- means its privateClass Member Functions+ means its public() – include parameters here: - include type (if any) after


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?