C++分文件编写

目录

函数分文件编写

类分文件编写


函数分文件编写

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算不出来没办法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值