#include <iostream>
#include <string>
using namespace std;
//定义一个类
class GradeBook
{
public:
void setCourseName( string name)
{
courseName = name;
}
string getCourseName()
{
return courseName;
}
void displayMassage( string courseName )
{
cout << "Welcome to the Grade Book for\n" << getCourseName() << "!" << endl;
}
private:
string courseName;
};
int main()
{
string nameOfCourse;
GradeBook myGradeBook;//创建了一个名字叫myGradeBook的GradeBook的对象
cout << "Initial course name is: " << myGradeBook.getCourseName() << endl;
cout << "\nPlease enter the course name:" << endl;
cin >> nameOfCourse;
myGradeBook.setCourseName(nameOfCourse);
cout << endl;
myGradeBook.displayMassage( nameOfCourse ) ;//使用对象的displayMassage(成员函数)
return 0;
}
C++ GradeBook类定义一个具有形参的成员函数
最新推荐文章于 2024-08-23 21:39:27 发布