C++ Exercises(六)

class  String  
{
public :
    String();
    String(
const  String &  another); // 拷贝构造函数
     explicit  String( const   char *  st);
    
explicit  String( int  size);
    String
&   operator   = ( const  String &  another); // 赋值构造函数
     virtual   ~ String();
    
int  GetLength() const ;
    
void  assign( const   char *  st);
    
void  print() const ;
    
bool   operator   == ( const  String &  another) const ; // 是否相等
     bool   operator   != ( const  String &  another) const
    friend String
&   operator   + ( const  String &  first, const  String &  second);
private :
    
char   * value;
    
int  len;

};

#include 
" stdafx.h "
#include 
" String.h "
#include 
< string .h >
#include 
< iostream.h >

/ /
//  Construction/Destruction
/ /

String::String()
{
    
this -> len  =   0 ;
    
this -> value  =  NULL;
}

String::String(
const  String &  another)
{
    
int  len1  =  another.GetLength();
    
this -> value  =   new   char [len1 + 1 ];
    strcpy(
this -> value,another.value);
    
this -> len  =  len1;
}

String::String(
const   char *  st)
{
    
int  len1  =  strlen(st);
    
this -> value  =   new   char [len1 + 1 ];
    strcpy(
this -> value,st);
    
this -> len  =  len1;
}

String::String(
int  size)
{
    
this -> value  =   new   char [size + 1 ];
    
this -> len  =  size;
}

String
&  String:: operator   = ( const  String &  another)
{
    
int  len1  =  another.GetLength();
    
if ( this -> value != NULL)
    {
        delete []
this -> value;
    }
    
this -> value  =   new   char [len1 + 1 ];
    strcpy(
this -> value,another.value);
    
this -> len  =  len1;
    
return   * this ;
}

String::
~ String()
{
    delete [] 
this -> value;
}

int  String::GetLength() const
{
    
return   this -> len;
}

void  String::assign( const   char   * st)
{
    
int  len1  =  strlen(st);
    
if ( this -> value != NULL)
    {
        delete []
this -> value;
    }
    
this -> value  =   new   char [len1 + 1 ];
    strcpy(
this -> value,st);
    
this -> len  =  len1;
}

void  String::print() const
{
    cout
<< this -> value << " \nLength:  " << this -> len << endl;
}

bool  String:: operator   == ( const  String &  another) const
{
    
if (strcmp( this -> value,another.value) == 0 )
    {
        
return   true ;
    }
    
else
    {
        
return   false ;
    }
}

bool  String:: operator   != ( const  String &  another) const
{
    
return   ! ( * this == another);
}

String
&   operator   + ( const  String &  first, const  String &  second)
{
    String 
* tmp  =   new  String(first.len + second.len);
    strcpy(tmp
-> value,first.value);
    strcat(tmp
-> value,second.value);
    
return   * tmp;
}

//  demo1.cpp : Defines the entry point for the console application.
//

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


int  main( int  argc,  char *  argv[])
{
    String str1,str3;
    
char  name[ 10 ] = " dyk " ;
    str1.assign(
" hello,world " );
    str1.print();
    String str2(str1);
    str2.print();
    str3 
=  str2;
    str3.print();
    
if (str1 == str2)
    {
        cout
<< " str1==str2!!! " << endl;
    }
    str3.assign(name);
    str3.print();
    String
*  pStr4  =   new  String(str3);
    pStr4
-> print();

    String str5(name);
//     str5 = "phinecos";
    
// 当类定义中提供了单个参数的构造函数时,
    
// 该类便提供了一种将其他数据类型的数值或变量转换为用户所定义数据类型
    str5.print();

    str5
= str1 + str2;
    str5.print();

    
return   0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值