m0_65215277 2022-05-25 18:14 采纳率: 57.1%
浏览 11
已结题

各位看下这个运算符重载的题咋做


#include <iostream>
#include <string>
#include <malloc.h>
#include <string.h> 

using namespace std;

class String{
private:
    char * s;
public:
    String();
    String(const char *);
    String(const String &);
    ~String();
    String & operator=(const char *);
    String & operator=(const String &);
    String operator+(const char *);
    String operator+(const String &);
    String & operator+=(const char *);
    String & operator+=(const String &);
    friend istream & operator>>(istream &, String &);
    friend ostream & operator<<(ostream &, const String &);
    friend bool operator==(const String &, const char *);
    friend bool operator==(const String &, const String &);
    friend bool operator!=(const String &, const char *);
    friend bool operator!=(const String &, const String &);
};

String::String(){
    s = NULL;
}

String::String(const char *s){
    this->s = (char*)s;
}

String::String(const String &s){
    this->s = (char*)s.s;
}

String::~String(){}

String & String::operator=(const char *c){
    static String s = String("1");
    static String s2 = String("0");
    if (this->s == c)
        return s;
    else
        return s2;
}

String & String::operator=(const String &s){
    static String s3 = String("1");
    static String s2 = String("0");
    if (this->s == s.s)
        return s3;
    else
        return s2;
}

String String::operator+(const char *c){
    if (this->s == "")
        this->s = (char *)c;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(c));
        strcpy(name, this->s);
        strcat(name, c);
        this->s = name;
    }
    return *this;
}

String String::operator+(const String &s){
    if (this->s == "")
            this->s = (char *)s.s;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(s.s));
        strcpy(name, this->s);
        strcat(name, s.s);
        this->s = name;
    }
    return *this;
}

String & String::operator+=(const char *s){
    if (this->s == "")
        this->s = (char *)s;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(s));
        strcpy(name, this->s);
        strcat(name,s);
        this->s = name;
    }
    return *this;
}

String & String::operator+=(const String &s){
    if (this->s == "")
        this->s = (char *)s.s;
    else{
        char *name = (char *)malloc(strlen(this->s) + strlen(s.s));
        strcpy(name, this->s);
        strcat(name, s.s);
        this->s = name;
    }
    return *this;
}

istream & operator>>(istream &input, String &s){
    string str;
    input >> str;
    s.s = (char*)str.c_str();
    cout << s.s <<endl;
    return input;
}

ostream & operator<<(ostream &output, const String &s){
    output << s.s;
    return output;
}

bool operator==(const String &s, const char *c){
    if (s.s == c)
        return true;
    return false;
}

bool operator==(const String &s, const String &c){
    if (s.s == c.s)
        return true;
    return false;
}

bool operator!=(const String &s, const char *c){
    if (s.s != c)
        return true;
    return false;
}

bool operator!=(const String &s, const String &c){
    if (s.s != c.s)
        return true;
    return false;
}


int main()
{
    String s;  
    s += "hello";
    cout << s << endl;
    String s1("String1");
    String s2("copy of "); 
    s2 += "String1";
    cout << s1 << "\n" << s2 << endl;
    String s3;
    cin >> s3;
    cout << s3 << endl;
    String s4("String4"), s5(s4);
    cout << (s5 == s4) << endl;
    cout << (s5 != s4) << endl;
    String s6("End of "), s7("my string.");
    s6 += s7;
    cout << s6 << endl;
    return 0;
}

img


运行之后咋没有任何效果?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 6月2日
    • 创建了问题 5月25日

    悬赏问题

    • ¥15 win2012 iscsi ipsec
    • ¥15 封装的 matplotlib animation 不显示图像
    • ¥15 python摄像头画面无法显示
    • ¥15 关于#3d#的问题:d标定算法(语言-python)
    • ¥15 cve,cnnvd漏洞扫描工具推荐
    • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
    • ¥15 如何构建全国统一的物流管理平台?
    • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
    • ¥15 用js遍历数据并对非空元素添加css样式
    • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)