目录
函数分文件编写
text.h文件代码:
#pragma once
#include<iostream>
using namespace std;
void show();
int sumall(int a, int b);
text.cpp文件代码:
#include"text.h"
void show() {
cout << "函数分文件编写" << endl;
}
int sumall(int a, int b) {
return a + b;
}
测试文件代码:
#include<iostream>
#include"text.h"
using namespace std;
int main() {
show();//输出:函数分文件编写
cout << sumall(1, 2) << endl;//输出:3
return 0;
}
类分文件编写
函数分文件和类分文件可以同时使用
stu.h文件代码:
#pragma once
#include<iostream>
using namespace std;
class Student {
private:
string name;
string number;
public:
void setName(string name);
void setNumber(string number);
string getName();
string getNumber();
};
void show();
stu.cpp文件代码:
#include"stu.h"
void Student::setName(string name) {
this->name = name;
}
void Student::setNumber(string number) {
this->number = number;
}
string Student::getName() {
return name;
}
string Student::getNumber() {
return number;
}
void show(){
cout<<"函数分文件编写"<<endl;
}
测试文件代码:
#include<iostream>
#include"stu.h"
using namespace std;
int main() {
Student student;
student.setName("小红");
student.setNumber("001");
cout << "姓名:" << student.getName() << endl;
cout << "编号:" << student.getNumber() << endl;
return 0;
}
//输出结果:
//姓名:小红
//编号:001