比上一个版本添加了一个计算班级平均分的函数determineClassAverage()
还是3个文件
GradeBook.h
#ifndef GRADEBOOK_H #define GRADEBOOK_H // GradeBook.h // GradeBook类展示了该类的公共接口。成员函数出现在GradeBook.cpp中 #include<string> // 该程序使用C++标准的string类 using std::string; // GradeBook类的定义 class GradeBook { public: /** 构造函数初始化GradeBook对象 */ GradeBook( string ); /** Access courseName * 返回courseName */ string getCourseName(); /** Set courseName * 设定courseName的值 */ void setCourseName(string ); void displayMessage(); // 显示一条欢迎信息 void determineClassAverage(); // 计算用户输入的成绩的平均分 private: string courseName; //这个GradeBook的课程名称 }; // GradeBook类定义结尾 #endif // GRADEBOOK_H
GradeBook.cpp
// GradeBook.cpp // Implementation of the GradeBook member-function definitions. // The setCourseName function performs validation #include<iostream> using std::cout; using std::endl; using std::cin; #include "GradeBook.h" // include definition of class GradeBook // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) { setCourseName( name ); // validate and store courseName } // end GradeBook constructor // function that sets the course name // ensures that the courseName 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 courseName if( name.length() > 25 ) // if name has more than 25 characters { courseName = name.substr( 0, 25 ); // start at 0, length of 25 cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "Limiting courseName to the first 25 characters.\n" << endl; } } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return courseName; // return object‘s courseName } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // call the getCourseName to get the coureName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << 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 sum 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 += grade; // add grade to total gradeCounter++; // 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
测试文件 main.cpp
// main.cpp // Create GradeBook object and invoke its determineClassAverage function. #include "GradeBook.h" // include definition of class GradeBook // function main begins program execution int main() { // Create GradeBook object myGradeBook and // pass course name to constructor GradeBook myGradeBook( "CS101 C++ Programming!" ); myGradeBook.displayMessage(); // display welcome message myGradeBook.determineClassAverage(); // find average of 10 grades return 0; // indicate successful termination } // end main
软件工程知识
常见的编程错误
错误预防技巧
良好的编程习惯
原文:http://www.cnblogs.com/tmmuyb/p/3762956.html