首页 > 后端开发 > C++ > 正文

C++ 函数的艺术:继承与多态,对象设计模式之精髄

WBOY
发布: 2024-09-15 20:06:02
原创
299人浏览过

c++++ 中的继承允许创建新类,从基类继承数据和函数;多态通过虚函数和基类指针实现,允许对象根据类型展示不同行为。实战案例展示了继承和多态在银行账户层次结构中的应用,包括储蓄、支票和信用卡账户,每个账户类型都有特定行为,例如存款、取款、支付账单等。

C++ 函数的艺术:继承与多态,对象设计模式之精髄

C++ 函数的艺术:继承与多态,对象设计模式之精髓

继承

继承是 C++ 中一种创建新类的方式,新类继承了基类的数据成员和成员函数。语法如下:

立即学习C++免费学习笔记(深入)”;

class DerivedClass : public BaseClass
{
    // Derived class members
};
登录后复制

多态

多态是对象设计模式的核心,允许对象根据其类型表现出不同的行为。虚函数和基类指针是实现多态的关键。语法如下:

class BaseClass {
public:
    virtual void doSomething() = 0; // 抽象函数
};

class DerivedClass : public BaseClass {
public:
    void doSomething() override {
        // 覆盖基类函数
    }
};
登录后复制

实战案例:多态银行账户

创建一个银行账户层次结构,实现储蓄账户、支票账户和信用卡账户。

#include <iostream>

class BankAccount {
protected:
    double balance;
public:
    virtual double getBalance() const { return balance; }
};

class SavingsAccount : public BankAccount {
public:
    void deposit(double amount) { balance += amount; }
    double withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            return balance;
        }
        return -1; // 提款失败
    }
};

class CheckingAccount : public BankAccount {
public:
    void deposit(double amount) { balance += amount; }
    double withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            return amount;
        }
        return -1; // 提款失败
    }
    bool payBill(double amount) {
        if (amount <= balance) {
            balance -= amount;
            return true;
        }
        return false; // 支付账单失败
    }
};

class CreditCardAccount : public BankAccount {
protected:
    double creditLimit;
public:
    CreditCardAccount(double limit) : creditLimit(limit) {}
    void deposit(double amount) { balance = amount; }
    double withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            return balance;
        } else if (balance + creditLimit >= amount) {
            double overdraft = amount - balance;
            balance = -overdraft;
            return amount;
        }
        return -1; // 提款失败
    }
};

void printBalance(BankAccount& account) {
    std::cout << "Balance: " << account.getBalance() << std::endl;
}

int main() {
    SavingsAccount savings;
    CheckingAccount checking;
    CreditCardAccount credit(1000);

    savings.deposit(500);
    savings.withdraw(200);
    printBalance(savings);

    checking.deposit(1000);
    checking.withdraw(800);
    checking.payBill(300);
    printBalance(checking);

    credit.deposit(200);
    credit.withdraw(500);
    printBalance(credit);

    return 0;
}
登录后复制

在这个示例中,继承和多态被用于创建银行账户层次结构,其中每个账户类型都有不同的行为。调用基类函数 getBalance() 和 printBalance() 可以为所有账户类型一致地获取和打印余额。

以上就是C++ 函数的艺术:继承与多态,对象设计模式之精髄的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号