指出下列程序的错误,说明错误的原因并改正。 #include <iostream> using namespace std; class Student { string name; float score; static int total; int Student (const char n, float s) { name = n; total += s; return 0; } }; int main() { int Student::total = 0; Student s1("张三", 95); Student s2("李四", 90); Student s3("王五", 85); cout << "总分=" << total << endl; }

时间: 2024-01-28 17:03:03 浏览: 31
这个程序有以下错误: 1. 在 class Student 中的构造函数应该是 public,而不是 private。 2. 构造函数的参数类型应该是 const char* 而不是 const char。 3. 在构造函数中,total += s 应该改为 score = s; total += s;。 4. 在 main 函数中,应该先声明并初始化静态变量 total,然后才能创建对象。 5. 在 main 函数中,输出总分时应该使用类名 Student::total 而不是 total。 下面是改正后的程序: ``` #include <iostream> #include <string> using namespace std; class Student { string name; float score; static int total; public: Student(const char* n, float s) { name = n; score = s; total += s; } }; int Student::total = 0; int main() { Student s1("张三", 95); Student s2("李四", 90); Student s3("王五", 85); cout << "总分=" << Student::total << endl; return 0; } ``` 改正后的程序中,构造函数被声明为 public,参数类型改为 const char*,在构造函数中同时给 name 和 score 赋值,先声明并初始化静态变量 total,然后才能创建对象,输出总分时使用类名 Student::total。

相关推荐

#include <iostream> #include <string> #include <vector> using namespace std; class Student { private: int id; //学号 string name; //姓名 float score; //个人成绩 public: //构造函数 Student(int id, string name, float score) { this->id = id; this->name = name; this->score = score; } //获取学号 int getId() { return id; } //获取姓名 string getName() { return name; } //获取成绩 float getScore() { return score; } }; class Project { private: int pId; //项目编号 string pName; //项目名称 Student leader; //组长 Student members[3]; //小组成员 float pScore; //小组成绩 public: //构造函数 Project(int pId, string pName, Student leader, Student members[]) { this->pId = pId; this->pName = pName; this->leader = leader; for(int i=0; i<3; i++) { this->members[i] = members[i]; } //计算小组成绩 pScore = (members[0].getScore() + members[1].getScore() + members[2].getScore()) / 3; } //获取项目编号 int getPid() { return pId; } //获取项目名称 string getPname() { return pName; } //获取组长 Student getLeader() { return leader; } //获取小组成员 Student* getMembers() { return members; } //获取小组成绩 float getScore() { return pScore; } //输出项目组信息 void printProject() { cout << "项目编号:" << pId << endl; cout << "项目名称:" << pName << endl; cout << "组长:" << leader.getName() << endl; cout << "小组成员:" << endl; for(int i=0; i<3; i++) { cout << members[i].getName() << endl; } cout << "小组成绩:" << pScore << endl; } }; int main() { //创建学生 Student s1(1, "张三", 80); Student s2(2, "李四", 85); Student s3(3, "王五", 90); Student s4(4, "赵六", 85); Student s5(5, "钱七", 92); Student s6(6, "孙八", 87); //创建项目组 Student members1[3] = {s1, s2, s3}; Project p1(1, "项目一", s1, members1); Student members2[3] = {s4, s5, s6}; Project p2(2, "项目二", s4, members2); //输出项目组信息 p1.printProject(); p2.printProject(); return 0; }修改程序

#include <iostream> #include <iomanip> #include <string.h> #include <cmath> #define M 3 // 课程门数 #define N 4 //学生数组中的学生个数 using namespace std; class Student { public: Student() {} Student(const Student&); void input_info() { cin>>id; cin>>name; for(int i=0; i<3; i++) cin>>score[i]; isFail=false; for(int i=0; i<3; i++) if(score[i]<60) isFail=true; } void input_lesson_ids() { for(int i=0; i<M; i++) cin>>lesson_id[i]; } void show_info() { cout<<"Student id:"<<id<<endl; cout<<"Student name:"<<name<<endl; cout<<setw(10)<<"lesson_id "; for(int i=0; i<M; i++) cout<<setw(10)<<lesson_id[i]; cout<<setw(10)<<"Average"; cout<<endl; cout<<setw(10)<<"scores "; for(int i=0; i<M; i++) cout<<setw(10)<<score[i]; cout<<setw(10)<<ave(); cout<<endl; if(isFail) cout<<"The student failed."<<endl; else cout<<"The student didn\'t fail."<<endl; cout<<"------华丽的分割线--------"<<endl; } float ave()//求平均成绩 { //实现求平均成绩并返回 ...... } string get_name() { return name; }private: int id; string name; bool isFail; static int lesson_id[M]; float score[M]; }; int Student::lesson_id[M]; Student::Student(const Student& s) { //完成拷贝构造函数的实现,拷贝出的对象和原对象一致 ...... } int main() { Student cs[N]; cs[0].input_lesson_ids();// 用一个学生对象对静态数组成员赋值 for(int i=0; i<N; i++) cs[i].input_info(); //求出最高平均成绩并按要求格式输出相关语句 ...... //求出最低平均成绩并按要求格式输出相关语句 ...... //按照平均成绩的高低对学生数组进行排序 ...... for(int i=0; i<N; i++)//输出排序后的结果 cs[i].show_info(); return 0; }补全c++

输入: 201 202 203 1 zhang 67 88 92 2 li 44 77 99 3 wang 76 82 95 4 zhao 63 82 86 输出: Student wang got the highest average score as 84.3333 Student li got the lowest average score as 73.3333 Student id:3 Student name:wang lesson_id 201 202 203 Average scores 76 82 95 84.3333 The student didn't fail. ------华丽的分割线-------- Student id:1 Student name:zhang lesson_id 201 202 203 Average scores 67 88 92 82.3333 The student didn't fail. ------华丽的分割线-------- Student id:4 Student name:zhao lesson_id 201 202 203 Average scores 63 82 86 77 The student didn't fail. ------华丽的分割线-------- Student id:2 Student name:li lesson_id 201 202 203 Average scores 44 77 99 73.3333 The student failed. ------华丽的分割线-------- 程序部分代码如下,请补充完整。 #include <iostream> #include <iomanip> #include <string.h> #include <cmath> #define M 3 // 课程门数 #define N 4 //学生数组中的学生个数 using namespace std; class Student { public: Student() {} Student(const Student&); void input_info() { cin>>id; cin>>name; for(int i=0; i<3; i++) cin>>score[i]; isFail=false; for(int i=0; i<3; i++) if(score[i]<60) isFail=true; } void input_lesson_ids() { for(int i=0; i<M; i++) cin>>lesson_id[i]; } void show_info() { cout<<"Student id:"<<id<<endl; cout<<"Student name:"<<name<<endl; cout<<setw(10)<<"lesson_id "; for(int i=0; i<M; i++) cout<<setw(10)<<lesson_id[i]; cout<<setw(10)<<"Average"; cout<<endl; cout<<setw(10)<<"scores "; for(int i=0; i<M; i++) cout<<setw(10)<<score[i]; cout<<setw(10)<<ave(); cout<<endl; if(isFail) cout<<"The student failed."<<endl; else cout<<"The student didn\'t fail."<<endl; cout<<"------华丽的分割线--------"<<endl; } float ave()//求平均成绩 { //实现求平均成绩并返回 ...... } string get_name() { return name; } private: int id; string name; bool isFail; static int lesson_id[M]; float score[M]; }; int Student::lesson_id[M]; Student::Student(const Student& s) { //完成拷贝构造函数的实现,拷贝出的对象和原对象一致 ...... } int main() { Student cs[N]; cs[0].input_lesson_ids();// 用一个学生对象对静态数组成员赋值 for(int i=0; i<N; i++) cs[i].input_info(); //求出最高平均成绩并按要求格式输出相关语句 ...... //求出最低平均成绩并按要求格式输出相关语句 ...... //按照平均成绩的高低对学生数组进行排序 ...... for(int i=0; i<N; i++)//输出排序后的结果 cs[i].show_info(); return 0; }

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

matlab基于RRT和人工势场法混合算法的路径规划.zip

matlab基于RRT和人工势场法混合算法的路径规划.zip
recommend-type

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。