#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-29 07:03:06 浏览: 17
这是一个使用C++语言编写的简单的学生类程序,包括构造函数、成员函数和数据成员。程序的输出结果为:
```
101 78.5
101 80.5
```
即先输出学号和成绩,然后调用change函数修改学号和成绩,并再次输出修改后的学号和成绩。
相关问题
#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(); }修改上面的程序,增加一个 fun 函数,改写 main 函数。在 main 函数中调用 fun 函 数,在 fun函数中调用change和 display 函数。在 fun 函数中使用对象的引用(Student &) 作为形参。
修改后的程序如下所示:
```cpp
#include<iostream>
using namespace std;
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 fun(Student &stud){
stud.display();
stud.change(101, 80.5);
stud.display();
}
int main() {
Student stud(101, 78.5);
fun(stud);
return 0;
}
```
在新的程序中,我们增加了一个名为`fun`的函数,该函数的参数是一个`Student`类对象的引用。在`fun`函数中,我们先调用了`display`函数输出对象的原始信息,然后使用`change`函数修改了学号和成绩,最后再次调用`display`函数输出修改后的信息。
在`main`函数中,我们首先创建了一个`Student`类对象`stud`,然后调用了`fun`函数,并将`stud`对象的引用作为参数传递给`fun`函数。这样,`fun`函数就可以对`stud`对象进行修改和输出。
代码改错#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];
```
改完后的代码如下: