查看代码#include <iostream> using namespace std; class Student { int num; float score; public: Student input() { Student A; cin >> A.num; cin >> A.score; return A; } void show(); }; void Student::show() { cout <<"学号:"<<num<<endl; cout << "成绩:"<<score << endl; } int main() { Student stu[5], * p; int i; p = stu; for (i = 0; i < 5; i++) { p->input(); p++; } for (i = 0; i < 5; i += 2) { p->show(); p++; } }
时间: 2024-03-11 15:44:52 浏览: 20
这段代码定义了一个名为 "Student" 的类,其中包含了 "num" 和 "score" 两个私有成员变量,以及 "input()" 和 "show()" 两个公有成员函数。
在 main() 函数中,首先创建了一个由 5 个 "Student" 对象组成的数组 "stu",同时定义了一个 "Student" 类型的指针变量 "p",并将其指向数组 "stu" 的第一个元素。接下来使用指针变量 "p" 对数组 "stu" 进行遍历,调用每个 "Student" 对象的 "input()" 函数进行输入。这里需要注意的是,每次调用 "input()" 函数时,都会创建一个名为 "A" 的新 "Student" 对象,然后将用户输入的数据存储到 "A" 中,并将 "A" 对象作为返回值返回给调用者。在遍历完整个数组后,每个 "Student" 对象都已经被正确地初始化了。
接下来,再次使用指针变量 "p" 对数组 "stu" 进行遍历,调用每个 "Student" 对象的 "show()" 函数进行输出。其中只输出下标为偶数的对象,即下标为 0、2、4 的对象。需要注意的是,由于在上一个循环中,指针变量 "p" 已经遍历到了数组的末尾,因此在这里需要将指针变量 "p" 重新指向数组 "stu" 的第一个元素,才能正确地进行第二次遍历。
相关问题
#include <iostream> using namespace std; class Student { int num; float score; public: Student input() { Student A; cin >> A.num; cin >> A.score; return A; } void show(); }; void Student::show() { cout <<"学号:"<<num<<endl; cout << "成绩:"<<score << endl; } int main() { Student stu[5], * p; int i; p = stu; for (i = 0; i < 5; i++) { p->input(); p++; } for (i = 0; i < 5; i += 2) { p->show(); p++; } }
这是一段 C++ 代码,定义了一个名为 "Student" 的类,其中包含了 "num" 和 "score" 两个私有成员变量,以及 "input()" 和 "show()" 两个公有成员函数。main() 函数中创建了一个 "Student" 类型的数组 "stu",并将其指针赋给指针变量 "p"。接下来使用指针变量 "p" 对数组 "stu" 进行遍历,调用每个 "Student" 对象的 "input()" 函数进行输入,最后再次遍历数组 "stu" 输出每个 "Student" 对象的信息,其中只输出下标为偶数的对象。
代码改错#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];
```
改完后的代码如下: