CPP41. 浅拷贝和深拷贝
描述
现有一个 Person 类,成员变量:姓名(string name)和年龄(int age),请给 Person 添加一个拷贝构造函数,让程序能够正确运行。输入描述
键盘输入用户名和年龄输出描述
通过 Person 类的showPerson()成员方法输出 Person 对象的姓名和年龄,中间使用空格隔开示例1
输入:
zhangsan 20
输出:
zhangsan 20
C++ 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2022-07-23
#include <iostream> #include <cstring> #pragma warning(disable : 4996) using namespace std; class Person { public: char* name; // 姓名 int age; // 年龄 Person(const char* name, int age) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; } Person(Person &p) { this->name=new char[strlen(name)+1]; strcpy(this->name,p.name); this->age=p.age; } // write your code here...... void showPerson() { cout << name << " " << age << endl; } ~Person() { if (name != nullptr) { delete[] name; name = nullptr; } } }; int main() { char name[100] = { 0 }; int age; cin >> name; cin >> age; Person p1(name, age); Person p2 = p1; p2.showPerson(); return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2021-12-31
#include <iostream> #include <cstring> #pragma warning(disable : 4996) using namespace std; class Person { public: char* name; // 姓名 int age; // 年龄 Person(const char* name, int age) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; } // write your code here...... Person(const Person&p){ this->name=new char[strlen(p.name)+1]; strcpy(this->name,p.name); this->age=p.age; } void showPerson() { cout << name << " " << age << endl; } ~Person() { if (name != nullptr) { delete[] name; name = nullptr; } } }; int main() { char name[100] = { 0 }; int age; cin >> name; cin >> age; Person p1(name, age); Person p2 (p1); p2.showPerson(); return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-12-24
#include <iostream> #include <cstring> #pragma warning(disable : 4996) using namespace std; class Person { public: char* name; // 姓名 int age; // 年龄 Person(const char* name, int age) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; } // write your code here...... Person(const Person &p) { this->name = new char[strlen(p.name) + 1]; strcpy(this->name, p.name); this->age = p.age; } void showPerson() { cout << name << " " << age << endl; } ~Person() { if (name != nullptr) { delete[] name; name = nullptr; } } }; int main() { char name[100] = { 0 }; int age; cin >> name; cin >> age; Person p1(name, age); Person p2 = p1; p2.showPerson(); return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-10-14
#include <iostream> #include <cstring> #pragma warning(disable : 4996) using namespace std; class Person { public: char* name; // 姓名 int age; // 年龄 Person(const char* name, int age) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; } Person(const Person& p) { this->name = new char[strlen(p.name) + 1]; strcpy(this->name, p.name); this->age = p.age; } void showPerson() { cout << name << " " << age << endl; } ~Person() { if (name != nullptr) { delete[] name; name = nullptr; } } }; int main() { char name[100] = { 0 }; int age; cin >> name; cin >> age; Person p1(name, age); Person p2 = p1; p2.showPerson(); return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2022-04-17
#include <iostream> #include <cstring> using namespace std; class Person { public: char* name; // 姓名 int age; // 年龄 Person(const char* name, int age) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; } // write your code here...... Person(const Person& p){ //拷贝构造函数 this->name = new char[strlen(p.name) + 1]; //设置name字符数组的长度 strcpy(this->name, p.name); //拷贝name数组 this->age = p.age; } void showPerson() { cout << name << " " << age << endl; } ~Person() { if (name != nullptr) { delete[] name; name = nullptr; } } }; int main() { char name[100] = { 0 }; int age; cin >> name; cin >> age; Person p1(name, age); Person p2 = p1; p2.showPerson(); return 0; }