构建一个学生类Student,含有字符串name,整数成员age及双精度score,函数Init含三个默认参数初始化学生对象(xiaoming,18,88.65)。成员函数print没有形参,需使用this指针,显示对象数据成员的内容。要求编写程序声明两个对象s1、s2,其中s2使用默认参数进行初始化,并显示学生对象数据成员的值。
时间: 2023-05-23 07:05:13 浏览: 107
A:
```C++
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int age;
double score;
public:
void Init(string n = "xiaoming", int a = 18, double s = 88.65) {
name = n;
age = a;
score = s;
}
void print() {
cout << "Name: " << name << endl
<< "Age: " << age << endl
<< "Score: " << score << endl;
}
};
int main() {
Student s1, s2;
s1.Init("xiaohong", 20, 90.0);
s2.Init();
cout << "s1:\n";
s1.print();
cout << "s2:\n";
s2.print();
return 0;
}
```
输出:
```
s1:
Name: xiaohong
Age: 20
Score: 90
s2:
Name: xiaoming
Age: 18
Score: 88.65
```
阅读全文