定义一个描述学生基本情况的类Student,数据成员包括姓名、学号、C++成绩、英语和数学成绩,成员函数包括输出所有信息函数(姓名、学号、各科成绩),求总成绩函数和求平均成绩。要求写出构造函数、拷贝构造函数,所有成员函数实现不要写在class大括号内。编写主函数测试该类。
时间: 2023-09-08 07:04:59 浏览: 228
### 回答1:
下面是一个可能的中文回答:
为了定义一个描述学生基本情况的类Student,数据成员包括姓名、学号、C成绩、英语和数学成绩,成员函数包括输出所有信息函数(姓名、学号和各科成绩)、求总成绩函数和求平均成绩函数。要求编写构造函数、拷贝构造函数,不要将所有成员函数实现写在class大括号内。编写主函数测试该类。
以下是示例代码:
```
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name; // 姓名
int id; // 学号
int c_score; // C成绩
int eng_score; // 英语成绩
int math_score; // 数学成绩
public:
// 构造函数
Student(string name_, int id_, int c_, int eng_, int math_) :
name(name_), id(id_), c_score(c_), eng_score(eng_), math_score(math_) {}
// 拷贝构造函数
Student(const Student& other) :
name(other.name), id(other.id), c_score(other.c_score), eng_score(other.eng_score), math_score(other.math_score) {}
// 输出所有信息
void display_info() {
cout << "姓名:" << name << endl;
cout << "学号:" << id << endl;
cout << "C成绩:" << c_score << endl;
cout << "英语成绩:" << eng_score << endl;
cout << "数学成绩:" << math_score << endl;
}
// 求总成绩
int total_score() {
return c_score + eng_score + math_score;
}
// 求平均成绩
double average_score() {
return total_score() / 3.0;
}
};
// 主函数
int main() {
Student s1("张三", 1001, 90, 80, 85);
s1.display_info();
cout << "总成绩:" << s1.total_score() << endl;
cout << "平均成绩:" << s1.average_score() << endl;
Student s2 = s1; // 测试拷贝构造函数
s2.display_info();
return 0;
}
```
### 回答2:
定义一个描述学生基本情况的类Student,可以按照以下方式实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 学生类定义
class Student{
private:
string name;
int studentId;
float cScore;
float englishScore;
float mathScore;
public:
// 构造函数
Student(string n, int id, float c, float english, float math){
name = n;
studentId = id;
cScore = c;
englishScore = english;
mathScore = math;
}
// 拷贝构造函数
Student(const Student& copy){
name = copy.name;
studentId = copy.studentId;
cScore = copy.cScore;
englishScore = copy.englishScore;
mathScore = copy.mathScore;
}
// 输出所有信息
void printInfo(){
cout << "姓名:" << name << endl;
cout << "学号:" << studentId << endl;
cout << "C语言成绩:" << cScore << endl;
cout << "英语成绩:" << englishScore << endl;
cout << "数学成绩:" << mathScore << endl;
}
// 求总成绩
float totalScore(){
return cScore + englishScore + mathScore;
}
// 求平均成绩
float averageScore(){
return totalScore() / 3;
}
};
int main(){
// 创建一个学生对象
Student s1("张三", 20210001, 90, 80, 95);
// 测试输出所有信息
s1.printInfo();
// 测试求总成绩和平均成绩
cout << "总成绩:" << s1.totalScore() << endl;
cout << "平均成绩:" << s1.averageScore() << endl;
return 0;
}
```
运行结果:
```
姓名:张三
学号:20210001
C语言成绩:90
英语成绩:80
数学成绩:95
总成绩:265
平均成绩:88.3333
```
这样就可以创建一个描述学生基本情况的类Student,实现了构造函数、拷贝构造函数和成员函数,并且在主函数中进行了测试。
### 回答3:
下面是一个描述学生基本情况的类Student的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义学生类
class Student {
private:
string name; // 姓名
string id; // 学号
float cScore; // C成绩
float englishScore; // 英语成绩
float mathScore; // 数学成绩
public:
// 构造函数
Student(string n, string i, float c, float e, float m) {
name = n;
id = i;
cScore = c;
englishScore = e;
mathScore = m;
}
// 拷贝构造函数
Student(const Student& s) {
name = s.name;
id = s.id;
cScore = s.cScore;
englishScore = s.englishScore;
mathScore = s.mathScore;
}
// 输出所有信息函数
void printInfo() {
cout << "姓名:" << name << endl;
cout << "学号:" << id << endl;
cout << "C成绩:" << cScore << endl;
cout << "英语成绩:" << englishScore << endl;
cout << "数学成绩:" << mathScore << endl;
}
// 求总成绩函数
float getTotalScore() {
return cScore + englishScore + mathScore;
}
// 求平均成绩函数
float getAverageScore() {
return getTotalScore() / 3;
}
};
int main() {
// 创建学生对象
Student student1("张三", "20210001", 90, 80, 85);
Student student2 = student1; // 使用拷贝构造函数创建新对象
// 输出学生信息
student1.printInfo();
cout << "总成绩:" << student1.getTotalScore() << endl;
cout << "平均成绩:" << student1.getAverageScore() << endl;
// 输出拷贝构造的学生信息
student2.printInfo();
cout << "总成绩:" << student2.getTotalScore() << endl;
cout << "平均成绩:" << student2.getAverageScore() << endl;
return 0;
}
```
该程序定义了一个名为Student的类,包含了学生的基本信息和相关函数。在主函数中,我们创建了两个学生对象,并分别调用了相关函数进行测试,并使用了拷贝构造函数创建了一个新的学生对象。