C++运算符的重载

#include <iostream>
#include <cassert>
using namespace std;
class Test;
void fun1(Test);
class Test {
//友元函数,不属于成员函数,特点可以直接访问类的私有成员,友元函数没有this指针
    //成员函数在类外实现需要加上作用域 Test::
    friend void fun1(Test& t);

//重载输出运算符 ostream是类型名,operator<<是函数名
    friend ostream& operator<<(const Test& t);
    friend ostream& operator<<(ostream& out, const Test& t);
    //重载输出运算符
    friend istream& operator>>(istream& in, const Test& t);
public:
    Test(int data = 0)
    {
        m_data = data;
    }
    void fun()
    {
        m_data = 100;
    }
public:
    int GetData()const
    {
        return m_data;
    }

private :
    int m_data;
};


void fun1(Test& t)
{
    int value = t.GetData;
}
ostream& operator<<(const Test& t)
{
    // TODO: 在此处插入 return 语句
}
ostream& operator<<(ostream& out, const Test& t)
{
    out << t.m_data;
    return out;
}

istream& operator>>(istream& in, const Test& t)
{
    in >> t.m_data;
    return in;
}
void main()
{
    Test t(10), t1(100), t2(200);
    cout << ":" << t1 << ":" << t2 << endl;
}
class Complex
{

    Complex(int real,int imag):m_real(real),m_imag(imag)
    {}
    ~Complex()
    {}
    //友元函数
    friend Complex operator+(int i,const Complex&c);
public:
    //成员方法
    Complex operator+(const Complex& c)
    {
        Complex tmp(m_real + c.m_real, m_imag + c.m_imag);
        return tmp;
    }
    //重载加整数
    Complex operator+(int i)
    {
        Complex tmp(m_real + i,m_imag+i);
        return tmp;
    }
    Complex operator+(int i, const Complex& c)
    {
        Complex tmp(i + c.m_real, c.m_imag);
        return tmp;
    }
private:
    int m_real;
    int m_imag;
};


void main()
{
    Complex c1(1, 2), c2(3, 4);
    Complex c = c1 + c2;
    c = c1 + 10;//当对象在左边时会自动调用对象的内部成员函数
}


/*运算符重载是具有特殊函数名的函数,函数名字为:关键字operator后面接需要重载的运算符符号
//函数原型:返回值类型operator操作符(参数列表)
class MyInt

{

public:
    //MyInt(int i = 0) :m_i(i)//参数列表的方式进行初始化
    MyInt(int i = 0)
    {
        m_i = i;
    }
    //重载在类里面的函数是成员函数,参数的个数比运算符少一个,少的这个通过引用进行调用
    MyInt operator+(const MyInt& i)
    {
        MyInt tmp(this->m_i + i.m_i);
        return tmp;
    }
    MyInt operator-(const MyInt& i)
    {
        MyInt tmp(this->m_i + i.m_i);
        return tmp;
    }
    MyInt operator*(const MyInt& i)
    {
        MyInt tmp(this->m_i + i.m_i);
        return tmp;
    }
    MyInt operator/(const MyInt& i)
    {
        MyInt tmp(this->m_i + i.m_i);
        return tmp;
    }
public:
    //重载++运算符,根据参数来区分是前++还是后++
    MyInt& operator++()//++a
    {
        m_i++;
        return*this;
    }
    MyInt& operator++(int)//a++
    {
        MyInt tmp = *this;
        m_i++;
        return tmp;
    }
private:
    int m_i;
};
int  main()
{
    MyInt a = 1;
    MyInt b = 2;
    MyInt c = a + b;
}
*/


class String
{
public:
    String(const char* str = "")
    {
        m_data = (char*)malloc(strlen(str) + 1);
        assert(m_data != NULL);
        strcpy(m_data, str);

    }
    //拷贝构造
    String(const String& s)
    {
        //浅拷贝,只拷贝了它的指向,即两个指整同时指向了同一空间
        m_data = s.m_data;
    }
    //深拷贝,开辟空间
    String(const String& s)
    {
        m_data = (char*)malloc(strlen(s.m_data) + 1);
        assert(m_data != NULL);
        strcpy(m_data, s.m_data);
    }
    ~String()
    {
        free(m_data);
        m_data = NULL;
    }
    //赋值
    String&opretor=(const String&s)
    {
        //1.判断是否自己给自己赋值
        if (this != &s)
        {
            //2.释放原有空间
            free(m_data);
            //3.重新开辟空间
            m_data = (char*)malloc(strlen(s.m_data) + 1);
            assert(m_data != NULL);
            //4.拷贝
            strcpy(m_data, s.m_data);
        }
    return *this;
    }
public:
String operator+(const String &t)
{
  String newStr;
  newStr.m_data=realloc(newStr.m_data,strlen(m_data)+strlen(t.m_data)+1);
  assert(newStr.m_data!=NULL);
  strcpy(newStr.m_data,m_data);
  strcat(newStr.m_data,t.m_data);
  return newStr;
}
//内存泄漏
String operator+(const String &t)
{
//开辟两字符串长度之和的空间大小
    char*new_data=(char*)malloc(strlen(m_data)+strlen(t.m_data)+1);
    assert(new_data!=NULL);
    strcpy(new_data,m_data);
    strcat(new_data,t.m_data);
//构造对象
String newStr(new_data);
return newStr;
}
String& operator+=(const String& t)
{
    char* new_data = (char*)malloc(strlen(m_data) + strlen(t.m_data) + 1);
    assert(new_data != NULL);
    strcpy(new_data, m_data);
    strcat(new_data, t.m_data);
    //将原来的空间释放
    free(m_data);
    m_data = new_data;
    return *this;
}
public:
{
    bool operator==(const String& s)
    {
        return (strcmp(m_data, s.m_data) == 0);
    }
    bool operator!=(const String& s)
    {
        return !(*this== s);
    }
    bool operator>(const String& s)
    {
        return (strcmp(m_data, s.m_data) >0);
    }
    bool operator<=(const String& s)
    {
        return (strcmp(m_data, s.m_data) <= 0);
    }
    bool operator<(const String& s)
    {
        return (strcmp(m_data, s.m_data) <=0);
    }
}
private :
    char* m_data;
};

void main()
{
    String m_data;
    String s1("abc");
    String s2("xyz");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值