C++多文件编程--类的声明和实现分开

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++多⽂件编程--类的声明和实现分开类的声明⼀般放在头⽂件中(xx.h),类的实现⼀般放在源⽂件中(xx.cpp)
注意:在VS中,要通过项⽬-->类来添加或右击⼯程--添加;
不能创建⽂件,另存为,这样是错误的
Student.h⽂件:
#include <iostream>
using namespace std;
class Student //类的声明
//包括函数的声明,成员的声明
{
public:
Student(const string& name, int age, int no);//构造函数的声明
void who(void); //⾃定义函数的声明
private:
string m_name;
int m_age;
int m_no;
};
Student.cpp⽂件
#include "Student.h"//导⼊⾃⼰的头⽂件,Student.h是头⽂件名
using namespace std;
Student::Student(const string& name, int age, int no) { //类函数的实⾏
//注意在函数名前⾯加上类名:: ,声明它的作⽤域
//如果不加类名::,它就不是类函数了,是全局函数了
cout << "执⾏构造函数了" << endl;
m_name = name;
m_age = age;
m_no = no;
}
void Student::who(void) {
cout << "我的名字是:" << m_name << endl;
cout << "我的年龄是:" << m_age << endl;
cout << "我的学号是:" << m_no << endl;
}
main⽂件:
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
Student s("张三", 25, 10010);
s.who();
}。

相关文档
最新文档