昨天把所有程序码都写在一起包括class与程序进入点main(),但这样做有个缺点就是会暴露原始码, C/C++算是一门很老的语言当时的风气倾向保密细节只留下.h标头档展示类别的介面(interface of class)如下GradeBook.h,而实作会放在.cpp档中先编译好,这样就可以避免使用者看到实作细节。
//GradeBook.h
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
class GradeBook
{
public:
GradeBook( string ); // constructor that initializes courseName
void setCourseName( string ); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage(); // function that displays a welcome message
private:
string courseName; // course name for this GradeBook
};
//GradeBook.cpp
#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
}
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
if ( name.length() > 25 ) // if name has more than 25 characters
{
// set courseName to first 25 characters of parameter name
courseName = name.substr( 0, 25 ); // start at 0, length of 25
cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
<< "Limiting courseName to first 25 characters.\n" << endl;
} // end if
}
string GradeBook::getCourseName()
{
return courseName; // return object's courseName
}
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
}
<<: Day 3 [Python ML] 选择建模用的资料(DecisionTree)
初探 LeetCode 的操作与使用 注册登入 LeetCode 之後,可以从 Problems ...
专案的档案结构规划好之後, 就可以依照每支档案负责的工作填好对应的内容再组装起来。如果在组装之前能确...
接续昨天的练习:https://ithelp.ithome.com.tw/articles/1026...
执行环境就是当前Javascript代码被执行时所在的环境,Javascript在运行任何代码都是在...
文件 原文文件:Page migration 翻译: 核心中使用 migrate_pages() =...