代码段一:【类】
#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;
}
}