zoukankan      html  css  js  c++  java
  • C++ 运算符重载

    person.h


    #include <string.h>
    #include <iostream>
    #include <malloc.h>
    using namespace std;

    #ifndef _person
    #define _person

    class person{
    private: //成员变量
        int id;
        char * name;
    public: //函数
        void printMsg();
        void setId(int id);
        void setName(char * name);
        int getNameLen();
    public: //构造函数
        explicit person();
        explicit person(int id, char * name);
    public: //拷贝构造函数
         person(person& temp){
            this->id = temp.id;
            this->name = new char[20];
            strcpy(this->name, temp.name);
        }
    public: //运算符重载
        void operator=(person& temp){
            this->id = temp.id;
            this->name = new char[20];
            strcpy(this->name, temp.name);
        }
        //&  引用传递
        person operator+(person& temp){
             person newPerson;
             newPerson.setId(this->id + temp.id);
             newPerson.setName(this->name);
             return newPerson;
        }
        //转换成int  
        //类型转换 1、不能指定返回类型  2、无参
        // 显示转换 运算符重载 不能定义为全局
        //--》 int i = (int)onePerson;
        operator int(){
            return this->id;
        }
        //前置++
        person& operator++(){
            this->id++;
            return *this;
        }
        //后置++ 先赋值后加
        person operator++(int){
            person newPerson=*this;
            this->id++;
            return newPerson;
        }
        //下标取字符
        char operator[](int temp){
            //cout<<strlen(this->name)<<endl;
            if(this->name==NULL) return '\0';
            if(temp>=strlen(this->name)) return '\0';
            return this->name[temp];
        }
        /*
        new 运算符重载
            1、必须是static [注意:此处可以不加static 编译器都认为是静态的运算符重载]
            2、返回值必须是 void*
            3、可以带多个参数, 但第一个参数必须是size_t  
                [typedef unsigned int size_t;]
        */
        void* operator new(size_t){
            void* newPerson = malloc(sizeof(person));
            return newPerson;
        }
        void* operator new[](size_t temp){
            cout<<temp<<endl;
            void* newPerson = malloc(sizeof(person));
            return newPerson;
        }
        /*
        delete运算符重载
            1、必须是static [注意:此处可以不加static 编译器都认为是静态的运算符重载]
            2、必须返回void类型
            3、可以带多个参数
                3.1、第一个参数必须释放内存的地址 void*
                3.2、如果指定了第二个参数,则必须是 size_t 类型
        */
        void operator delete(void* temp, size_t t){
            if(temp!=NULL){
                free(temp);
                temp=NULL;
            }    
        }
    public: //析构函数
        ~person();


    public: //友元
        //friend person operator+(person& temp1, person& temp2);
        friend ostream& operator<<(ostream&, person&);
        friend void operator>>(istream&, person&);
    };
    #endif _person


    person.cpp

    #include "person.h"
    #include <iostream>
    using namespace std;
    #include <string.h>

    /*-----------------------
            构造函数
    ------------------------*/
    person::person(){
        this->id = 0;
        this->name = new char[20];
        strcpy(this->name, "itao");
    }
    person::person(int id, char * name){
        this->id = id;
        this->name = new char[20];
        strcpy(this->name, name);
    }

    /*-----------------------
            常用函数
    ------------------------*/
    void person::printMsg(){
        if(this->name!=NULL)
            cout<<"id="<<this->id<<" name="<<this->name<<endl;
        else
            cout<<"id="<<this->id<<endl;
    }

    void person::setId(int id){
        this->id = id;
    }

    void person::setName(char * name){
        this->name = new char[20];
        strcpy(this->name, name);
    }

    int person::getNameLen(){
        return strlen(this->name);
    }

    /*-----------------------
            析构函数
    ------------------------*/
    person::~person(){
        if(this->name!=NULL){
            delete this->name;
            this->name = NULL;
        }
    }

     

    operator.cpp

    #include <iostream>
    #include <string.h>
    using namespace std;
    #include <Afxwin.h>
    #include "person.h"

    //全局 运算符重载  在person类中声明为友元 才有访问成员变量的权限
    /*
    person operator+(person& temp1, person& temp2){
         person newPerson;
         newPerson.id = temp1.id + temp2.id;
         newPerson.setName(temp1.name);
         return newPerson;
    }


    */

    //输出 运算符重载  需在类中声明为 友元
    ostream& operator<<(ostream& out, person& temp){
        out<<temp.id<<endl;
        out<<temp.name<<endl;
        return out;
    }
    //输入 运算符重载 需在类中声明为 友元
    void operator>>(istream& in, person& temp){
        cout<<"please input the person's id:";
        in>>temp.id;
        cout<<"please input the person's name:";
        in>>temp.name;
    }

    int main(){
        /*
        new 运算符重载
        */
        person* onePerson = new person;
        cout<<sizeof *onePerson<<endl;
        onePerson->printMsg();
        delete onePerson; //先调用析构函数 再调用 delete运算符重载

        //cout<<onePerson<<endl;
        return 0;
    }

    int test5(){
        person onePerson(21, "qintangtao");
        int i;
        int len = onePerson.getNameLen();
        for(i=0; i<len; i++){
            cout<<onePerson[i]<<endl;
        }

        return 0;
    }

    int test4(){
        person onePerson(4, "itao");
        person otherPerson=onePerson++; //先赋值后加
        cout<<onePerson; //5
        cout<<otherPerson; //4
        return 0;
    }

    int test3(){
        person onePerson(4, "heihei");
        //int i = (int)onePerson; //用到了运算符重载
        //cout<<"i="<<i<<endl;

        /*
        cout<<onePerson<<endl;
        //等价
        ::operator<<(cout, onePerson).operator<<(endl);
        */

        cin>>onePerson;
        cout<<onePerson;

        return 0;
    }



    int test2(){
        person onePerson(1, "itao");
        person twoPerson(2, "love");
        person threePerson;
        threePerson = onePerson + twoPerson; //注意此处 返回的是临时变量 在赋值时 调用的是拷贝构造函数 而不是 赋值运算符重载
        //等价于 全局函数 [注意:全局函数和局部函数重名 只能取其中一个]
        //threePerson = ::operator+(onePerson, twoPerson);
        threePerson.printMsg();

        person otherPerson = onePerson;
        return 0;
    }

    int test1(){
        /*string a("Hello Word");
        a+=" itao";
        cout<<a.c_str()<<endl;*/

        CPoint a(100, 100);
        CPoint b(200, 200);
        CPoint c;
        /*c.x = a.x + b.x;
        c.y = a.y + b.y;
        */
        c = a + b;
        cout<<c.x<<endl<<c.y<<endl;
        return 0;
    }

     

     

  • 相关阅读:
    Win10 UWP Tile Generator
    Win10 BackgroundTask
    UWP Tiles
    UWP Ad
    Win10 build package error collections
    Win10 八步打通 Nuget 发布打包
    Win10 UI入门 pivot multiable DataTemplate
    Win10 UI入门 导航滑动条 求UWP工作
    UWP Control Toolkit Collections 求UWP工作
    Win10 UI入门 SliderRectangle
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2761737.html
Copyright © 2011-2022 走看看