QT第25-26节课程笔记 - 继承及分文件

2.10 继承

2.10.1 继承基本概念

继承是面向对象编程(OOP)中的一个核心概念,特别是在C++中。它允许一个类(称为派生类或子类)继承另一个类(称为基类或父类)的属性和方法。继承的主要目的是实现代码重用,以及建立一种类型之间的层次关系。

特点

  1. 代码重用:子类继承了父类的属性和方法,减少了代码的重复编写。
  2. 扩展性:子类可以扩展父类的功能,添加新的属性和方法,或者重写(覆盖)现有的方法。
  3. 多态性:通过继承和虚函数,C++支持多态,允许在运行时决定调用哪个函数。

基本用法

在C++中,继承可以是公有(public)、保护(protected)或私有(private)的,这决定了基类成员在派生类中的访问权限。

#include <iostream>
using namespace std;

//基类,父类
class Vehicle{ //交通工具,车,抽象的概念
public:
    string type;
    string contry;
    string color;
    double price;
    int numOfWheel;

    void run(){
        cout << "车跑起来了" << endl;
    }
    void stop();
};

//派生类,子类
class Bickle : public Vehicle{

};

//派生类,子类
class Roadster : public Vehicle{ //跑车,也是抽象,比父类感觉上范围缩小了点
public:
    int stateOfTop;

    void openTopped();
    void pdrifting();
};

int main()
{
    Roadster ftype;
    ftype.type = "捷豹Ftype";
    ftype.run();
    Bickle bike;
    bike.type = "死飞";
    bike.run();
    return 0;
}

在这个例子中,Vehicle 类公有地继承自 Vehicle 类,这意味着所有 Vehicle 类的公有成员在 Vehicle 类中也是公有的。

让我们用一个简单而有趣的案例来说明继承的概念:动物园中的动物。

想象我们正在创o在这个程序中,我们有一个基类 Animal,它定义了所有动物共有的特性和行为。然后,我们可以创建几个派生类,如 LionElephantBird,这些类继承自 Animal 类,并添加或修改特定于它们自己的特性和行为。

基类:Animal

#include <iostream>
#include <string>

class Animal {
protected:
    std::string name;
    int age;

public:
    Animal(std::string n, int a) : name(n), age(a) {}

    virtual void makeSound() {
        std::cout << name << " makes a sound." << std::endl;
    }

    virtual void display() {
        std::cout << "Animal: " << name << ", Age: " << age << std::endl;
    }
};

派生类:Lion

class Lion : public Animal {
public:
    Lion(std::string n, int a) : Animal(n, a) {}

    void makeSound() override {
        std::cout << name << " roars." << std::endl;
    }

    void display() override {
        std::cout << "Lion: " << name << ", Age: " << age << std::endl;
    }
};

派生类:Elephant

class Elephant : public Animal {
public:
    Elephant(std::string n, int a) : Animal(n, a) {}

    void makeSound() override {
        std::cout << name << " trumpets." << std::endl;
    }

    void display() override {
        std::cout << "Elephant: " << name << ", Age: " << age << std::endl;
    }
};

派生类:Bird

class Bird : public Animal {
public:
    Bird(std::string n, int a) : Animal(n, a) {}

    void makeSound() override {
        std::cout << name << " sings." << std::endl;
    }

    void display() override {
        std::cout << "Bird: " << name << ", Age: " << age << std::endl;
    }
};

使用这些类

int main() {
    Lion lion("Leo", 5);
    Elephant elephant("Ella", 10);
    Bird bird("Bella", 2);

    lion.display();
    lion.makeSound();

    elephant.display();
    elephant.makeSound();

    bird.display();
    bird.makeSound();

    return 0;
}

在这个例子中:

  • Animal 是基类,定义了所有动物共有的属性(如 nameage)和方法(如 makeSounddisplay)。
  • LionElephantBird 是派生类,它们继承了 Animal 的特性,并根据自身的特性重写了 makeSounddisplay 方法。
  • main 函数中,创建了各种动物的实例,并展示了它们的行为。

这个例子展示了继承如何使代码更有组织、更易于管理,并且如何通过重写基类方法来实现多态性。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

上官可编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值