#include<iostream> #include<string> using namespace std; class shape { public: double width; double length; shape(double=4.0, double=3.0):width(width),length(length){} }; class Rectangle :public shape { public: Rectangle(double width=4, double length=3) {} double get_Aera() { double area; area = width * length; cout << area; 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(4,3); cout << r.get_Aera() << endl; Student s("Tom", 20, 90); cout << s.name << s.age << s.score << endl; return 0; }使其正确并写出输出结果
时间: 2023-11-22 14:54:49 浏览: 32
以下是正确的代码和输出结果:
```cpp
#include<iostream>
#include<string>
using namespace std;
class shape {
public:
double width;
double length;
shape(double w = 4.0, double l = 3.0) :width(w), length(l) {}
};
class Rectangle :public shape {
public:
Rectangle(double w = 4, double l = 3) :shape(w, l) {}
double get_Area() {
double area;
area = width * length;
cout << "The area of the rectangle is: " << area << endl;
return width * length;
}
};
struct Student {
string name;
int age;
double score;
Student(string n = "", int a = 0, double s = 0) :name(n), age(a), score(s) {}
};
int main() {
Rectangle r(4, 3);
cout << "Rectangle: " << endl;
r.get_Area();
cout << endl;
Student s("Tom", 20, 90);
cout << "Student: " << endl;
cout << "Name: " << s.name << ", Age: " << s.age << ", Score: " << s.score << endl;
return 0;
}
```
输出结果为:
```
Rectangle:
The area of the rectangle is: 12
12
Student:
Name: Tom, Age: 20, Score: 90
```