ClassExamples.doc - Example 1: #include <iostream> #include...

This preview shows page 1 - 5 out of 13 pages.

The preview shows page 3 - 5 out of 13 pages.
Example 1:#include <iostream>#include <conio>class GradeBook{void display(){cout<<"Welcome to the Grade Book!\n";}};void main(){GradeBook myGradeBook;myGradeBook.display();getch();}Example 2:#include <iostream>#include <conio>class GradeBook{public: void display(){cout<<"Welcome to the Grade Book!\n";}};void main(){1
GradeBook myGradeBook;myGradeBook.display();getch();}Example 3:#include <iostream>#include <conio>class GradeBook{public:void display();};void GradeBook:: display(){cout<<"Welcome to the Grade Book!\n";}void main(){GradeBook myGradeBook;myGradeBook.display();getch();}2
Defining a Member Function with a ParameterExample 4:#include <iostream>#include <conio>#include <string> // program uses C++ standard string class// GradeBook class definitionclass GradeBook{public:// function that displays a welcome message to the GradeBook uservoid displayMessage( string courseName ){cout << "Welcome to the grade book for\n" << courseName << "!"<< endl;} // end function displayMessage}; // end class GradeBookvoid main(){string nameOfCourse; // string of characters to store the course nameGradeBook myGradeBook; // create a GradeBook object named myGradeBook// prompt for and input course namecout << "Please enter the course name:" << endl;std::getline( cin, nameOfCourse, '\n' ); // read a course name with blankscout << endl; // output a blank line// call myGradeBook's displayMessage function and pass nameOfCourse as an argumentmyGradeBook.displayMessage( nameOfCourse );getch();}GradeBook Class with a Data Member, a set Function and a get FunctionIn our next example, classGradeBookmaintains the course name as a data member so that itcan be used or modified at any time during a program's execution. The class contains memberfunctionssetCourseName,getCourseNameanddisplayMessage. Member functionsetCourseNamestores a course name in aGradeBookdata membermember functiongetCourseNameobtains aGradeBook's course name from that data member. MemberfunctiondisplayMessagewhich now specifies no parametersstill displays a welcomemessage that includes the course name. However, as you will see, the function now obtainsthe course name by calling another function in the same classgetCourseName.Example 5:#include <iostream>#include <conio>#include <string> // program uses C++ standard string class3
// GradeBook class definitionclass GradeBook{public:// function that sets the course namevoid setCourseName( string name ){courseName = name; // store the course name in the object} // end function setCourseName// function that gets the course namestring getCourseName(){return courseName; // return the object's courseName} // end function getCourseName// function that displays a welcome messagevoid displayMessage(){// this statement calls getCourseName to get the// name of the course this GradeBook representscout << "Welcome to the grade book for\n" << getCourseName() << "!"<< endl;} // end function displayMessageprivate:string courseName; // course name for this GradeBook}; // end class GradeBook// function main begins program executionvoid main(){string nameOfCourse; // string of characters to store the course nameGradeBook myGradeBook; // create a GradeBook object named myGradeBook

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 13 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Summer
Professor
Dr Suzan
Tags
Object Oriented Programming, Method, Subroutine

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture