美文网首页
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 //子类没有赋值  调用父类的值 父类没有初始化值 所以是随机值

相关文章

  • java 基本程序结构

    关键术语 简单性 剔除了部分C++的复杂结构,如头文件,指针运算,操作符重载等 面向对象 封装、继承、多态,其中...

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

    1.类外运算符重载。 2.类里运算符重载。 3.括号运算符。 4.C++对象继承。 C++多继承 会出现二义性 处...

  • Java技术栈梳理

    Java语言 基础, 基本类型, 操作符, 运算符, 表达式 面向对象, 类, 继承, 多态, 重写, 重载 S...

  • 清华毕业扫地僧,深入讲解Java基础+高级进阶300集,总计15

    前言 Java继承了C++语言面向对象技术的核心,又舍弃了C++语言中的指针、运算符重载以及多重继承的特性,同时引...

  • Go 语言中的组合设计

    介绍 在 C++,Java 等面向对象的语言中,继承和重载是整个语言的核心价值,而 Go 语言并非是完全面向对象的...

  • Kotlin --- Operator Overloading

    简述 Kotlin的操作符重载与C++类似,虽然没有C++那么强大,但是仍然可以实现Kotlin的操作符重载。 操...

  • iOS复习之重写与重载

    iOS面向对象的三大特征 封装、继承、多态 Swift-重写与重载的使用和比较

  • Javascript——面向对象程序设计和继承

    Javascript——面向对象程序设计和继承 面向对象设计模式 创建对象——单个对象 字符字变量 new 操作符...

  • 09.类

    面向对象语言的重头戏———类 我们知道C++是一门面向对象的语言,与面向过程的语言C不同的是C++的封装,继承和多...

  • JAVA的对象和类

    作为面向对象的语言,支持以下基本概念:多态、继承、封装、抽象、类、对象、实例、方法、重载。其中类和对象是面向对象的...

网友评论

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

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