04 INTRODUTION TO CLASS
2025. 1. 2. 14:31ㆍ학부/객체지향프로그래밍(C++)
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <string>
class Account {
public:
Account(std::string accountName,int initialbalance)
:name{accountName}
{
if (initialbalance >= 0)
{
balance = initialbalance;
}
}
void withdraw(int withdrawAmount)
{
if (balance >= withdrawAmount)
{
balance -= withdrawAmount;
}
else
{
std::cout << "잘못된 금액입니다." << std::endl;
}
}
void deposit(int depositAmount)
{
balance += depositAmount;
}
int getBalance()
{
return balance;
}
void setName(std::string accountName)
{
name = accountName;
}
std::string getName()
{
return name;
}
private:
std::string name;
int balance{ 0 };
};
#endif
#include <iostream>
#include <string>
#include "Account.h"
using namespace std;
int main()
{
Account account1{ "Jane Green",50 };
Account account2{ "Jone Blue",-7 };
cout << "account1의 계좌 이름: " << account1.getName() << "account1의 잔액: " << account1.getBalance() << endl;
cout << "account2의 계좌 이름: " << account2.getName() << "account2의 잔액: " << account2.getBalance() << endl;
int depositAmount;
cout << "account1에 입금할 금액을 입력하시오: ";
cin >> depositAmount;
account1.deposit(depositAmount);
cout << "account1에 입금한 뒤 잔액은: " << account1.getBalance() << "원 입니다." << endl;
cout << "account2에 입금할 금액을 입력하시오: ";
cin >> depositAmount;
account2.deposit(depositAmount);
cout << "account2에 입금한 뒤 잔액은: " << account2.getBalance() << "원 입니다." << endl;
int withdrawAmount;
cout << "account1에서 출금할 금액을 입력하시오: ";
cin >> withdrawAmount;
account1.withdraw(withdrawAmount);
cout << "account1에서 출금한 뒤 잔액은: " << account1.getBalance() << endl;
cout << "account2에서 출금할 금액을 입력하시오: ";
cin >> withdrawAmount;
account2.withdraw(withdrawAmount);
cout << "account2에서 출금한 뒤 잔액은: " << account2.getBalance() << endl;
}
'학부 > 객체지향프로그래밍(C++)' 카테고리의 다른 글
06 ALGORITHM DEVELOPMENT AND CONTROL STATEMENTS (0) | 2025.01.03 |
---|---|
05 ALGORITHM DEVELOPMENT AND CONTROL STATEMENTS (0) | 2025.01.02 |
03 INTRODUCTION TO CLASS (0) | 2025.01.02 |
02 INTRODUCTION TO C++ (0) | 2025.01.02 |
01 INTRODUCTION TO COMPUTERS (0) | 2025.01.02 |