코드한건데 이자가 추가가안되네요 ㅜㅜ/연산자가 /100한걸 정수단위로 짤라서 그런가하고 더블자료형으로 소수로 이자해봣는데도 안되네요...왜이자가안나올까요
잘못된 이유는 다음과 같습니다. |
○ Virtual 함수 선언 / 사용에 대해서 다시 확인하세요. Virtual은 상속 하는 상위 클래스에 선언합니다.
○ Int형 변수에 10/100을 넣으니 0이 되고, 이 때문에 이자 계산이 0이 됩니다.
수정된 코드는 아래와 같습니다. |
#include#include using namespace std; const int NAME_LEN = 20; void ShowMenu(void);//메뉴출력 void MakeAccount(void);//계좌생성 void InsertMoney(void);//입금 void ExtractMoney(void);//출금 void OutputInfo(void);//계좌정보출력 enum { MAKE = 1, INSERT = 2, EXTRACT = 3, OUTPUT = 4, EXIT = 5 }; class Account { private: char*username; int userid; int usermoney; public: Account(char*name, int id, int money) :userid(id), usermoney(money) { int len = strlen(name) + 1; username = new char[len]; strcpy(username, name); } int getid() const { return userid; } virtual void deposit(int money) { usermoney += (money); } void withdraw(int money) { if (usermoney < money) cout << "잔액부족" << endl; else usermoney -= money; } void showinfo() const { cout << "성명: " << username << endl; cout << "계좌번호: " << userid << endl; cout << "잔액: " << usermoney << endl; } ~Account() { delete[]username; } }; class NormalAccount : public Account { private: float userija; public: NormalAccount(char*name, int id, int money, int ija) : Account(name, id, money), userija(ija) { } void deposit(int money) { Account::deposit(money); Account::deposit(money*(userija / 100)); } }; class HighCreditAccount : public NormalAccount { private: int userplusija; public: HighCreditAccount(char*name, int id, int money, int ija, int plusija) :NormalAccount(name, id, money, ija) {} void deposit(int money) { NormalAccount::deposit(money); Account::deposit(money*(userplusija / 100)); } }; class Handler { private: Account*Arr[100]; int accnum; public: Handler() : accnum(0) {} void showMenu(void) { cout << "---------Menu----------" << endl; cout << "1.계좌개설" << endl; cout << "2.입 금" << endl; cout << "3.출금" << endl; cout << "4.계좌정보 전체 출력" << endl; cout << "5.프로그램 종료" << endl; } void MakeAccount() { int choice; cout << '[' << "계좌종류선택" << ']' << endl; cout << "1.보통계좌선택 2.신용계좌선택" << endl; cout << "입력:"; cin >> choice; if (choice == 1) MakeNormalAccount(); else MakeHighCreditAccount(); } void MakeNormalAccount() { int id; char name[NAME_LEN]; int money; int ija; cout << "[보통계좌개설]" << endl; cout << "이름을 입력하시오: "; cin >> name; cout << "계좌번호를 입력하시오: "; cin >> id; cout << "입금액을 입력하시오: "; cin >> money; cout << "기본 이자율:"; cin >> ija; cout << endl; Arr[accnum++] = new NormalAccount(name, id, money, ija); } void MakeHighCreditAccount() { int id; char name[NAME_LEN]; int money; int ija; int choice; int plusija; cout << "[보통계좌개설]" << endl; cout << "이름을 입력하시오: "; cin >> name; cout << "계좌번호를 입력하시오: "; cin >> id; cout << "입금액을 입력하시오: "; cin >> money; cout << "기본 이자율:"; cin >> ija; cout << "신용등급[A(1),B(2),C(3)]"; cin >> choice; cout << endl; switch (choice) { case 1: plusija = 107; Arr[accnum++] = new HighCreditAccount(name, id, money, ija, plusija); break; case 2: plusija = 104; Arr[accnum++] = new HighCreditAccount(name, id, money, ija, plusija); break; case 3: plusija = 102; Arr[accnum++] = new HighCreditAccount(name, id, money, ija, plusija); break; default: cout << "잘못입력하셨습니다" << endl; } } void InsertMoney(void) { int id; int money; cout << "[입금]" << endl; cout << "계좌번호: "; cin >> id; cout << "입금액 : "; cin >> money; cout << endl; for (int i = 0; i < accnum; i++) { if (Arr[i]->getid() == id) { Arr[i]->deposit(money); cout << "입금완료" << endl; return; } else cout << "잘못입력" << endl; } } void ExtractMoney(void) { int id; int money; cout << "[출금]" << endl; cout << "계좌번호: "; cin >> id; cout << "출금액: "; cin >> money; for (int i = 0; i < accnum; i++) { if (Arr[i]->getid() == id) { Arr[i]->withdraw(money); cout << "출금완료" << endl; return; } else cout << "잘못입력" << endl; } } void OutputInfo(void) { int id; cout << "[계좌정보출력]" << endl; cout << "계좌번호입력 : "; cin >> id; for (int i = 0; i < accnum; i++) { if (Arr[i]->getid() == id) { Arr[i]->showinfo(); } } } }; int main(void) { Handler bank; int choice; while (1) { bank.showMenu(); cout << "선택: "; cin >> choice; switch (choice) { case MAKE: bank.MakeAccount(); break; case INSERT: bank.InsertMoney(); break; case EXTRACT: bank.ExtractMoney(); break; case OUTPUT: bank.OutputInfo(); break; case EXIT: return 0; default: cout << "잘못입력하셨습니다. "; } } return 0; };