您的位置:首页 > 房产 > 建筑 > 兰州网站建设推广报价_加盟建筑公司办分公司_设计网站推荐_seo排名优化公司价格

兰州网站建设推广报价_加盟建筑公司办分公司_设计网站推荐_seo排名优化公司价格

2025/5/2 1:41:59 来源:https://blog.csdn.net/qq_35918970/article/details/144123342  浏览:    关键词:兰州网站建设推广报价_加盟建筑公司办分公司_设计网站推荐_seo排名优化公司价格
兰州网站建设推广报价_加盟建筑公司办分公司_设计网站推荐_seo排名优化公司价格

基于MVC(Model-View-Controller)架构的小型应用程序。模拟一个简单的银行账户管理系统,包括账户模型、用户界面和控制器。

Model(模型)

// Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H#include <string>class Account {
private:std::string accountNumber;double balance;public:Account(const std::string& number, double initialBalance) : accountNumber(number), balance(initialBalance) {}void deposit(double amount) {if (amount > 0) {balance += amount;}}bool withdraw(double amount) {if (amount > 0 && balance >= amount) {balance -= amount;return true;}return false;}double getBalance() const {return balance;}const std::string& getAccountNumber() const {return accountNumber;}
};#endif // ACCOUNT_H

View(视图)

// AccountView.h
#ifndef ACCOUNTVIEW_H
#define ACCOUNTVIEW_H#include "Account.h"
#include <iostream>class AccountView {
public:void displayAccountInfo(const Account& account) {std::cout << "Account Number: " << account.getAccountNumber() << std::endl;std::cout << "Balance: $" << account.getBalance() << std::endl;}void displayTransactionResult(bool success) {if (success) {std::cout << "Transaction successful." << std::endl;} else {std::cout << "Transaction failed." << std::endl;}}
};#endif // ACCOUNTVIEW_H

Controller(控制器)

// AccountController.h
#ifndef ACCOUNTCONTROLLER_H
#define ACCOUNTCONTROLLER_H#include "Account.h"
#include "AccountView.h"class AccountController {
private:Account account;AccountView view;public:AccountController(const std::string& number, double initialBalance) : account(number, initialBalance), view() {}void deposit(double amount) {account.deposit(amount);view.displayTransactionResult(true);}void withdraw(double amount) {bool success = account.withdraw(amount);view.displayTransactionResult(success);}void displayAccountInfo() {view.displayAccountInfo(account);}
};#endif // ACCOUNTCONTROLLER_H

Main(主函数)

// main.cpp
#include "AccountController.h"
#include <iostream>int main() {// 创建一个账户AccountController myAccount("123456789", 1000.0);// 显示账户信息myAccount.displayAccountInfo();// 执行存款操作std::cout << "Depositing $500..." << std::endl;myAccount.deposit(500.0);// 执行取款操作std::cout << "Withdrawing $200..." << std::endl;myAccount.withdraw(200.0);// 再次显示账户信息std::cout << "Account info after transactions:" << std::endl;myAccount.displayAccountInfo();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com