3 Introduction to Classes, Objects, Member Functions and Strings

目录:

1、created your own classes and member functions

2、create UML class diagrams that model the member functions, attributes and constructors of classes

Account.h


#ifndef UNTITLED1_ACCOUNT_H
#define UNTITLED1_ACCOUNT_H

#include <string>
#include <utility>

class Account{
public:
    explicit Account(std::string accountName, int initialBalance): name{std::move(accountName)}{
        if (initialBalance > 0) {
            balance = initialBalance;
        }
    }

    void deposit(int depositAmount) {
        if (depositAmount > 0) {
            balance = balance + depositAmount;
        }
    }

    int getBalance() const {
        return balance;
    }

    void setName(std::string accountName){
        name = std::move(accountName);
    }
    std::string getName() const {
        return name;
    }

private:
    std::string name;
    int balance{0};
};


#endif //UNTITLED1_ACCOUNT_H

main.cpp

#include <iostream>
#include "Account.h"

using namespace std;

int main() {
    Account account1{"Jane Green", 50};
    Account account2{"John Blue", -7};

    cout << "account1: " << account1.getName() << " balance is $" << account1.getBalance() << endl;
    cout << "account2: " << account2.getName() << " balance is $" << account2.getBalance() << endl;

    cout << "Enter deposit amount for account1: ";
    int depositAmount;
    cin >> depositAmount;
    cout << "adding " << depositAmount << " to account1 balance" << endl;
    account1.deposit(depositAmount);
    cout << "account1: " << account1.getName() << " balance is $" << account1.getBalance() << endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值