把类的声明和定义分开写,然后在主函数中通过开放的端口调用
1.先写类的声明放到头文件"Employee.h"里面
#include "iostream"
using namespace std;
class Employee //声明类
{
private:
int age;
int yearsOfService;
int Salary;
public:
Employee(int i_age,int i_yearOfService,int i_Salary);
int GetAge(){ return age;}
int GetYear(){ return yearsOfService;}
int GetSalary(){return Salary;}
void print(){cout<<GetAge()<<endl
<<GetYear()<<endl
<<GetSalary()<<endl;}
};
2.写类的定义部分放到源码文件"Employee.cpp"
//定义构造函数
#include "Employee.h"
Employee::Employee(int i_age,int i_yearOfService,int i_Salary)
{
age=i_age;
yearsOfService=i_yearOfService;
Salary=i_Salary;
}
3.写主函数"main.cpp"
#include "Employee.h" //首先载入头文件
int main(void)
{
Employee C1(1,2,3),C2(4,5,6); //声明对象,因为构造函数带参数,所以要带参数,没有无参数构造函数
C1.print();
C2.print();
return 1;
}