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;
}


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/07/04/805782.html,如需转载请自行联系原作者

目录
相关文章