分析这段代码的执行过程:#include<iostream.h> class Student {public: Student (int n, float s):num (n), score (s){} void change (int n, float s){num=n;score=s;} void display(){cout<<num<<" "<<score<<endl;} private: int num; float score; }; void main() {Student stud (101, 78.5); stud.display(); stud.change (101, 80.5); stud.display(); }
时间: 2024-01-23 11:02:37 浏览: 21
这段代码的功能是定义了一个名为 Student 的类。该类有两个私有成员变量 num 和 score,分别表示学号和分数。类中定义了三个成员函数,其中一个是构造函数,用于初始化 num 和 score;另外两个是公共成员函数,一个是 change,用于修改 num 和 score 的值;另一个是 display,用于输出 num 和 score 的值。
在 main 函数中,首先创建了一个名为 stud 的 Student 类型的对象,构造函数的参数为 101 和 78.5。然后调用 stud 对象的 display 函数输出 num 和 score 的值。接着调用 stud 对象的 change 函数,将 num 的值修改为 101,score 的值修改为 80.5。最后再次调用 stud 对象的 display 函数,输出修改后的 num 和 score 的值。整个程序执行完毕后,输出如下结果:
```
101 78.5
101 80.5
```
因此,该程序的执行过程是:定义 Student 类 -> 创建 stud 对象并初始化 num 和 score -> 调用 stud 对象的 display 函数输出 num 和 score 的值 -> 调用 stud 对象的 change 函数修改 num 和 score 的值 -> 再次调用 stud 对象的 display 函数输出修改后的 num 和 score 的值。
相关问题
#include<iostream.h> class Student {public: Student (int n, float s):num (n), score (s){} void change (int n, float s){num=n;score=s;} void display(){cout<<num<<" "<<score<<endl;} private: int num; float score; }; void main() {Student stud (101, 78.5); stud.display(); stud.change (101, 80.5); stud.display(); }
这是一个使用C++语言编写的简单的学生类程序,包括构造函数、成员函数和数据成员。程序的输出结果为:
```
101 78.5
101 80.5
```
即先输出学号和成绩,然后调用change函数修改学号和成绩,并再次输出修改后的学号和成绩。
代码改错#include <iostream> #include <string.h> #include <stdio.h> using namespace std; class String { public: String() {} String(char str[20]); char Str[20]; friend istream& operator>>(istream& in, String& s); friend ostream& operator<<(ostream& out, String& s); }; String::String(char str[20]) { size_t len = strlen(str); strcpy_s(Str,len,str); } istream& operator>>(istream& in, String& s) { char p[20]; in.getline(p, 20); size_t len = strlen(p); strcpy_s(s.Str,len, p); return in; } ostream& operator<<(ostream& out, String& s) { out << s.Str; return out; } template<class TNo, class TScore, int num>//TNo和TScore为参数化类型 class Student { private: TNo StudentID; //参数化类型,存储姓名 TScore score[num]; //参数化类型数组,存储num门课程的分数 public: void Input();//数据的录入 TScore MaxScore(); //查找score的最大值并返回该值 void Update(TScore sscore, int i);//更新学生的第i门课程成绩为sscore void SelectSort(); //采用选择排序法对学生成绩进行升序排列 void Print(); //输出所有学生的信息 }; template<class TNo, class TScore, int num> void Student<TNo,TScore,num>::Input() { cin >> StudentID; for (int i = 0; i < num; i++) { cin >> score[i]; } } template<class TNo, class TScore, int num> TScore Student<TNo, TScore, num>::MaxScore() { TScore x = 0; if (num != 0) { for (int i = 0; i < num; i++) { if (score[i] > x) { x = score[i]; } } } return x; } template<class TNo, class TScore, int num> void Student<TNo, TScore, num>::Update(TScore sscore, int i) { if (i >= 0 && i < num) { score[i] = sscore; } } template<class TNo, class TScore, int num> void Student<TNo, TScore, num>::SelectSort() { for (int i = 0; i < num; i++) { TScore a = score[i]; for (int j = i; j < num; j++) { if (score[j] < a) { a = score[j]; } } score[i] = a; } } template<class TNo, class TScore, int num> void Student<TNo, TScore, num>::Print() { cout << MaxScore() << endl; cout << StudentID << "\t"; for (int i = 0; i < num; i++) { cout << score[i] << "\t"; } } int main(void) { Student <String, float, 3> s; s.Input(); s.SelectSort(); s.Print(); return 0; }
在代码中,第一行和第二行之间应该加一个空格,即将:
```c++
#include <iostream> #include <string.h> #include <stdio.h>
```
改为:
```c++
#include <iostream>
#include <string.h>
#include <stdio.h>
```
另外,函数`String::String(char str[20])`中,`strcpy_s`函数的第一个参数应该是`s.Str`,即将:
```c++
strcpy_s(Str,len,str);
```
改为:
```c++
strcpy_s(s.Str,len,str);
```
最后,在`Student<TNo, TScore, num>::SelectSort()`函数中,应该将`a`初始化为`score[i]`,即将:
```c++
TScore a = score[i];
```
改为:
```c++
TScore a = score[j];
```
改完后的代码如下: