본문 바로가기
C++

[C언어->C++언어 개념] 캡슐화

by cod1ng 2023. 11. 7.

C언어-> C++언어 핵심 개념

OOP(Object-Oriented Concepts)

캡슐화,상속,다형성

오늘 수업 도중 헷갈렸던 내용을 기록한다

C언어 구조체의 캡슐화 대상 : 변수

C++언어 구조체의 캡슐화 대상 : 변수, 함수

 

캡슐화 << 정보의 은닉 

정보 << 데이터 , 기능 

 

기존 C언어 구조체 방식

#include <iostream>
using namespace std;

struct Account
{
int number; //계좌번호
string name; //고객이름
int balance; //잔액
string date; //개설일
};

void MakeAccount(Account *pacc, int number, string name, int money, string date)
{
pacc->number = number;
pacc->name = name;
pacc->balance = money;
pacc->date = date; 
}
void PrintAccount(Account acc)
{
cout << "[계좌번호] " << acc.number << "\t";
cout << "[이름] " << acc.name << "\t";
cout << "[잔액] " << acc.balance << "원\t";
cout << "[개설일] " << acc.date << endl;
}
void InputAccount(Account* pacc, int money)
{
pacc->balance = pacc->balance + money;
}
void OutputAccount(Account* pacc, int money)
{
pacc->balance = pacc->balance - money;
}

int main()
{
Account acc;
MakeAccount(&acc, 1111, "홍길동", 1000, "2023-11-06");
PrintAccount(acc);

InputAccount(&acc, 2000);
PrintAccount(acc);

OutputAccount(&acc, 1000);
PrintAccount(acc);

return 0;
}

 

//02_C++언어 구조체.cpp
/*
계좌 생성, 입금, 출금, 정보 확인 기능
*/
#include <iostream>
using namespace std;

struct Account
{
int number; //계좌번호
string name; //고객이름
int balance; //잔액
string date; //개설일

void MakeAccount(int n, string na, int bal, string da)
{
number = n;
name = na;
balance = bal;
date = da;
}
void PrintAccount()
{
cout << "[계좌번호] " << number << "\t";
cout << "[이름] " << name << "\t";
cout << "[잔액] " << balance << "원\t";
cout << "[개설일] " << date << endl;
}
void InputAccount(int money)
{
balance = balance + money;
}
void OutputAccount(int money)
{
balance = balance - money;
}
};


int main()
{
Account acc;
acc.MakeAccount(1111, "홍길동", 1000, "2023-11-06");
acc.PrintAccount();

acc.InputAccount(2000);
acc.PrintAccount();

acc.OutputAccount(1000);
acc.PrintAccount();

return 0;
}

 

기존에는 구조체에 맴버 변수를 선언했다면 맴버 함수까지 포함시킨다 . 함수는 기능을 포함한다 클래스의 전 개념이라고 보면 된다