#include<iostream> using namespace std; class student { private: string name; int age; const int id; static int num; public: // 构造函数 student(); student(string, int age = 20, int id = 20201120); student(const student& s); // 成员函数 void set_name(string name); void set_age(int age); string get_name() const; int get_age() const; int get_id() const; static int get_num(); void print(void) { cout << name<< " " << age << " " << id << endl; } // 友元函数 friend bool operator==(const student& s1, const student& s2); // 析构函数 ~student(); }; int student::num = 0; // 构造 student::student() : name("null"), age(0), id(20201120+num) {} student::student(string name, int age, int id) : name(name), age(age), id(id) { ++num; } student::student(const student& s) : name(s.name), age(s.age), id(20201120+num) {} // 成员 void student::set_name(std::string name) { this->name = name; } void student::set_age(int age) { this->age = age; } string student::get_name() const { return name; } int student::get_age() const { return age; } int student::get_id() const { return id; } int student::get_num() { return num; } // 友元 bool operator==(const student& s1, const student& s2) { return (s1.name == s2.name) && (s1.age == s2.age); } // 析构 student::~student() { num--; } //对比 int equals(student s1, student s2) { if (s1 == s2) { cout << "True" << endl;} else { cout << "False" << endl;} return 0; } int main() { student s1; s1.print(); //带参数有默认值 student s2("小红",18); s2.print(); //使用set更改姓名 s2.set_name("小蓝"); s2.print(); student s3(s2); s3.print(); //测试友元函数是否相同 equals(s2, s3); }对这段代码的输出进行分析
时间: 2024-01-10 07:02:08 浏览: 40
这段代码的输出如下:
```
null 0 20201120
小红 18 20201121
小蓝 18 20201121
小蓝 18 20201122
True
```
代码中创建了三个student对象,s1使用默认构造函数创建,输出为"null 0 20201120";s2使用带参数的构造函数创建,输出为"小红 18 20201121";s2调用set_name函数将姓名更改为"小蓝",再次输出为"小蓝 18 20201121";s3使用拷贝构造函数从s2创建,输出为"小蓝 18 20201122";最后调用equals函数比较s2和s3,因为两者姓名和年龄相同,所以输出为True。
相关问题
详细解释一下这段代码:#include<iostream> using namespace std; #include<string> struct Stu{ string name; int age; double score; };
这段代码是C++的代码片段,包括了三个部分:头文件、命名空间和结构体定义。
头文件`#include<iostream>`是C++标准库的一部分,用来实现输入输出流的操作,包括了cin、cout、endl等常用函数。
命名空间`using namespace std;`是为了方便使用标准库中的函数和类,将std命名空间中的所有内容引入当前作用域,可以避免在使用标准库函数时需要加std前缀的麻烦。
结构体`struct Stu{ string name; int age; double score; };`定义了一个名为Stu的结构体,包括了三个成员变量:name、age和score。其中,name是一个string类型的变量,用来存储学生的姓名;age是一个int类型的变量,用来存储学生的年龄;score是一个double类型的变量,用来存储学生的成绩。这个结构体可以用来存储多个学生的信息,例如可以定义一个数组来存储多个Stu类型的变量,每个变量对应一个学生的信息。
补全程序,并写出程序运行结果。 #include <iostream> using namespace std; class Class { public : Class ( int...
补全程序如下:
```
#include <iostream>
using namespace std;
class Class {
public:
Class(int num) {
cout << "Constructor with one argument called." << endl;
}
Class(int num1, int num2) {
cout << "Constructor with two arguments called." << endl;
}
};
int main() {
Class obj1(10);
Class obj2(20, 30);
return 0;
}
```
程序运行结果为:
```
Constructor with one argument called.
Constructor with two arguments called.
```
解释:
这是一个带有两个构造函数的类`Class`。第一个构造函数接受一个整数参数,第二个构造函数接受两个整数参数。在`main`函数中,我们创建了两个对象`obj1`和`obj2`,分别调用了两个不同的构造函数。所以,程序输出了两个不同的消息。