10.2总结

cpp 复制代码
#include <iostream>

using namespace std;

class MyString
{
private:
    string pos1;
    string pos2;
public:
    MyString()
    {}
    MyString(string pos1,string pos2):pos1(pos1),pos2(pos2)
    {
        cout << "调用了有参构造" << endl;
    }
    MyString(const MyString &other):pos1(other.pos1),pos2(other.pos2)
    {
        cout << "调用了拷贝构造函数" << endl;
    }
    const MyString operator+(const MyString &R)const
    {
        MyString t;
        t.pos1 = pos1+R.pos1;
        t.pos2 = pos2+R.pos2;
        return  t;
    }

    const MyString operator>(const MyString &R)const
    {
        MyString t;
        t.pos1 = (pos1>R.pos1)?"true":"false";
        t.pos2 = (pos2>R.pos2)?"true":"false";
        return  t;
    }
    const MyString operator<(const MyString &R)const
    {
        MyString t;
        t.pos1 = (pos1<R.pos1)?"true":"false";
        t.pos2 = (pos2<R.pos2)?"true":"false";
        return  t;
    }
    const MyString operator==(const MyString &R)const
    {
        MyString t;
        t.pos1 = (pos1==R.pos1)?"true":"false";
        t.pos2 = (pos2==R.pos2)?"true":"false";
        return  t;
    }
    void input()
    {
        cout << "请输入pos1: ";
        cin >> pos1;
        cout << "请输入pos2: ";
        cin >> pos2;
    }


    void output() const
    {
        cout << "pos1: " << pos1 << ", pos2: " << pos2;
    }


    void show()
    {
        cout << "pos1的长度为" << pos1.size() << endl;
        cout << "pos2的长度为" << pos2.size() << endl;
        cout << "pos1= " << pos1 << " " << "pos2= " << pos2 << endl;

    }

};

int main()
{
    //    MyString s1("z","x");
    //    MyString s2("b","a");
    MyString s1;
    s1.input();
    // s1.output();
    MyString s2;
    s2.input();
    //s2.output();
    MyString s =s1+s2;
    MyString s3 =s1>s2;
    s.show();
    s3.show();
    return 0;
}

相关推荐
毕设源码-邱学长6 小时前
【开题答辩全过程】以 基于Java的学校住宿管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
rookieﻬ°7 小时前
PHP框架漏洞
开发语言·php
busideyang7 小时前
为什么推挽输出不能接收串口数据,而准双向口可以?
c语言·stm32·单片机·嵌入式硬件·嵌入式
炸膛坦客8 小时前
单片机/C/C++八股:(二十)指针常量和常量指针
c语言·开发语言·c++
爱编码的小八嘎8 小时前
C语言完美演绎4-8
c语言
兑生8 小时前
【灵神题单·贪心】1481. 不同整数的最少数目 | 频率排序贪心 | Java
java·开发语言
炸膛坦客9 小时前
单片机/C/C++八股:(十九)栈和堆的区别?
c语言·开发语言·c++
零雲9 小时前
java面试:了解抽象类与接口么?讲一讲它们的区别
java·开发语言·面试
Jay_Franklin9 小时前
Quarto与Python集成使用
开发语言·python·markdown
2401_8318249610 小时前
代码性能剖析工具
开发语言·c++·算法