第四章课堂作业

#include<istream>  
using namespace std;  
#include<string.h>  
  
class String  
{  
  
public:  
  
    String();  
  
    virtual ~String();  
  
    String(int n);  
  
    String(const String &s2);  
  
    String(const char *str);  
  
    String & operator = (const String &s1);  
  
    friend bool operator >(String &string1, String &string2);  
  
    friend bool operator <(String &string1, String &string2);  
  
    friend bool operator == (String &string1, String &string2);  
  
    friend bool operator <= (String &string1, String &string2);  
  
    friend bool operator >= (String &string1, String &string2);  
  
    friend ostream& operator << (ostream& , String &);  
  
  
  
  
  
private:  
  
    char *s;  
  
};  
  
String::String()//初始化,默认长度为100  
{  
    s = new char[100];  
  
    s[0] = '\0';  
}  
  
String::String(int n)//构造一个大小为n的数组  
{  
    s = new char [n];  
  
    s[0] = '\0';  
}  
  
String::~String()//析构  
{  
    if(s)  
    {  
        delete s;  
    }  
    s = NULL;  
}  
  
String::String(const String &s2)//拷贝构造函数  
{  
    this->s = new char [strlen[s2.s]+1]  ;  //ERROR :C:\Users\Administrator\Desktop\I  can  do  it\fsada.cpp(69) : error C2107: illegal index, indirection not allowed  
                                      //ERROR: C:\Users\Administrator\Desktop\I  can  do  it\fsad//error //C2440: 'initializing' : //cannot convert from 'char *' to 'int'  
  
  
  
    s[0] = '\0';  
  
    strcpy(s, s2.s);  
}  
  
String::String(const char *str)  
{  
    s = new char[strlen[str]+1];//同上 = =。  
  
    s[0] = '\0';  
}  
  
String& String :: operator = (const String &s1)//赋值构造函数  
{  
    if(this == &s1)   
    {  
        return *this;  
    }  
    else  
    {  
        delete   []s1; //ERROR :No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called  
                       //ERRORE:fatal error C1903: unable to recover from previous error(s); stopping compilation  
  
                         
  
  
        s1 = new char [strlen[s1.s]+1];  
  
        return *this;  
    }  
  
}  
  
bool operator > (String &string1, String &string2)  
{  
    if(strcmp(string1.s , string2.s) > 0)   
          
        return true;  
  
    else  
  
        return false;  
}  
  
bool operator < (String &string1, String &string2)  
{  
    if(strcmp(string1.s , string2.s) < 0)   
          
        return true;  
  
    else  
  
        return false;  
}  
  
bool operator == (String &string1, String &string2)  
{  
    if(strcmp(string1.s , string2.s) == 0)   
          
        return true;  
  
    else  
  
        return false;  
}  
  
bool operator <= (String &string1, String &string2)  
{  
    if(strcmp(string1.s , string2.s) > 0)   
          
        return false;  
  
    else  
  
        return true;  
}  
  
bool operator >= (String &string1, String &string2)  
{  
    if(strcmp(string1.s , string2.s) < 0)   
          
        return false;  
  
    else  
  
        return true;  
}  
  
ostream& operator << (ostream& output , String & s3)  
{  
  
    output << s3.s << endl;  
  
    return output;  
  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值