c++17(4)-类,构造函数,复制构造函数,数组参数,返回数组(1)

#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{
    char mess[20]{"hello1"};
    RunMessage *myMess=new RunMessage(1,MesType::Input,mess);
	cout<<myMess->getStr()<<endl;
    delete myMess;
    return 0;
}

hello1

默认的复制构造函数

#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{
    char mess[20]{"hello1"};
    RunMessage myMess1{1,MesType::Input,mess};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
    return 0;
}
//learn.h

enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char msStr[])
          {
               id=msId;
               type=msType;
               strcpy(messStr,msStr);
            }
        
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              strcpy(messStr,msStr);            
         } 
    private:
        int id;
        MesType type;
        char messStr[20];
};

hello1
hello2

自定义复制构造函数

//learn.h
#include <iostream>
enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char msStr[])
          {
               id=msId;
               type=msType;
               strcpy(messStr,msStr);
            }
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               strcpy(messStr,otherMess.messStr);
            }            
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              strcpy(messStr,msStr);            
         } 
    private:
        int id;
        MesType type;
        char messStr[20];
};
#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{
    char mess[20]{"hello1"};
    RunMessage myMess1{1,MesType::Input,mess};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
    return 0;
}

hello1
construct1…
hello2

#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
 	cout<<myMess1.getStr()<<endl; 

   return 0;
}
//learn.h
#include <iostream>
enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               strcpy(messStr,msStr);
            }
          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               strcpy(this->messStr,otherMess.messStr);
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               strcpy(messStr,otherMess.messStr);
            }            
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              strcpy(messStr,msStr);            
         } 
    private:
        int id;
        MesType type;
        char messStr[20];
};

hello1
construct1…
hello2
hello1

//learn.h
#include <iostream>
enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               strcpy(messStr,msStr);
            }
          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               strcpy(this->messStr,otherMess.messStr);
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               strcpy(messStr,otherMess.messStr);
            }            
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              strcpy(messStr,msStr);            
         } 
    private:
        int id;
        MesType type;
        char messStr[20];
};
#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
 	cout<<myMess1.getStr()<<endl; 
    RunMessage myMess3(myMess1);
    myMess3=myMess2;
   return 0;
}

hello1
construct1…
hello2
hello1
construct1…
construct2…

发布了385 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/104162663