COSC 181 Foundations of Computer Programming Class 6
Defining the GradeBook Class Line 9 17 //GradeBook class definition class GradeBook { public: //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 public: indicates that the affected part of the class (members and functions) are accessible by functions outside the class (i.e. main()) 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. Include a second string data member that represents the course instructor s name 2. Provide a set function to change the instructor s name 3. 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. 4. 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