C++:04.操作符重载和类的继承

知识点:

1. 操作符重载

一般来讲我们定义在类的里面

格式:返回值 operate需要重载的操作符(参数){} 例如:void operator++(){}

1.1 加减操作符

class Vector {
public:
    Vector(int x, int y) {
        this->x = x;
        this->y = y;
    }

    Vector(const Vector &vector) {
        this->x = vector.x;
        this->y = vector.y;
        cout << "拷贝构造函数" << endl;
    }

private:
    int x;
    int y;

public:
    void setX(int x) {
        this->x = x;
    }

    void setY(int y) {
        this->y = y;
    }

    int getX() {
        return this->x;
    }

    int getY() {
        return this->y;
    }

    // 重载减号运算符
    // 为什么要用引用,为了防止重新创建对象
    // const 关键常量,为了防止去修改值
    Vector operator-(const Vector &vector) {
        int x = this->x - vector.x;
        int y = this->y - vector.y;
        Vector res(x, y);
        return res; // 不建议返回引用
    }

    Vector operator + (const Vector &vector) {
        int x = this->x + vector.x;
        int y = this->y + vector.y;
        Vector vector(x,y);
        return vector;
    }
};

int main() {
    Vector vector1(2, 3);
    Vector vector2(2, 3);

    // java 中 string + string;

    // char* str = "123" + "456";

    // 重载运算符 -
    Vector vector = vector1 - vector2;
    cout << vector.getX() << " , " << vector.getY() << endl;

    getchar();
}

打印结果

0,0

1.2 自增减运算符

class Vector {
public:
    Vector(int x, int y) {
        this->x = x;
        this->y = y;
    }

    Vector(const Vector &vector) {
        this->x = vector.x;
        this->y = vector.y;
        cout << "拷贝构造函数" << endl;
    }

private:
    int x;
    int y;

public:
    void setX(int x) {
        this->x = x;
    }

    void setY(int y) {
        this->y = y;
    }

    int getX() {
        return this->x;
    }

    int getY() {
        return this->y;
    }

    // 自增运算符
    void operator++() { // ++X
        this->x = ++this->x;
        this->y = ++this->y;
    }

    void operator++(int) { // X++
        this->x = this->x++;
        this->y = this->y++;
    }
};

int main() {

    Vector vector(1, 2);
    // vector++;
    ++vector;

     cout << vector.getX() << " , " << vector.getY() << endl;

    getchar();
}

打印结果:

2 , 3

1.3 输出运算符

定义:

class Vector {
public:
    Vector(int x, int y) {
        this->x = x;
        this->y = y;
    }

    Vector(const Vector &vector) {
        this->x = vector.x;
        this->y = vector.y;
        cout << "拷贝构造函数" << endl;
    }

private:
    int x;
    int y;

public:
    void setX(int x) {
        this->x = x;
    }

    void setY(int y) {
        this->y = y;
    }

    int getX() {
        return this->x;
    }

    int getY() {
        return this->y;
    }

    // 输出运算符
    friend ostream &operator<<(ostream &_Ostr, const Vector vector) {
        _Ostr << vector.x << " , " << vector.y << endl;
        return _Ostr;
    }
};

int main() {

    Vector vector(1, 2);
    
    // 这么使用
    cout << vector << vector;

    getchar();
}

打印结果:

    拷贝构造函数
    拷贝构造函数
    1 , 2
    1 , 2

1.4 条件运算符

    // 条件运算符
    bool operator==(const Vector &vector) {
        return (this->x == vector.x && this->y == vector.y);
    }

1.5 括号运算符

    // 操作符[]
    int operator[](int index) {
        return this->array[index];
    }

2. 类继承

2.1 变量修饰符和继承修饰符

变量修饰符:

  1. private :本类中使用
  2. protected :子类中能使用(默认)
  3. public :公开,任何地方都可以

类继承修饰符:

  1. private :本类中使用
  2. protected :子类中能使用(默认)
  3. public :公开,任何地方都可以
// 加了 public 才可以访问 Person 中的公有属性和方法
class Student : public Person {
    ...
};

2.2 初始化父类属性

不光可以给父类初始化属性,还可以给本类的属性进行初始化,用 , 隔开即可

 Student(char *name, int age, char *course) : Person(name, age), course(course) {
        cout << "Student 构造函数" << endl;
    }

2.3 初始化属性和函数重载

class Person {
    //变量修饰符
private:// 本类中使用
    // protected: 子类中能使用(默认)
    // public:公开,任何地方都可以
    char *name;
    int age;
public:
    Person(char *name, int age) {
        this->name = name;
        this->age = age;
        cout << "Person 构造函数" << endl;
    }

public:
    void print() {
        cout << this->name << " , " << this->age << endl;
    }
};
class Student : public Person {
private:
    char *course;
public:
    // :Person(name,age) 调用构造函数初始化父类的属性
    // 不光可以给父类初始化属性,还可以给本类的属性进行初始化,用 , 隔开即可
    Student(char *name, int age, char *course) : Person(name, age), course(course) {
        cout << "Student 构造函数" << endl;
    }

    void print() {
        cout << "course: " << course << endl;
    }
};

int main(){
    Student stu("eastrise",25,"数学");
    stu.print();
}

打印结果:

Person 构造函数
Student 构造函数
course: 数学

2.4 多继承

, 号 隔开

class Student : public Person , public Vector{
    
};

版权声明:本文为q925092273原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。