Created
January 3, 2023 22:59
-
-
Save EteimZ/fe9d470d77e81c1f052da9b6190b9d58 to your computer and use it in GitHub Desktop.
GradeBook OOP example from the Book C How to Program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GradeBook Implementation | |
| #include <iostream> | |
| using std::cout; | |
| using std::endl; | |
| #include "GradeBook.h" // include interface | |
| // constructor implementation | |
| GradeBook::GradeBook(string name){ | |
| setCourseName( name ); // validate and store courseName | |
| } | |
| // setCourseName implementation | |
| // ensures that the course name has at most 25 characters | |
| void GradeBook::setCourseName( string name ){ | |
| if ( name.length() <= 25 ) | |
| courseName = name; | |
| if ( name.length() > 25 ){ | |
| courseName = name.substr(0, 25); // start at 0 and end at 25 | |
| cout << "Name \"" << name << "\" exceeds maximum length (25). \n" | |
| << "Limiting courtName to first 25 characters.\n" << endl; | |
| } | |
| } | |
| // getCourseName implementation | |
| string GradeBook::getCourseName(){ | |
| return courseName; // return object's courseName | |
| } | |
| // display a welcome message to the GradeBook user | |
| void GradeBook::displayMessage(){ | |
| cout << "Welcome to the grade book for\n" << getCourseName() | |
| << "!" << endl; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Gradebook interface | |
| #include <string> | |
| using std::string; | |
| class GradeBook{ | |
| public: | |
| GradeBook( string ); // GradeBook constructor | |
| void setCourseName( string ); // setter function | |
| string getCourseName(); // getter function | |
| void displayMessage(); // display welcome message | |
| private: | |
| string courseName; // course name for GradeBook | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GradeBook main program | |
| #include <iostream> | |
| using std::cout; | |
| using std::endl; | |
| #include "GradeBook.h" // include GradeBook header file | |
| int main(){ | |
| // Create two GradeBook objects | |
| GradeBook gradeBook1( "CS101 Introduction to programming in C++" ); | |
| GradeBook gradeBook2( "CS102 C++ Data Structures" ); | |
| cout << "gradeBook1's initial course name is: " | |
| << gradeBook1.getCourseName() | |
| << "\ngradeBook2's initial course name is:" | |
| << gradeBook2.getCourseName() << endl; | |
| gradeBook1.setCourseName( "CS101 C++ Programming" ); | |
| // display each GradeBook's courseName | |
| cout << "\ngradeBook1's course name is: " | |
| << gradeBook1.getCourseName() | |
| << "\ngradeBook2's course name is: " | |
| << gradeBook2.getCourseName() << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment