首页 > 代码库 > 类和对象-1

类和对象-1

共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();  // 显示一条欢迎信息
    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;

#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

 

测试main.cpp

// main.cpp
// Create and manipulate a GradeBook object; illustrate validation
#include<iostream>
using std::cout;
using std::endl;

#include "GradeBook.h"  // include definition of class GradeBook

// function main begins program execution
int main()
{
    // create two GradeBook objects
    // initialize course name of gradeBook1 is too long
    GradeBook gradeBook1( "CS101 Introduction to Programming in C++" );
    GradeBook gradeBook2( "CS102 C++ Data Structures" );

    // display each GradeBook‘s courseName
    cout << "gradeBook1‘s initial course name is:"
    << gradeBook1.getCourseName()
    << "\ngradeBook2‘s initial course name is:"
    << gradeBook2.getCourseName() << endl;

    // modify gradeBook1‘s courseName with a valid-length string
    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;   // indicate successful termination
}   // end main

 

软件工程知识

  • 把数据成员设置成private,而由public成员函数控制访问数据成员的权利,尤其是写的权利,将有助于保证数据的完整性。
  • 设置private数据的值成员函数应当核实应用中的新值是否正确。如果不是,设置函数应该将private数据成员置于适当的状态中。

 

错误预防技巧

数据完整性的好处不是简单地将数据成员设置成为private就可获得的,程序员必须提供合适的有效性检查并报告错误。