c++day4

#include <iostream>
#include <cstring>
using namespace std;
char c='\0';
class myString
{
private:
    char* str;
    int size;
    int rel;
    int vir;
public:
    myString():str(new char),size(0){}
    myString(char* p,int size):str(new char[size+1]),size(size)
    {
        strcpy(str,p);
    }
    myString(string s1):str(new char[s1.size()+1]),size(s1.length())
    {
        strcpy(str,s1.c_str());
    }
    //拷贝构造
    myString(const myString &other):str(new char[other.size+1]),size(size)
    {
        strcpy(str,other.str);
    }
    //拷贝赋值
    myString &operator=(const myString &other)
    {
        if(&other!=this)
        {
            delete[]str;
            str=new char[other.size+1];
            strcpy(str,other.str);
            this->size=other.size;

        }
        return *this;
    }
    //析构函数
    ~myString()
    {
        delete[]str;
    }
    //判空函数
    bool empty()
    {
        return size==0;
    }
    //size函数
    int size_()
    {
        return size;
    }
    //c_str函数
    const char* c_str()
    {
        return str;
    }
    //at函数
    char &at(int pos)
    {
        if(pos<0||pos>=size)
        {
            cout <<"位置不合理:" << endl;
            return c;
        }
        return str[pos];
    }
    void show()
    {
        cout <<this->rel <<"+" <<this->vir <<endl;
    }
    myString operator+(const myString m1);
    bool operator>(const myString m1);
    friend bool operator==(const myString m1,const myString m2);
    friend istream &operator>>(istream &in,myString &m1);
    friend ostream &operator <<(ostream &out,myString &m1);
};
myString myString::operator+(const myString m1)
{
    myString temp;
    temp.rel=this->rel+m1.rel;
    temp.vir=this->vir+m1.vir;
    return temp;
}
bool myString::operator>(const myString m1)
{
    return this->rel>m1.rel;
}
bool operator ==(const myString m1,const myString m2)
{
    return m1.rel==m2.rel&&m1.vir==m2.vir;
}
istream &operator>>(istream &in,myString &m1)
{
    in >> m1.rel >>m1.vir;
    return in;
}
ostream &operator <<(ostream &out,myString &m1)
{
    out <<m1.rel <<m1.vir;
    return out;
}
int main()
{
    char str[]="hello";
    myString str1(str,5);
    cout <<str1.empty() <<endl;
    cout <<str1.size_() <<endl;
    cout <<str1.c_str() <<endl;
    cout <<str1.at(3) <<endl;
    str1.at(3)='9';
    cout <<str1.at(3) <<endl;
    return 0;
}
Complex Complex::operator^(Complex c1)
{
    Complex temp;
    temp.rel=(this->rel)^c1.rel;
    temp.vir=(this->vir)^c1.vir;
    return temp;
}
Complex Complex::operator<<(int a)
{
    rel=rel<<a;
    vir=vir<<a;
    return *this;
}
Complex Complex::operator>>(int a)
{
    rel=rel>>a;
    vir=vir>>a;
    return *this;
}
Complex Complex::operator~()
{
    rel=~rel;
    vir=~vir;
    return *this;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值