#include<iostream>
using namespace std;
#include<cmath>
#include<cstring>
#include<cstdlib>
int main()
{
Student std1;
std1.set_data(1, "xiaoming");
std1.inputdata();
std1.show();
getchar();
getchar();
return 0;
}
class Student
{
private:
int no; //学生学号
char name[20]; //学生姓名
double score[3]; //课程成绩
double average();
double sum();
public:
void set_data(int n, char *p);
void inputdata();
void show();
};
void Student::set_data(int n, char *p)
{
no = n;
strcpy(name, p);
}
void Student::inputdata()
{
int i;
cout << "please input score of " << name << ": " << endl;
for (i = 0; i < 3;i++)
{
cin >> score[i];
}
}
double Student::sum()
{
return score[0] + score[1] + score[2];
}
double Student::average()
{
return sum() / 3;
}
void Student::show()
{
cout << "No.: " << no << ", Name: " << name << endl;
cout << "Score: " << score[0] << ", " << score[1] << ", " << score[2] << endl;
cout << "average: " << average() << '\t' << "Sum: " << sum() << endl << endl;
}
直接上程序。