随意建立一个文件夹 用VS code 打开该文件夹而不是文件
然后随意写个包含类定义的头文件(保存在刚建立的新文件夹)
如代码块1
// Fig. GradeBook.h
#include <iostream>
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook(string name)
{
setCourseName(name);
}
void setCourseName(string name)
{
courseName = name;
}
string getCourseName()
{
return courseName;
}
void dispalyMessage()
{
cout << "Welcome to the grade book for\n"
<< getCourseName() << "!" << endl;
}
private:
string courseName;
};
接下来在用使用main函数的cpp文件中引用即可
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main(int argc, char const *argv[])
{
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
cout << "gradeBook1 created for course : " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course : " << gradeBook2.getCourseName() << endl;
return 0;
}
网友评论