#include<iostream> #include<string> using namespace std; class shape { public: double width; double length; shape(double width=0,double length=0) :width(width), length(length){} }; class Rectangle :public shape { public: double get_Aera() { return (width * length); } }; struct Student { string name; int age; double score; Student(string name="",int age=0,double score=0):name(name),age(age),score(score){} }; int main() { Rectangle r(3.4,4.5); cout << r.get_Aera() << endl; Student s("Tom", 20, 90); cout << s.name << s.age << s.score << endl; return 0; }改正它
时间: 2023-11-22 08:53:47 浏览: 49
使用include <iostream>时一定要加using namespace std
代码中有两个问题需要修正:
1. 头文件和命名空间应该写在 #include 和 main 函数之间;
2. 打印学生信息时应该分别输出各个属性,并在输出字符串之间加上空格。
修改后的代码如下:
```c++
#include<iostream>
#include<string>
using namespace std;
class shape {
public:
double width;
double length;
shape(double width=0, double length=0) :width(width), length(length) {}
};
class Rectangle :public shape {
public:
double get_Aera() {
return (width * length);
}
};
struct Student {
string name;
int age;
double score;
Student(string name="", int age=0, double score=0):name(name), age(age), score(score) {}
};
int main() {
Rectangle r(3.4, 4.5);
cout << r.get_Aera() << endl;
Student s("Tom", 20, 90);
cout << s.name << " " << s.age << " " << s.score << endl;
return 0;
}
```
阅读全文