c++ primer plus 第五版第10章编程练习题1

文件层级:

 

指针版:

头文件q1.h

#ifndef Q1_H
#define Q1_H

#include <cstring>
#include <iostream>
using namespace std;
class BankAccount
{
    private:
    std::string name;
    std::string accountnum;
    double balance;

    public:
    BankAccount();
    BankAccount(const string* client, const string* num, double bal = 0.0);
    void show() const;
    bool deposit(double cash);
    bool withdraw(double cash);
    ~BankAccount();
};
#endif

实现文件q1.cpp

#include "q1.h"

BankAccount::BankAccount(){};

BankAccount::BankAccount(const string* client, const string* num, double bal)
{
    name = *client; //这个不是指针了,而是解引用
    accountnum = *num;
    balance = bal;
}

BankAccount::~BankAccount()
{
    cout << "Bye~~" << endl;
}

void BankAccount::show()const
{
    cout << "Your current money is: " << balance << endl;
}

bool BankAccount::withdraw(double bal)
{
    if((balance - bal) >= 0)
    {
        balance -= bal;
        cout << "Withdraw: " << bal << "successfully" << endl;
        return true;
    }
    else
    {
        cout << "Not enough money!" << endl;
        return false;
    }
}

bool BankAccount::deposit(double bal)
{
    balance += bal;
    cout << "deposit: " << bal << "successfully" << endl;
    return true;
}

主文件q1_main.cpp

#include "q1.h"


int main(void){
    string name = "Frank";
    string accountNum = "1234567890";
    double money =5000.0;
    string* pName = &name;
    string* pAccountNum = &accountNum;
    BankAccount account1(pName, pAccountNum, money);
    account1.show();
    account1.withdraw(2000.0);
    account1.show();
    account1.deposit(3000);
    account1.show();
    account1.withdraw(1000000);
    account1.show();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(Q1)
include_directories("../")
add_executable(q1_main q1_main.cpp)
add_library(q1_h SHARED q1.cpp)
target_link_libraries(q1_main q1_h)

引用版:

头文件:

#ifndef Q1_H
#define Q1_H

#include <cstring>
#include <iostream>
using namespace std;
class BankAccount
{
    private:
    std::string name;
    std::string accountnum;
    double balance;

    public:
    BankAccount();
    BankAccount(const string& client, const string& num, double bal = 0.0);
    void show() const;
    bool deposit(double cash);
    bool withdraw(double cash);
    ~BankAccount();
};
#endif

实现文件:

#include "q1.h"

BankAccount::BankAccount(){};

BankAccount::BankAccount(const string& client, const string& num, double bal)
{
    name = client;
    accountnum = num;
    balance = bal;
}

BankAccount::~BankAccount()
{
    cout << "Bye~~" << endl;
}

void BankAccount::show()const
{
    cout << "Your current money is: " << balance << endl;
}

bool BankAccount::withdraw(double bal)
{
    if((balance - bal) >= 0)
    {
        balance -= bal;
        cout << "Withdraw: " << bal << "successfully" << endl;
        return true;
    }
    else
    {
        cout << "Not enough money!" << endl;
        return false;
    }
}

bool BankAccount::deposit(double bal)
{
    balance += bal;
    cout << "deposit: " << bal << "successfully" << endl;
    return true;
}

主文件:

#include "q1.h"


int main(void){
    string name = "Frank";
    string accountNum = "1234567890";
    double money =5000.0;
    BankAccount account1(name, accountNum, money);
    account1.show();
    account1.withdraw(2000.0);
    account1.show();
    account1.deposit(3000);
    account1.show();
    account1.withdraw(1000000);
    account1.show();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值