C++学习笔记——(五)操作符重载和类的继承

注:编码工具是CLion+Cygwin64

目录

操作符重载

+/-

前置++/后置++

<<

[]

类的继承

一般示例

私有和公有继承

多继承

虚继承


操作符重载

        用操作符实现对象之间的运算,简化对象的操作。

+/-

#include <iostream>

using namespace std;

class Point{
public:
    int x, y;
    Point(int x, int y): x(x), y(y){}

    Point operator +(const Point & another){
        return Point(x + another.x, y + another.y);
    }

    Point operator -(const Point & another){
        return Point(x - another.x, y - another.y);
    }
};

int main(){
    Point p1(3, 5), p2(9, 19);
    Point sum = p1 + p2;
    Point diff = p2 - p1;
    cout << "sum.x = " << sum.x << ", sum.y = " << sum.y << endl;
    cout << "diff.x = " << diff.x << ", diff.y = " << diff.y << endl;
    return 0;
}

输出:

sum.x = 12, sum.y = 24
diff.x = 6, diff.y = 14

前置++/后置++

#include <iostream>

using namespace std;

class Point{
public:
    int x, y;
    Point(int x, int y): x(x), y(y){}

    Point operator ++(){
        return Point(x + 1, y + 1);
    }

    Point operator ++(int){
        return Point(x + 1, y + 1);
    }
};

int main(){
    Point p(3, 5);
    Point p2 = p++;
    cout << "p2.x = " << p2.x << ", p2.y = " << p2.y << endl;
    Point p3 = ++p2;
    cout << "p3.x = " << p3.x << ", p3.y = " << p3.y << endl;
    return 0;
}

输出:

p2.x = 4, p2.y = 6
p3.x = 5, p3.y = 7

<<

        可以用cout直接打印对象。

#include <iostream>

using namespace std;

class Point{
public:
    int x, y;
    Point(int x, int y): x(x), y(y){}

    friend void operator <<(ostream & out, const Point & point)
    {
        out << "x = " << point.x << ", y = " << point.y << endl;
    }
};

int main(){
    Point p(3, 5);
    cout << p;
    return 0;
}

输出:

x = 3, y = 5

打印Point对象后,还能继续打印别的数据:

#include <iostream>

using namespace std;

class Point{
public:
    int x, y;
    Point(int x, int y): x(x), y(y){}

    friend ostream & operator <<(ostream & out, const Point & point)
    {
        out << "x = " << point.x << ", y = " << point.y << endl;
        return out;
    }
};

int main(){
    Point p(3, 5);
    cout << p << "继续使用<<输出";
    return 0;
}

输出:

x = 3, y = 5
继续使用<<输出

[]

#include <iostream>

using namespace std;

class Array{
private:
    int size = 0;
    int arr[10];
public:
    int getSize(){
        return size;
    }

    void set(int index, int element)
    {
        arr[index] = element;
        size ++;
    }

    int operator [](int index){
        return arr[index];
    }
};

int main(){
    Array array;
    array.set(0, 1);
    array.set(1, 2);
    array.set(2, 3);
    int i;
    int size = array.getSize();
    cout << "size = " << size << endl;
    for(i = 0; i < size; i ++){
        cout << "第" << i + 1 << "个元素为" << array[i] << endl;
    }
    return 0;
}

输出:

size = 3
第1个元素为1
第2个元素为2
第3个元素为3

类的继承

一般示例

        类中没用访问修饰符修饰的函数和属性都是私有的。

#include <iostream>

using namespace std;

class Base{
public:
    int i = 5;
    void show(){
        cout << "i = " << i << endl;
    }
};

class Sub: Base{
public:
    void test(){
        show();
        i = 99;
        show();
    }
};

int main(){
    Sub sub;
    sub.test();
    return 0;
}

输出:

i = 5
i = 99

私有和公有继承

        继承类时,父类没有用访问修饰符修饰默认是私有继承,这种方式继承,子类可以访问父类的公有属性和函数,但是子类的变量不可以。当父类用public修饰时,子类的对象就可以访问父类的公有属性和函数了。

#include <iostream>

using namespace std;

class Base{
public:
    int i = 5;
    void show(){
        cout << "i = " << i << endl;
    }
};

class Base2{
public:
    int j = 8;
    void show2(){
        cout << "j = " << j << endl;
    }
};

class Sub: Base, public Base2{
public:
    void test(){
        show();
        i = 99;
        show();
    }
};

int main(){
    Sub sub;
    sub.test();
    // sub.i = 100; // 私有继承Base,无法访问i
    // sub.show(); // 私有继承Base,无法访问show
    sub.j = 1000; // 公有继承Base2,可以访问j
    sub.show2(); // 公有继承Base2,可以访问show2
    return 0;
}

输出:

i = 5
i = 99
j = 1000

多继承

        C++允许类继承多个类,这会造成二义性,当两个及以上父类拥有相同的属性或函数,而子类没有覆盖时,编译器无法确定应该访问哪个父类的属性或函数,就会报错。解决的方式是覆盖或者访问时指明父类。

#include <iostream>

using namespace std;

class Base{
public:
    int i = 100;
    void show(){
        cout << "Base show i = " << i << endl;
    }
};

class Base2{
public:
    int i = 99;
    void show(){
        cout << "Base2 show i = " << i << endl;
    }
};

// 覆盖
class Sub: public Base, public Base2{
public:
    int i = 10000;
    void show(){
        cout << "Sub show i = " << i << endl;
    }
};

class Sub2: public Base, public Base2{};

int main(){
    Sub sub;
    sub.show();// 因为子类覆盖了属性和方法,不存在二义性
    Sub2 sub2;
    // sub2.show();// 因为子类没覆盖属性和方法,存在二义性,不能直接访问
    // 指明父类
    sub2.Base::show();
    sub2.Base2::show();
    return 0;
}

输出:

Sub show i = 10000
Base show i = 100
Base2 show i = 99

虚继承

        同时有两个或以上的父类继承了祖父类,但都没有覆盖祖父类的属性和方法,可以用虚继承的方式使祖父类、父类和子类只有唯一的属性和函数。

#include <iostream>

using namespace std;

class Base{
public:
    int num = 9999;
    void show(){
        cout << "num = " << num << endl;
    }
};

class SubBase: virtual public Base{};
class SubBase2: virtual public Base{};

class Sub : public SubBase, public SubBase2{};

int main(){
    Base base;
    SubBase subBase;
    SubBase2 subBase2;
    Sub sub;

    base.show();
    subBase.show();
    subBase2.show();
    sub.show();

    base.num = 10;
    subBase.num = 100;
    subBase2.num = 1000;
    sub.num = 10000;

    base.show();
    subBase.show();
    subBase2.show();
    sub.show();
    return 0;
}

输出:

num = 9999
num = 9999
num = 9999
num = 9999
num = 10
num = 100
num = 1000
num = 10000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值