【无标题】

文章展示了两个C++代码片段。第一个涉及创建一个名为Guest的类,包括构造函数、显示信息的方法、获取当前酒店入住人数的静态函数以及计算总收入的函数。然后用3个实例测试了这些功能。第二个代码片段定义了一个Student结构体,实现了读取、写入和备份学生数据到文件的功能。
摘要由CSDN通过智能技术生成

代码段一:【类】

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

/*定义一个客人类 Guest。包含成员属性:编号 Num、姓名 Name、房费 Fee、当前酒店入住人数 Count。
其中编号 Num 需要程序自动生成。
现在要求实现以下 Guest 的成员函数:构造函数、Show()显示 Guest 的信息、 GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。
并定义 3 个 Guest 对象来对成员函数进行测试。*/

int total = 0;

class Guest {
private:
	int Num;
	string Name;
	double Fee;
	static int Count;
public:
	Guest(string, double);
	void show();
	static int GetCount(){ return Count; }
};

int Guest::Count = 0;

Guest::Guest(string name,double fee) {
	Num = ++total;
	Name = name;
	Fee = fee;
	Count++;
}

void Guest::show() {
	cout << endl << "Name:" << Name << endl;
	cout << "Num:" << Num << endl;
	cout << "Fee:" << Fee << endl;
}

int main()
{
	Guest gue[3] = {
		Guest("A",20),
		Guest("B", 30),
		Guest("C",40)
	};
	for (int i = 0; i < 3; i++) {
		gue[i].show();
	}
	cout << endl;
	cout << "人数:" << Guest::GetCount() << endl;
}

输出:在这里插入图片描述
**

代码段二:【结构体+文件读写】

**

#include<iostream>
#include<fstream>
using namespace std;

/*写一个Student的结构体,包括学生的姓名,学号,语数英三科成绩
再写三个函数,一个是读操作,一个是写操作,一个是备份操作。
读写在Student.dat文件中,备份至Student.bak文件中*/

struct Student {
	char name[20];
	int id;
	double Ch;
	double Ma;
	double En;
};

void read() {
	Student a;
	ifstream infile("Student.dat",ios::binary);
	while (infile.read((char*)&a, sizeof(Student))) {
	cout << "******************" << endl;
	cout << "name:" << a.name << endl;
	cout << "id:" << a.id << endl;
	cout << "语文:" << a.Ch << endl;
	cout << "数学:" << a.Ma << endl;
	cout << "英语:" << a.En << endl;
	cout << "******************" << endl;
	}
	infile.close();
}

void write(Student a) {
	ofstream ofile("Student.dat", ios::binary);
	ofile.write((char*)&a, sizeof(Student));
	cout << "保存成功" << endl;
	ofile.close();
}

void back_up() {
	Student a;
	ifstream infile("Student.dat", ios::binary);
	ofstream ofile("Student.bak", ios::binary);
	while (infile.read((char*)&a, sizeof(Student))) {
		ofile.write((char*)&a, sizeof(Student));
	}
	cout << "备份成功" << endl;
	infile.close();
	ofile.close();
}

int main() {
	Student stu[2];
	stu[0] = { "Mark",1,11.0,22.0,33.0 };
	stu[1] = { "Nay",2,55.0,66.0,44.0 };
	write(*stu);
	write(stu[1]);
	back_up();
	read();
	return 0;
}

输出:在这里插入图片描述
**

代码段三:写一个类或结构体,包括学号姓名语数外成绩,输入n,新建n个,然后分别输入

**

#include<iostream>
#include<string>
using namespace std;

struct Student {
    string name;
    string id;
    double Chinses;
    double Math;
    double English;
};

int main() {
    int n;
    cout << "please input n:";
    cin >> n;

    Student* stu = new Student[n];

    for (int i = 0; i < n; i++) {
        cout << endl;
        cout << "please input name:";
        cin >> stu[i].name;
        cout << "please input ID:";
        cin >> stu[i].id;
        cout << "please input Chinese Score:";
        cin >> stu[i].Chinses;
        cout << "please input Math score:";
        cin >> stu[i].Math;
        cout << "please input English Score:";
        cin >> stu[i].English;
    }

    cout << endl << "display:" << endl;
    for (int i = 0; i < n; i++) {
        cout << "*******************************" << endl;
        cout << "name:" << stu[i].name << endl;
        cout << "ID:" << stu[i].id << endl;
        cout << "Chinese Score:" << stu[i].Chinses << endl;
        cout << "Math score:" << stu[i].Math << endl;
        cout << "English Score:" << stu[i].English << endl;
        cout << "*******************************" << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值