C++ 拷贝构造小练习
//头文件 #ifndef STUDENT_H #define STUDENT_H //武器类 class Weapon { public: Weapon(); ~Weapon(); Weapon(const char* name); //带参构造 Weapon(const Weapon& other); //拷贝构造 char* Get_Weapon_name(); //获取武器名称 void Set_Weapon_name(const char* m_name); //设置武器名称 //private: char* Weapon_Name; }; //角色类 class Gamer { public: Gamer(); ~Gamer(); Weapon* Get_weapon(); //获取武器信息 void Set_weapon(const Weapon& other);//设置武器信息 char* Get_name(); //获取角色名称 void Set_name(const char* m_name); //设置角色名称 int Get_Level(); //获取游戏等级 void Set_Level(int m_level);//设置游戏等级 Gamer(const char* m_name, int m_Level,const Weapon& other);//带参构造 Gamer(const Gamer& other) ; //拷贝构造 void speak(); // private: Weapon* weapon; //角色的武器 char* name; //角色名称 int Level; //角色等级 }; #endif //cpp文件 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <string> #include <easyx.h> #include "Student.h" using namespace std; //默认构造 Weapon::Weapon() { this->Weapon_Name = new char[32]; } //释放武器变量 Weapon::~Weapon() { if (this->Weapon_Name != NULL) { delete []this->Weapon_Name; this->Weapon_Name = NULL; } } //返回武器名称 char* Weapon::Get_Weapon_name() { return this->Weapon_Name; } //设置武器名称 void Weapon::Set_Weapon_name(const char* m_name) { strcpy(Weapon_Name, m_name); } //带参构造 Weapon::Weapon(const char* name) { this->Weapon_Name = new char[strlen(name)+1]; strcpy(Weapon_Name, name); } //拷贝构造 Weapon::Weapon(const Weapon& other) { this->Weapon_Name = new char[strlen(other.Weapon_Name) + 1]; strcpy(this->Weapon_Name, other.Weapon_Name); } //默认构造 Gamer::Gamer() { weapon = new Weapon(); this->name = new char[32]; Level = 0; } //析构成员 Gamer::~Gamer() { if (weapon != NULL) { delete weapon; weapon = NULL; } if (this->name != NULL) { delete[]this->name; this->name = NULL; } Level = 0; } //带参构造 Gamer::Gamer(const char* m_name, int m_Level, const Weapon& other) { this->weapon = new Weapon(other); this->weapon->Weapon_Name = new char[strlen(other.Weapon_Name) + 1]; strcpy(this->weapon->Weapon_Name, other.Weapon_Name); this->name = new char[strlen(m_name) + 1]; strcpy(this->name, m_name); this->Level = m_Level; } //拷贝构造 Gamer::Gamer(const Gamer& other) { this->name = new char[strlen(other.name) + 1]; strcpy(this->name, other.name); weapon = new Weapon(); strcpy(this->weapon->Weapon_Name, other.weapon->Weapon_Name); this->Level = other.Level; } //获取武器信息 Weapon* Gamer::Get_weapon() { return this->weapon; } //设置武器信息 void Gamer::Set_weapon(const Weapon& other) { weapon = new Weapon(other); } //获取角色名称 char* Gamer::Get_name() { return this->name; } //设置角色名称 void Gamer::Set_name(const char* m_name) { strcpy(this->name,m_name); } //获取角色等级 int Gamer::Get_Level() { return this->Level; } //设置角色等级 void Gamer::Set_Level(int m_level) { this->Level = m_level; } //输出角色信息 void Gamer::speak() { cout <<"等级:"<< Level << "\t"<<"角色名称:" << name << "\t" <<"武器名称:" <<weapon->Get_Weapon_name()<< endl; } int main() { string weaponname = "狼牙棒"; string username = "神迹gg"; Weapon temp(weaponname.c_str());//初始化一把武器 Gamer user(username.c_str(),100 ,temp); //初始化角色信息 user.speak(); return 0; }