COSC 181 Foundations of Computer Programming Class 6 Defining the GradeBook Class Line 9 17 public indicates that the affected GradeBook class definition part of the class members and class GradeBook functions are accessible by functions outside the class i e public main function that displays a message void 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 bracket 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 1 GradeBook myGradeBook GradeBook type is defined because we included the GradeBook class in the code myGradeBook 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 Operations Defining Member Function w Parameters include string allows use of string type using std string using std getline Fig 3 3 Lines 16 22 public function displays a welcome message void displayMessage string courseName cout Welcome to the grade book for n courseName endl main function lines 26 40 int 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 Programming Welcome to the grade book for COSC181 C Programming Function Definition and Calls Note Function Definition void displayMessage string courseName Function Call myGradeBook displayMessage nameOfCourse Good Programming Practices Use different names for passed arguments and corresponding variables in function definition Use meaningful function and parameter identifiers UML Class Diagram Updated Remember Class Name Class Attributes Class Methods Data Members set and get Functions Fig 3 5 lines 15 40 class 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 function int 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 Input Initial course name is Please enter the course name COSC181 C Programming Welcome to the grade book for COSC181 C Programming Additional Main Function int 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 Programming Please enter the course name of 381 COSC381 C Programming Welcome to the grade book for COSC181 C Programming Welcome to the grade book for COSC381 C Programming GradeBook 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 stored GradeBook Updated Class Diagram Name Class Attributes means its private Class Member Functions means its public include parameters here include type if any after here Try It Type in and test Fig 3 5 Compile and Run When you are done do not close DevC In Class Exercise Modify class GradeBook 1 2 3 4 Include a second string data member that represents the course instructor s name Provide a set function to change the instructor s name Modify function displayMessage to output the welcome message and course name then the string This course is presented by followed by the instructor s name Compile and Run with the modified class Output Initial course name is C Instructor s name is Dr Ray Welcome to the grade book for C This course is presented by Dr Ray
View Full Document