一般情况下 一个头文件 一个源程序 同名 源程序include进来头文件
Student.h
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
//把私有的类内成员变量封装起来
void setName(string name);
string getName();
void setAge(int age);
int getAge();
void study();
private:
string m_strName;
int m_iAge; //定义两个私有的成员变量
};
Student.cpp
#include"Student.h"
void Student::setName(string name)
{
m_strName = name;
}
string Student::getName()
{
return m_strName;
}
void Student::setAge(int age)
{
m_iAge = age;
}
int Student::getAge()
{
return m_iAge;
}
void Student::study() {
cout << "study" << endl;
}
主程序:
#include<iostream>
#include"Student.h"
using namespace std;
int main() {
Student stu;
stu.setName("join");
stu.setAge(20);
cout<<stu.getName() << " " << stu.getAge() << endl;
stu.study();
system("pause");
return 0;
}