1. 定义student 类。 要求: (1学习在Visual c++6.0 中使用类向导建立类定义和类实现: (2)建立多文件,将类定义和类实现以及用户实现分别写在不同的文件中; (3)在该类中包括有学号、姓名和成绩,成绩设为有三个
时间: 2024-02-12 11:03:29 浏览: 32
属性:score1、score2、score3。同时,该类应包含以下成员函数:
1. 构造函数:用于初始化学号、姓名和成绩;
2. 获取学号、姓名和成绩的函数;
3. 设置学号、姓名和成绩的函数;
4. 计算总成绩的函数;
5. 计算平均成绩的函数。
下面是student类的定义和实现:
```c++
// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student {
public:
Student(); // 默认构造函数
Student(int no, string name, double s1, double s2, double s3); // 带参数的构造函数
int GetNo() const; // 获取学号
string GetName() const; // 获取姓名
double GetScore1() const; // 获取成绩1
double GetScore2() const; // 获取成绩2
double GetScore3() const; // 获取成绩3
void SetNo(int no); // 设置学号
void SetName(string name); // 设置姓名
void SetScore1(double s1); // 设置成绩1
void SetScore2(double s2); // 设置成绩2
void SetScore3(double s3); // 设置成绩3
double CalculateTotalScore() const; // 计算总成绩
double CalculateAverageScore() const; // 计算平均成绩
private:
int m_no; // 学号
string m_name; // 姓名
double m_score1; // 成绩1
double m_score2; // 成绩2
double m_score3; // 成绩3
};
#endif
// student.cpp
#include "student.h"
Student::Student() {
m_no = 0;
m_name = "";
m_score1 = 0.0;
m_score2 = 0.0;
m_score3 = 0.0;
}
Student::Student(int no, string name, double s1, double s2, double s3) {
m_no = no;
m_name = name;
m_score1 = s1;
m_score2 = s2;
m_score3 = s3;
}
int Student::GetNo() const {
return m_no;
}
string Student::GetName() const {
return m_name;
}
double Student::GetScore1() const {
return m_score1;
}
double Student::GetScore2() const {
return m_score2;
}
double Student::GetScore3() const {
return m_score3;
}
void Student::SetNo(int no) {
m_no = no;
}
void Student::SetName(string name) {
m_name = name;
}
void Student::SetScore1(double s1) {
m_score1 = s1;
}
void Student::SetScore2(double s2) {
m_score2 = s2;
}
void Student::SetScore3(double s3) {
m_score3 = s3;
}
double Student::CalculateTotalScore() const {
return m_score1 + m_score2 + m_score3;
}
double Student::CalculateAverageScore() const {
return (m_score1 + m_score2 + m_score3) / 3.0;
}
```
该类的用户实现可以写在另外一个文件中,例如:
```c++
// main.cpp
#include <iostream>
#include "student.h"
using namespace std;
int main() {
Student s(1001, "张三", 80, 90, 85); // 创建学生对象
cout << "学号:" << s.GetNo() << endl;
cout << "姓名:" << s.GetName() << endl;
cout << "成绩1:" << s.GetScore1() << endl;
cout << "成绩2:" << s.GetScore2() << endl;
cout << "成绩3:" << s.GetScore3() << endl;
cout << "总成绩:" << s.CalculateTotalScore() << endl;
cout << "平均成绩:" << s.CalculateAverageScore() << endl;
return 0;
}
```