C++继承多态函数重载

在 C 语言中并没有继承的概念,因此也不存在继承的重载。C++ 支持继承和重载,下面是一些示例代码:

  1. 继承

继承是面向对象编程中的一种机制,通过继承可以实现代码的重用和扩展。在继承中,子类可以继承父类的成员变量和成员函数,并且可以在子类中重写父类的成员函数。

示例代码:

class Animal {
public:
    virtual void eat() {
        cout << "Animal is eating." << endl;
    }
};

class Dog : public Animal {
public:
    void eat() override {
        cout << "Dog is eating." << endl;
    }
};

在上面的示例代码中,子类 Dog 继承了父类 Animal 的成员函数 eat,并且重写了这个函数。在子类中使用 override 关键字可以让编译器检测到子类是否真正实现了父类中的函数。

class Base {
public:
    int x;
    Base(int x) : x(x) {}
};

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

int main() {
    Derived d(1, 2);
    return 0;
}
  1. 多态

多态是面向对象编程中的一个概念,它是指同一个函数或者同一个方法可以在不同的对象中表现出不同的行为。C++ 中的多态有两种形式:虚函数和模板。

示例代码:

class Shape {
public:
    virtual void draw() const = 0;
};

class Circle : public Shape {
public:
    void draw() const override {
        cout << "Circle::draw" << endl;
    }
};

class Square : public Shape {
public:
    void draw() const override {
        cout << "Square::draw" << endl;
    }
};

void render(const Shape& shape) {
    shape.draw();
}

int main() {
    Circle circle;
    Square square;

    render(circle);
    render(square);

    return 0;
}

在上面的示例代码中,Shape 是一个抽象类,它有一个纯虚函数 draw。Circle 和 Square 都是 Shape 的派生类,它们分别实现了 draw 函数。在 render 函数中,我们使用了参数类型为 Shape 引用的形式实现了多态。

  1. 函数重载

函数重载是 C++ 中的一个重要概念,它允许我们定义多个函数,它们在名称相同的情况下可以接受不同数量或类型的参数。

示例代码:

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

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

void print(const char* c) {
    cout << "print string: " << c << endl;
}

int main() {
    print(10);
    print(3.14);
    print("hello world");

    return 0;
}

在上面的示例代码中,我们定义了三个名称相同的函数 print,它们分别接受 int、double 和 const char* 类型的参数,这就是函数重载的体现。

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    int a = 1, b = 2;
    double c = 1.5, d = 2.5;
    int sum1 = add(a, b);
    double sum2 = add(c, d);
    return 0;
}

在上面的代码中,我们对 add 函数进行了重载,分别定义了 int 类型和 double 类型的参数版本。在 main 函数中分别调用了这两个版本的函数,并将结果分别赋值给不同的变量。

  1. 运算符重载

C++ 中允许对运算符进行重载,实现类似于类成员函数的效果。运算符重载可以使得我们使用类对象时更加符合直觉,代码也更加优雅。

示例代码:

class Vector {
public:
    Vector(int x = 0, int y = 0) : m_x(x), m_y(y) {}

    Vector operator+(const Vector& v) {
        return Vector(m_x + v.m_x, m_y + v.m_y);
    }

    friend ostream& operator<<(ostream& os, const Vector& v) {
        os << "(" << v.m_x << ", " << v.m_y << ")";
        return os;
    }

private:
    int m_x;
    int m_y;
};

int main() {
    Vector v1(1, 2);
    Vector v2(3, 4);

    Vector v3 = v1 + v2;
    cout << v3 << endl;

    return 0;
}

在上面的示例代码中,我们定义了类 Vector,并重载了加号运算符和输出运算符。通过重载加号运算符,我们可以直接使用加号来对两个 Vector 对象进行加法运算,而不需要调用类中专门的加法函数。通过重载输出运算符,我们可以在输出 Vector 对象时更加简洁明了。

希望这些代码和解释能够帮助您理解 C++ 中的继承多态、函数重载和运算符重载。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywfwyht

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值