美文网首页
C++面向对象继承与操作符重载

C++面向对象继承与操作符重载

作者: Dalvik_ | 来源:发表于2021-04-02 17:57 被阅读0次

    1.类外运算符重载。

    //操作符重载定义   operator 关键字
    #include <iostream>
    
    using namespace std;
    
    class Person {
    private:
        int x;
        int y;
    public:
        Person(int x, int y) : x(x), y(y) {}
    
        void setX(int x) {
            this->x = x;
        };
    
        int getX() {
            return this->x;
        }
    
        void setY(int y) {
            this->y = y;
        };
    
        int getY() {
            return this->y;
        }
    };
    
    //类外操作符重载 重载+
    Person operator+(Person &p1, Person &p2) {
        return Person(p1.getX() + p2.getX(), p1.getY() + p2.getY());
    }
    
    int main() {
        Person p1(1, 2);
        Person p2(3, 4);
        Person p = p1 + p2;
        cout << "person:x=" << p.getX() << " y=" << p.getY() << endl;
        return 0;
    }
    

    2.类里运算符重载。

    //将操作符重载写在类里边 
    #include <iostream>
    
    using namespace std;
    
    class Person {
    private:
        int x;
        int y;
    public:
        Person(int x, int y) : x(x), y(y) {}
    
        void setX(int x) {
            this->x = x;
        };
    
        int getX() {
            return this->x;
        }
    
        void setY(int y) {
            this->y = y;
        };
    
        int getY() {
            return this->y;
        }
    
        //类内操作符重载
        Person operator+(const Person &person) {
            return Person(this->x + person.x, this->y + person.y);
        }
    
        Person operator-(const Person &person) {
            return Person(this->x - person.x, this->y - person.y);
        }
    };
    int main() {
        Person p1(1, 2);
        Person p2(3, 4);
        Person p = p1 - p2;
        cout << "person:x=" << p.getX() << " y=" << p.getY() << endl;
    
        return 0;
    }
    
    
        // 对象++ 运算符
        void operator++() {
            this->x++;
            this->y++;
        }
    
        // ++对象 运算符
        void operator++(int) {
            this->x++;
            this->y++;
        }
    
        //自定义输出运算符
        friend ostream &operator<<(ostream &os, Person &person) {
            os << "custom os: " << person.getX() << "  " << person.getY() << endl;
            return os;
        }
    
    int main() {
        Person p1(1, 2);
        Person p2(3, 4);
        Person p = p1 - p2;
        p++;
        ++p;
        cout << "person:x=" << p.getX() << " y=" << p.getY() << endl;
    
       cout << p << endl;
        return 0;
    }
    
    

    3.括号运算符。

    // [] 运算符重载
        int operator[](int index) {
            return this->arrayValue[index]; // 系统的
        }
    

    4.C++对象继承。

    class Person {
    public:
        char *name;
        int age;
    
        Person(char *name, int age) : name(name), age(age) {}
    };
    // 默认私有 
    // 公开继承  才可以调用父类的成员
    // 私有继承 在类里边是可以访问父类的成员。但是在类外边不行
    class Student : public Person {
    public:
        char *className;
    public:
        Student(char *name, int age, char *className) : Person(name, age), className(className) {}
    };
    
    int main() {
        Student student("张三", 88, "C++");
        cout << student.name << endl;
        return 0;
    }
    
    

    C++多继承

    • 会出现二义性
    class A {
    public:
        virtual void show() {
            cout << "俺是A类的show方法" << endl;
        }
    };
    
    class B {
    public:
        void show() {
            cout << "俺是B类的show方法" << endl;
        }
    };
    
    class Main : public A, B {
    public:
        //2 子类中重写该方法
        void show() {
            cout << "俺是Main类的show方法" << endl;
        }
    };
    
    int main() {
        //会产生二义性
        Main main1;
        main1.show();
        //解决方案
        //1 指定调用哪个父类的方法
        main1.A::show();
        //2 子类中重写该方法
        main1.show();
    
        return 0;
    }
    
    
    • 处理二义性
    //1 指定调用哪个父类的方法
       main1.A::show();
    
    //2 子类中重写该方法
       void show() {
           cout << "俺是Main类的show方法" << endl;
       }
    
    //3 virtual   【虚基类】 属于 虚继承的范畴
    class Object {
    public:
       int mum;
    public:
       virtual void show() {
           cout << "俺是Object类的show方法" << endl;
       }
    };
    
    class A : virtual public Object {
    public:
    };
    
    class B : virtual public Object {
    public:
    };
    
    class Main : public A, B {
    public:
    
    };
    
    int main() {
       //virtual 处理二义性
       Object object;
       Main main3;
       A a;
    
       object.show();
       main3.show();
       a.show();
    
       object.mum = 20;
       a.mum = 22;
    
       cout << object.mum << endl;
       cout << a.mum << endl;
       cout << main3.mum << endl;
       return 0;
    }
    //输出:
    俺是Object类的show方法
    俺是Object类的show方法
    俺是Object类的show方法
    20
    22
    54 //子类没有赋值  调用父类的值 父类没有初始化值 所以是随机值
    

    相关文章

      网友评论

          本文标题:C++面向对象继承与操作符重载

          本文链接:https://www.haomeiwen.com/subject/dkipcltx.html