【牛客带你学编程C++方向】项目练习第1期(截止2.5)


C++项目练习:第1期
练习时间:1月22日-2月5日(2周)
活动规则:
  • 每一期一个项目,届时会开新帖发布     
  • 学员直接将答案提交到该贴评论区即可     
  • 两周后,公布导师参考答案     
  • 导师评选出当期最佳代码     
奖励:牛客大礼包一份(牛客定制水杯 牛客定制笔 牛客定制程序员徽章 滑稽抱枕)
参与方式:直接将你的代码回复到本帖评论区

-----------------------------------------------------

本期题目:

编写MyString的构造函数、拷贝构造函数、析构函数和赋值函数
已知MyString的原型如下
class MyString
{
 public:
     MyString (const char *str=NULL);
     MyString (const MyString &other);
     ~ MyString(void);
     MyString & operator = (const MyString & other);
 private:
     char *m_data;
};

参与方式:直接将你的代码回复到本帖评论区


-------------------------------
本期最佳代码获得者:@3366159 
代码已设置为精彩评论!
全部评论
/* Problem:编写MyString的构造函数、拷贝构造函数、析构函数和赋值函数 Author:QiZhao Data:2018-02-04 Description: Copyright 2018 QiZhao. All rights reserved. */ #include<cstring> #include<iostream> #include<cstdlib> using namespace std; class MyString { public: MyString (const char *str = NULL); MyString (const MyString &other); ~ MyString(void); MyString & operator = (const MyString & other); friend ostream& operator<<(ostream &os, MyString &str); private: char *m_data; }; //构造函数 MyString::MyString(const char *str) { if(str == NULL) { m_data = new char[1]; *m_data = '\0'; } else { int len = strlen(str); m_data = new char[len + 1]; if(m_data = NULL) { cout << "内存分配失败" << endl; exit(-1); } strcpy(m_data, str); } cout << "调用构造函数" << endl; } //拷贝构造函数 MyString::MyString(const MyString &other) { int len = strlen(other.m_data); m_data = new char[len + 1]; if(m_data = NULL) { cout << "内存分配失败" << endl; exit(-1); } strcpy(m_data, other.m_data); cout << "调用拷贝构造函数" << endl; } //析构函数 MyString::~MyString(void) { delete[]m_data; m_data = NULL; cout << "调用析构函数" << endl; } //赋值函数 MyString& MyString::operator= (const MyString & other) { if(this != &other) { delete[]m_data; int len = strlen(other.m_data); m_data = new char[len + 1]; if(m_data = NULL) { cout << "内存分配失败" << endl; exit(-1); } strcpy(m_data, other.m_data); } return *this; } //输出 ostream& operator<<(ostream &os, MyString &str) { os << str.m_data; return os; } int main() { char str[] = "abcdef"; char *p = NULL; cout << "**********************构造函数测试**********************" << endl; MyString test1(str); MyString test2(p); cout << "**********************拷贝构造函数测试**********************" << endl; MyString test(test1); cout << "**********************赋值函数测试**********************" << endl; cout << "调用前:" << endl; cout << test1 << endl; cout << test2 << endl; cout << "调用后:" << endl; test2 = test1; cout << test1 << endl; cout << test2 << endl; cout << "**********************析构函数测试**********************" << endl; return 0; }
点赞 回复 分享
发布于 2018-02-05 23:59
#include <string.h> class MyString { public:     MyString(const char *str);     MyString(const MyString& other);     ~MyString();     MyString& operator=(const MyString& other); public:     // Never be nullptr(or NULL)     char *m_data;     void __copy(const char* str); }; MyString::MyString(const char *str) : m_data(nullptr) {     __copy(str); } MyString::MyString(const MyString& other) : m_data(nullptr) {     __copy(other.m_data); } MyString& MyString::operator=(const MyString& other) {     if (this != &other) {         delete[] m_data;         __copy(other.m_data);     }     return *this; } MyString::~MyString() {     delete[] m_data; } void MyString::__copy(const char* str) {     if (str == nullptr) {         m_data = new char[1];         m_data[0] = '\0';     } else {         int len = strlen(str) + 1;         m_data = new char[len];         strncpy(m_data, str, len);     } }
点赞 回复 分享
发布于 2018-01-25 16:40
//原型函数 class MyString{ public: MyString(const char* str = NULL);//构造函数 MyString(const MyString& other);//拷贝构造函数 ~MyString(void);//析构函数 MyString& operator=(const MyStirng& other);//赋值函数 private: char* m_data; }; //构造函数 MyString :: MyString(const char* str){ if(str == NULL) { m_data = new char[1]; *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length + 1]; strcpy(m_data, str); } } //拷贝构造函数 MyString :: MyString(const MyString& other){ int length = strlen(other.m_data); m_data = new char[length + 1]; strcpy(m_data, other.m_data); } //析构函数 MyString :: ~MyString(){ delete[]m_data; m_data = NULL; } //赋值函数 MyString& MyString :: operator=(const MyString& other){ if(&other == this) { return *this; } else { delete[]m_data; m_data = NULL; int length = strlen(other.m_data); m_data - new char[length + 1]; strcpy(m_data, other.m_data); return *this; } }
点赞 回复 分享
发布于 2018-01-23 11:37
#include "iostream" using namespace std; class MyString { public:     MyString(const char *str = NULL);//构造函数,默认为空     MyString(const MyString &other);//拷贝构造函数     ~MyString(void);//析构函数     MyString & operator = (const MyString & other);//赋值函数     void a() { cout << *m_data << endl; } private:     char *m_data; }; MyString::MyString(const char *str)//构造函数,默认为空 {     int len = strlen(str)+1;     m_data = new char[len];     if (m_data != NULL)     {         if (str == NULL)         {             m_data = '\0';         }         else         {             if (m_data != str)             {                 strcpy_s(m_data, len, str);             }         }     } } MyString::MyString(const MyString &other)//拷贝构造函数 {     if (other.m_data != m_data)     {         int len = strlen(other.m_data) + 1;         if (other.m_data != NULL)         {             m_data = new char[len];         }         if (m_data != NULL)         {             strcpy_s(m_data, len, other.m_data);          }     } } MyString::~MyString(void)//析构函数,无参数无输出 {     delete[] m_data;     m_data = NULL; } MyString & MyString::operator= (const MyString & other)//赋值函数,返回引用 {     if (this==&other)     {         return *this;     }     delete[] m_data;     int len = strlen(other.m_data) + 1;     m_data = new char[len];     if (m_data != NULL)     {         if (this->m_data != other.m_data)         {             strcpy_s(m_data, len, other.m_data);         }     }     return *this; }
点赞 回复 分享
发布于 2018-02-01 13:57
#include<iostream> class MyString {   public:     MyString (const char *str=NULL);     MyString (const MyString &other);     ~ MyString(void);     MyString & operator = (const MyString & other);   private:       char *m_data; }; #include "MyString.h" using namespace std; MyString::MyString(const char *str):m_data(NULL) {     char *p = NULL;     if(str == NULL)     {         p = new char[1];         if(p == NULL)         {             //分配空间失败             cout<<"分配空间失败"<<endl;             exit(-1);         }         else         {             *p = '\0';             m_data = p;             p = NULL;         }     }     else     {         size_t length = strlen(str);         p = new char[length +1];         if(p == NULL)         {             //分配空间失败             cout<<"分配空间失败"<<endl;             exit(-1);         }         else         {             strcpy(p,str);             m_data = p;             p = NULL;         }     } } MyString::MyString (const MyString &other) {     char *p = NULL;          size_t length = strlen(other.m_data);          p = new char[length+1];     if(p == NULL)     {         //分配空间失败         cout<<"分配空间失败"<<endl;         exit(-1);     }     else     {         strcpy(p,other.m_data);         m_data = p;         p = NULL;     } } MyString::~MyString(void) {     if(m_data != NULL)     {         delete[] m_data;         m_data = NULL;     } } MyString & MyString::operator=(const MyString & other) {     if(this == &other)     {         return *this;     }     char *p = NULL;          size_t length = strlen(other.m_data);          p = new char[length+1];     if(p == NULL)     {         //分配空间失败         cout<<"分配空间失败"<<endl;         //exit(-1);         return *this;//没有修改     }     else     {         strcpy(p,other.m_data);         delete[] m_data;         m_data = p;         p = NULL;     }     return *this; }
点赞 回复 分享
发布于 2018-02-01 13:30
class MyString{ public:     MyString(const char *str);     MyString(const MyString &other);     ~MyString(void);     void put_my(void);     MyString & operator = (const MyString & other); private:     char *m_data; }; MyString::MyString(const char *str) {     m_data =new char[strlen(str)+1];     strcpy(m_data,str); } MyString::MyString(const MyString &other) {     m_data =new char[strlen(other.m_data)+1];     strcpy(m_data,other.m_data); } MyString::~MyString(void) {     delete[] m_data; } MyString& MyString::operator= (const MyString & other) {     if(&other!=this)     {         MyString str(other);         delete[] m_data;         m_data =new char[strlen(other.m_data)];         strcpy(m_data,other.m_data);     }     return *this; } void MyString:: put_my(void) {     cout<<m_data<<endl; }
点赞 回复 分享
发布于 2018-01-30 15:12
#include<iostream> #include<stdlib.h> using namespace std; class MyString { public:     //构造函数     MyString(const char* str = NULL) :m_data(new char[strlen(str)+1])     {         strcpy(m_data,str);     }     //拷贝构造     MyString(const MyString& other)     {         if ((this != &other)&&(&other)!=NULL)         {             strcpy(m_data, other.m_data);         }     }     //析构函数     ~MyString()     {         if (m_data)             delete[] m_data;     }     //赋值函数     MyString& operator=(const MyString& other)     {         if (this != &other)         {             MyString tmp(other);             strcpy(m_data, other.m_data);         }     } private:     char* m_data; };
点赞 回复 分享
发布于 2018-01-26 18:15
#include <cstring> class MyString { public:     MyString(const char *str = nullptr);     MyString(const MyString &other);     ~MyString(void);     MyString & operator = (const MyString & other); private:     char *m_data; }; MyString::MyString(const char *str) {     if (str) {         m_data = new char[strlen(str) + 1];         strcpy(m_data, str);     }     else {         m_data = nullptr;     } } MyString::MyString(const MyString &other) {     if (other.m_data) {         m_data = new char[strlen(other.m_data)];         strcpy(m_data, other.m_data);     }     else {         m_data = nullptr;     } } MyString::~MyString() {     if (m_data) {         delete[]m_data;         m_data = nullptr;     } }; MyString &MyString::operator= (const MyString & other) {     if (&other == this)         return *this;     if (m_data) {         delete m_data;         m_data = nullptr;     }     if (other.m_data) {         m_data = new char[strlen(other.m_data)];         strcpy(m_data, other.m_data);     }     return *this; }
点赞 回复 分享
发布于 2018-01-26 12:41
//已知MyString函数原型 class MyString{ public: MyString(const char* str = NULL); MyString(const MyString& other); ~MyString(void); MyString& operator = (const MyString& other); private: char* m_data; }; //MyString的构造函数 MyString :: MyString(const char* str){ if(NULL == str) {   m_data = new char[1];   *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length + 1]; strcpy(m_data, str); } } //MyString的拷贝构造函数 MyString :: MyString(const MyString& other){ int length = strlen(other.m_data); m_data = new char[length + 1]; strcpy(m_data, other.m_data); } //MyString的析构函数 MyString :: ~MyString(void){ delete []m_data; m_data = NULL; } //MyString的赋值函数 MyString& MyString :: operator = (const MyString& other){ if(&other == this) { return *this; } else { delete []m_data; m_data = NULL; int length = strlen(other.m_data); m_data = new char[length + 1]; strcpy(m_data, other.m_data); return *this; } }
点赞 回复 分享
发布于 2018-01-25 14:27
哇!牛客终于出C++的课程啦!
点赞 回复 分享
发布于 2018-01-25 14:25
MyString::MyString(const char *str) {     if(!str)     {         m_data = new char[1];         strcpy(m_data, "\0");     }     else     {         int len = strlen(str);         m_data = new char[len + 1];         strcpy(m_data, str);     } } MyString::MyString(const MyString &other) {     int len = strlen(other.m_data);     m_data = new char[len + 1];     strcpy(m_data, other.m_data); } MyString::~MyString(void) {     delete []m_data;     m_data = nullptr; } MyString& MyString::operator=(const MyString &other) {     if(this != &other)     {         delete []m_data;         m_data = nullptr;         int len = strlen(other.m_data);         m_data = new char[len + 1];         strcpy(m_data, other.m_data);     }     return *this; }
点赞 回复 分享
发布于 2018-01-23 15:33
//编写MyString的构造函数、拷贝构造函数、析构函数和赋值函数 #include<string.h> class MyString { public:     MyString (const char *str=NULL);     MyString (const MyString &other);     ~MyString(void);     MyString & operator = (const MyString & other); private:     char *m_data; }; //构造函数  MyString::MyString(const char*str) {     if(str==NULL)     {         m_data=new char[1];         *m_data='\0';     }     else     {         int len=strlen(str);         m_data=new char[len+1];         strcpy(m_data,str);     } } //拷贝构造函数  MyString::MyString(const MyString &other) {     int len=strlen(other.m_data);     m_data=new char[len+1];     strcpy(m_data,other.m_data); } //析构函数  MyString::~MyString(void) {     delete[] m_data; }  //赋值函数  MyString & MyString::operator =(const MyString &other) {     if(this==other)     {         return *this;     }     delete[] m_data;     int len=strlen(other.m_data);     m_data=new char[len+1];     strcpy(m_data,other.m_data);     return *this; }
点赞 回复 分享
发布于 2018-01-23 11:06
MyString::MyString(const char* str) {     if (str == nullptr) {         m_data = new char[1];         *m_data = '\0';     }     else {         int length = strlen(str);         m_data = new char[length + 1];         strcpy(m_data, str);     } } MyString::MyString(const MyString& other) {     int length = strlen(other.m_data);     m_data = new char[length + 1];     strcpy(m_data, other.m_data); } MyString::~MyString() {     delete[] m_data; } MyString& MyString::operator = (const MyString& other) {     if(this == &other) return *this;     delete[] m_data;     m_data = nullptr;     m_data = new char[strlen(other.m_data) + 1];     strcpy(m_data, other.m_data);     return *this; }
点赞 回复 分享
发布于 2018-01-22 21:32
MyString::MyString(const char* str) {     if (str == nullptr) {         m_data = new char[1];         *m_data = '\0';     }     else {         int length = strlen(str);         m_data = new char[length + 1];         strcpy(m_data, str);     } } MyString::MyString(const MyString& other) {     int length = strlen(other.m_data);     m_data = new char[length + 1];     strcpy(m_data, other.m_data); } MyString::~MyString() {     delete[] m_data; } MyString& MyString::operator = (const MyString& other) {     if(this == &other) return *this;     delete[] m_data;     m_data = nullptr;     m_data = new char[strlen(other.m_data) + 1];     strcpy(m_data, other.m_data);     return *this; }
点赞 回复 分享
发布于 2018-01-22 21:32
class MyString{ public:     MyString (const char *str=NULL); //构造函数 ,默认为空      MyString (const MyString &other);//拷贝构造函数      ~ MyString(void);//析构函数      MyString & operator = (const MyString & other);//重载           friend ostream& operator<<(ostream &os, MyString &str);//输出 private:     char *m_data; }; MyString::MyString(const char *str){     if(!str){         m_data = new char[1];         *m_data = '\0';     }     else{         m_data = new char[ strlen(str) + 1 ];         strcpy(m_data,str);     } }  MyString::MyString(const MyString &other){     m_data = new char[ strlen(other.m_data) + 1];     strcpy(m_data,other.m_data);     //cout<<"Copy"<<endl; } MyString::~MyString(){     delete []m_data;     //cout<<"~MyString"<<endl; } MyString& MyString::operator =(const MyString &other){     //cout<<"operator ="<<endl;     if(this != &other){         delete []m_data; //防泄漏          m_data = new char[ strlen(other.m_data) + 1];         strcpy(m_data,other.m_data);     }     return *this; }  ostream& operator<<(ostream &os, MyString &str){     os << str.m_data;     return os; }
点赞 回复 分享
发布于 2018-01-22 14:58

相关推荐

09-13 08:41
服装/纺织设计
那一天的Java_J...:你第一次参加面试吗
点赞 评论 收藏
分享
牛客48826091...:哥们胸肌挺好看
点赞 评论 收藏
分享
评论
点赞
17
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务