2020.3.23 C++

今天学习了C++一些内容:重要的地方就是重载符号

#include <iostream>

using namespace std;
/*
class My
{
    friend ostream& operator<<(ostream &out,My &ob);
private:
    int a;
    int b;
public:
    My()
    {
        a=0;
        b=0;
        cout<<"无参的构造函数"<<endl;
    }
    My(int a,int b):a(a),b(b)
    {
        cout<<"有参构造"<<endl;
        //this->a = a;
        //thid->b = b;
    }
    void showMy(void)
    {
        cout<<"a="<<a<<",b="<<b<<endl;
    }
    ~My()
    {
        cout<<"析构函数"<<endl;
    }

    //成员函数 重载前置++ ++ob1(先加 后使用)
    //编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator++()
    My& operator ++()//++ob1
    {
        //先加
        a++;
        b++;
        //后使用
        return *this;
    }


    //成员函数 重载后置++ ob1++(先使用 后加)
    //编译器 默认识别operator(a,int)//但是a可以用this代替operator++(int)
    My& operator++(int)//ob1++
    {
        //先使用(备份加之前的值)
        static My old=*this;
        //后加
        a++;
        b++;
        //返回备份值
        return old;
    }
    //重载前置‐‐ ‐‐ob3
    //编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator‐‐(
    My& operator --()
    {
        //先减
        a--;
        b--;
        //后使用(返回)
        return *this;
    }
    //重载后‐‐ ob4‐‐
    //编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator++(int)
    My& operator --(int)
    {
        //先使用
        static My old=*this;
        //再减
        a--;
        b--;
        return old;

    }

};
//普通全局函数 作为类的友元函数 重载<<运算符
ostream& operator<<(ostream &out,My &ob)
{
    out<<"a="<<ob.a<<",b="<<ob.b<<endl;

    return out;
}
void test01()
{
    My ob1(10,20);
    ob1.showMy();
    //重载<<直接输出自定义对象的值
    //operator<<(cout,obt);
    cout<<ob1<<endl;

    //成员函数  重载 ++运算符
    cout<<++ob1<<endl;

    //后置加加
    My ob2(10,20);
    cout<<ob2++<<endl;
    cout<<ob2<<endl;

    //成员函数 重载--运算符
    My ob3(10,20);
    cout<<"ob3"<<ob3<<endl;
    cout<<--ob3<<endl;

    My ob4(10,20);
    cout<<"ob4"<<ob4<<endl;
    cout<<ob4--<<endl;
    cout<<"ob4"<<ob4<<endl;


}

class My
{
private:
    int a;
    int b;
public:
    My():a(0),b(0)
    {
        cout<<"无参构造"<<endl;
    }
    My(int a,int b):a(a),b(b)
    {
        cout<<"有参构造"<<endl;
    }
    void showMy(void)
    {
        cout<<"a="<<a<<",b="<<b<<endl;
    }
    ~My()
    {
        cout<<"析构函数"<<endl;
    }
};
void test02()
{
    My ob1(10,20);
    ob1.showMy();

    //注意 旧对象 给新对象赋值 调用的是拷贝构造(默认的拷贝构造函数就是简单地赋值)
    My ob2 = ob1;
    ob2.showMy();

    My ob3;
    ob3=ob1;//此处才是调用的赋值=运算符(默认赋值=运算是浅拷贝)
    ob3.showMy();
}
*/
#include<string.h>
class My
{
private:
    char *name;
public:
    My()
    {
        cout<<"无参构造"<<endl;
    }
    My(char *name)
    {
        //根据实际传入的 参数 给this->name申请空间
        this->name=new char[strlen(name)+1];

        //将name指向的字符串 拷贝到this->name指向的空间中
        strcpy(this->name,name);

        cout<<"有参构造"<<endl;
    }
    ~My()
    {
        cout<<"析构函数"<<endl;
        if(this->name=NULL)//为什么要判断,就是因为调用的是无参构造,那就this->name 指向空,再次释放就会产生错误
        {
            delete [] this->name;
            this->name=NULL;
        }
    }
    void showMy()
    {
        cout<<"name="<<name<<endl;
    }
    //成员函数 重载=(赋值)运算符
    My& operator =(My &ob)//ob==ob1
    {
        //this==&ob
        if(this->name!=NULL)//说明this‐>name 以前有指向(重点)
        {
            //释放以前的指向空间
            delete [] this->name;
            this->name=NULL;
        }
        //申请空间
        this->name=new char[strlen(ob.name)+1];
        //拷贝内容
        strcpy(this->name,ob.name);

        return *this;//用*this就是将对象ob3用引用返回
    }
    bool operator ==(My &ob)
    {
        if(strcmp(this->name,ob.name)==0)
        {
            return true;
        }
        return false;
    }
    bool operator !=(My &ob)
    {
        if(strcmp(this->name,ob.name)!=0)
        {
            return true;
        }
        return false;
    }
};

void test03()
{
    My ob1("lucy");
    ob1.showMy();
    My ob2=ob1;//调用拷贝构造

    My ob3("bob");
    //没写重载函数,就不是重载
    ob3=ob1;
    ob3.showMy();
    if(ob1==ob2)
    {
        cout<<"ob1 == ob2"<<endl;
    }
    else
    {
        cout<<"ob1 != ob2"<<endl;
    }
    if(ob1!=ob3)
    {
        cout<<"ob1!=ob3"<<endl;
    }
    else
    {
      cout<<"ob1=ob3"<<endl;
    }
}

int main(int argc, char *argv[])
{
    test03();
    return 0;
}
 

发布了12 篇原创文章 · 获赞 0 · 访问量 65

猜你喜欢

转载自blog.csdn.net/weixin_41604325/article/details/105060335
C++