구조체와 클래스

struct vs class

구조체

struct 구조체 이름
{
    코드
};

실습 예제

ch07ex1_account_struct.cpp

은행계좌(account) 구조체를 생성하고, 계좌를 생성한다. 생성된 계좌에 입금, 출금을 해본다.

account 계좌

속성 이름: name 번호: id 잔액: balance

기능 deposit(account, money) - 입금 withdraw(account, money) - 출금 displayAccount(account) - 출력

#include <iostream>

using namespace std;

struct account
{
	char name[20];
	char id[20];
	int balance;
};

void deposit(account& acc, int amount);
void withdraw(account& acc, int amount);
void displayAccount(const account& acc);

int main()
{
	account ac1 = { "홍길동", "111-222-333333", 10000 };
	displayAccount(ac1);
	deposit(ac1, 3000);
	displayAccount(ac1);
	withdraw(ac1, 1500);
	displayAccount(ac1);

	return 0;
}

void deposit(account& acc, int amount)
{
	acc.balance += amount;
}

void withdraw(account& acc, int amount)
{
	acc.balance -= amount;
}

void displayAccount(const account& acc)
{
	cout << "계좌번호: " << acc.id << endl;
	cout << "이름: " << acc.name << endl;
	cout << "잔액: " << acc.balance << endl;
	cout << endl;
}

클래스

ch07ex2_account_class.cpp

은행계좌(Account) 클래스를 생성하고, 계좌를 생성한다. 생성된 계좌에 입금, 출금을 해본다.

Last updated