#include GradeBook.h物件導向問題

"C 程式語言"和"物件導向程式設計"課程討論區-授課教授:蔡明志老師

版主: 蔡明志

分享到: Facebook

#include GradeBook.h物件導向問題

文章DanielChang » 週日 11月 29, 2015 9:16 pm

我在原文書看到這個,可是我一直不知道它的用處是做什麼?
書上寫是物件導向GradeBook,可是我compile後,#include "GradeBook.h" // include definition of class GradeBook這一個過不了,他說找不到這個檔案?
可以幫我解答一下嗎?
太感謝了~

#include <string> // program uses C++ standard string class
using namespace std;

// GradeBook class definition
class GradeBook
{
public:
GradeBook( string ); // constructor initializes course name
void setCourseName( string ); // function to set the course name
string getCourseName(); // function to retrieve the course name
void displayMessage(); // display a welcome message
void determineClassAverage(); // averages grades entered by the user
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook

#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
setCourseName( name ); // validate and store courseName
} // end GradeBook constructor
// function to set the course name;
// ensures that the course name has at most 25 characters void GradeBook::setCourseName( string name )
{
if ( name.length() <= 25 ) // if name has 25 or fewer characters courseName = name; // store the course name in the object
else // if name is longer than 25 characters
{ // set courseName to first 25 characters of parameter name
courseName = name.substr( 0, 25 ); // select first 25 characters cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
<< "Limiting courseName to first 25 characters.\n" << endl; } // end if...else
} // end function setCourseName
// function to retrieve the course name
string GradeBook::getCourseName()
{
return courseName;
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage() {
cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl;
} // end function displayMessage
// determine class average based on 10 grades entered by user
void GradeBook::determineClassAverage() {
int total; // sum of grades entered by user
int gradeCounter; // number of the grade to be entered next int grade; // grade value entered by user
int average; // average of grades
// initialization phase
total = 0; // initialize total
gradeCounter = 1; // initialize loop counter
// processing phase
while ( gradeCounter <= 10 ) // loop 10 times {
cout << "Enter grade: "; // prompt for input
cin >> grade; // input next grade
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter by 1
} // end while

// termination phase
average = total / 10; // integer division yields integer result

// display total and average of grades
cout << "\nTotal of all 10 grades is " << total << endl;
cout << "Class average is " << average << endl;
} // end function determineClassAverage
DanielChang
剛學走路的小朋友
 
文章: 1
註冊時間: 週日 11月 29, 2015 9:06 pm

回到 C/OOP 課程討論區

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 2 位訪客