C++_用类实现对5个学生的姓名,学号,性别的输入与输出。

Posted CaoPengCheng&

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++_用类实现对5个学生的姓名,学号,性别的输入与输出。相关的知识,希望对你有一定的参考价值。

C++_用类实现对5个学生的姓名,学号,性别的输入与输出。

#include<iostream>
using namespace std;
/**
 * 用类实现对5个学生的姓名,学号,性别的输入与输出。要求类外定义成员函数
 * @author CaoPengCheng
 * @date 2021-10-07
 */

class Student{
private:
	char name[40];
	long number;
	char sex[4];
public:
	void setData();
	void toString();
};
#include "2_1_Student.h"
#include<iostream>
using namespace std;
/**
 * 用类实现对5个学生的姓名,学号,性别的输入与输出。要求类外定义成员函数
 * @author CaoPengCheng
 * @date 2021-10-07
 */

void Student::setData(){
	cout<<"input name:";
	cin>>name;
	cout<<"input number:";
	cin>>number;
	cout<<"input sex:";
	cin>>sex;
}

void Student::toString(){
	cout<<"name="<<name<<endl;
	cout<<"number="<<number<<endl;
	cout<<"sex="<<sex<<endl;
}

#include "2_1_Student.h"
#include<iostream>
using namespace std;
/**
 * 用类实现对5个学生的姓名,学号,性别的输入与输出。要求类外定义成员函数
 * @author CaoPengCheng
 * @date 2021-10-07
 */

int main(){
Student stu1,stu2,stu3,stu4,stu5;
stu1.setData();
stu1.toString();
cout<<endl;
stu2.setData();
stu2.toString();
cout<<endl;
stu3.setData();
stu3.toString();
cout<<endl;
stu4.setData();
stu4.toString();
cout<<endl;
stu5.setData();
stu5.toString();
return 0;
}

以上是关于C++_用类实现对5个学生的姓名,学号,性别的输入与输出。的主要内容,如果未能解决你的问题,请参考以下文章