用c++编写一个学生类。输出每个学生的姓名、学号、成绩

用c++编写一个学生类。输出每个学生的姓名、学号、成绩

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
 Student(string s,int a,int b):name(s),id(a),score(b){}
 void display();
private:
 string name;
 int id;
 int score;
};
void Student::display()
{
 cout << "姓名:" << ends << name << endl;
 cout << "学号:" << ends << id << endl;
 cout << "成绩:" << ends << score << endl;
}
int main()
{
 Student stu("小明", 19, 97);
 stu.display();
 system("pause");
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-06-12
#include <iostream.h>
#include <stdlib.h>

class student
{
public:
int no;
char *name;
int deg;
student();
~student();
void display();
};

static int sum,num=3;
static int avg(int sum);

void main()
{
int aver;
student stu[3];
stu[0].no=1;
stu[0].name="li";
stu[0].deg=89;
stu[1].no=2;
stu[1].name="chen";
stu[1].deg=78;
stu[2].no=3;
stu[2].name="zheng";
stu[2].deg=94;
for(int i=0;i<3;i++)
{
stu[i].display();
sum+=stu[i].deg;
}
cout<<"the sum is:"<<sum<<endl;
aver=avg(sum);
cout<<"the average is:"<<aver<<endl;
}

student::student()
{
no=0;
name=(char*)malloc(sizeof(char)*8);
deg=0;
}

student::~student()
{
free(name);
}

void student::display()
{
cout<<no<<"\t"<<name<<"\t"<<deg<<endl;
}

static int avg(int sum)
{
int aver;
aver=sum/num;
return aver;
}