MapleStory Finger Point

πŸ”΅ C,C++,C#/C++ (MFC) κ°œλ…μ •λ¦¬

C++ 정리 #μ˜ˆμ™Έμ²˜λ¦¬ ~

HYEJU01 2024. 4. 8. 02:11

 

 

1. μ˜ˆμ™Έ μ²˜λ¦¬κ°€ 무엇인가.

μ˜ˆμ™Έ (exception) : 잘λͺ»λœ μ½”λ“œ , λΆ€μ •ν™•ν•œ 데이터 , μ˜ˆμ™Έμ μΈ 상황에 μ˜ν•˜μ—¬ λ°œμƒν•˜λŠ” 였λ₯˜ (예) 0으둜 λ‚˜λˆ„λŠ” 것과 같은 잘λͺ»λœ μ—°μ‚°μ΄λ‚˜ λ°°μ—΄μ˜ μΈλ±μŠ€κ°€ ν•œκ³„λ₯Ό λ„˜μ„ 수 도 있고 , λ””μŠ€ν¬μ—μ„œλŠ” ν•˜λ“œμ›¨μ–΄ μ—λŸ¬κ°€ 발

2. μ˜ˆμ™Έμ²˜λ¦¬ 방법

  1. μ˜ˆμ™ΈλΆ€λΆ„μ„ ν•˜λ‚˜ν•˜λ‚˜ 쑰건 μ²˜λ¦¬ν•¨

3. μ˜ˆμ™Έμ²˜λ¦¬ νƒ€μž…

try-catch 블둝

try : μ˜ˆμ™Έκ°€ λ°œμƒν•  수 μžˆλŠ” μœ„ν—˜ν•œ μ½”λ“œ

catch : μ˜ˆμ™Έλ₯Ό μ²˜λ¦¬ν•˜λŠ” μ½”λ“œ

throw: μ˜ˆμ™Έ 상황이 λ°œμƒν•­λ ΈμŒμ„ μ•Œλ¦΄ λ•Œ μ‚¬μš©ν•¨

Copy
//μ˜ˆμ™Έμ²˜λ¦¬κΈ° λ§€κ°œλ³€μˆ˜ λͺ¨λ“ νƒ€μž… κ°€λŠ₯함
throw persons;

catch (...) 
{
}

catch(TooSmallException e) {
//TooSmallException만 μž‘νžŒλ‹€.
}

//μ˜ˆμ™Έμ²˜λ¦¬κΈ° λ§€κ°œλ³€μˆ˜ νƒ€μž…μΌμΉ˜ν•΄μ•Όλ¨ !!

throw persons;

catch (int e) 
{
}


catch(...) {
//TooSmallException을 μ œμ™Έν•œ λ‚˜λ¨Έμ§€ μ˜ˆμ™Έλ“€μ΄ μž‘νžŒλ‹€.
}
Copy
int main()
{
int pizza_slices = 0;
int persons = -1;
int slices_per_person = 0;
try
{
		cout << "ν”Όμž 쑰각수λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
		cin >> pizza_slices;
		cout << "μ‚¬λžŒμˆ˜λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
		cin >> persons; //μ˜ˆμ™Έμ²˜λ¦¬κΈ° λ§€κ°œλ³€μˆ˜ νƒ€μž…μΌμΉ˜ν•΄μ•Όλ¨
		if (persons == 0)
			throw persons;
		slices_per_person = pizza_slices / persons;
		cout << "ν•œμ‚¬λžŒλ‹Ή ν”ΌμžλŠ” " << slices_per_person << "μž…λ‹ˆ
		λ‹€." << endl;
}
catch (int e) //μ˜ˆμ™Έμ²˜λ¦¬κΈ° λ§€κ°œλ³€μˆ˜ νƒ€μž…μΌμΉ˜ν•΄μ•Όλ¨
{
		cout << "!!μ‚¬λžŒμ΄ " << e << " λͺ… μž…λ‹ˆλ‹€. " << endl;
		}

return 0;
}
Copy
//μ˜ˆμ™Έμ²˜λ¦¬κΈ° ν•¨μˆ˜λ‘œ λ§€κ°œλ³€μˆ˜λ₯Ό λ°›λŠ” 
#include <iostream>
using namespace std;
int dividePizza(int pizza_slices, int persons);
int main()
{
int pizza_slices = 0;
int persons = 0;
int slices_per_person = 0;

try
{
		cout << "ν”Όμž 쑰각수λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
		cin >> pizza_slices;
		cout << "μ‚¬λžŒμˆ˜λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
		cin >> persons;
		slices_per_person = dividePizza(pizza_slices, persons);
		cout << "ν•œμ‚¬λžŒλ‹Ή ν”ΌμžλŠ” " << slices_per_person << "μž…λ‹ˆ
		λ‹€." << endl;
}
int dividePizza(int pizza_slices, int persons)
{
if (persons == 0)
	throw persons;
return pizza_slices / persons ;}

catch (int e)
{
	cout << "!! μ‚¬λžŒμ΄ " << e << " λͺ… μž…λ‹ˆλ‹€. " << endl;
}
return 0;
}



Copy
//닀쀑 catch λ¬Έ
try {
	cout << "ν”Όμž 쑰각수λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
	cin >> pizza_slices;
	cout << "μ‚¬λžŒμˆ˜λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
	cin >> persons;
	if (persons < 0) throw "negative"; // μ˜ˆμ™Έ λ°œμƒ!
	if (persons == 0) throw persons; // μ˜ˆμ™Έ λ°œμƒ!
	slices_per_person = pizza_slices / persons;
	cout << "ν•œμ‚¬λžŒλ‹Ή ν”ΌμžλŠ” " << slices_per_person << "μž…λ‹ˆλ‹€." << endl;
}
catch (const char *e) {
	cout << "였λ₯˜: μ‚¬λžŒμˆ˜κ°€ " << e << "μž…λ‹ˆλ‹€" << endl;
}
catch (int e) {
	cout << "였λ₯˜: μ‚¬λžŒμ΄ " << e << " λͺ…μž…λ‹ˆλ‹€." << endl;
}


4. ν…œν”Œλ¦Ώ, ν΄λž˜μŠ€ν…œν”Œλ¦Ώ (λ§Œλ“€λΌκ³  λ‚˜μ˜¬μˆ˜λ„μžˆμŒ)

 

❀️
1.λ‹€μ–‘ν•œ νƒ€μž…μ˜ 객체에 λŒ€ν•΄ ν•˜λ‚˜μ˜ ν˜•ν‹€μ„ λ§Œλ“€μ–΄μ„œ μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•˜λŠ” κ²ƒμž„ 2. 클래슀, ν•¨μˆ˜ ~λ“±λ“± 생성 μ „κΉŒμ§„ μ½”λ“œ 생성이 μ•ˆλ¨ (μ½”λ“œ 증가 μ‹œν‚€μ§€ μ•ŠμŒ) 3. STL μ΄λΌλŠ” ν…œν”Œλ¦Ώ 기반의 λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ œκ³΅λΌμ„œ μš°μˆ˜ν•¨ ⇒ ν•¨μˆ˜ν…œν”Œλ¦Ώλ§ŒμžˆμœΌλ©΄ μ›ν•˜λŠ” ν˜• 생성가λŠ₯ (ν•¨μˆ˜μ˜€λ²„λ‘œλ”©) ⇒

 

Copy
//ν•¨μˆ˜ ν…œν”Œλ¦Ώ
template <typename νƒ€μž…μ΄λ¦„>
ν•¨μˆ˜μ˜ μ›ν˜•
{
	....// ν•¨μˆ˜μ˜ 본체
}


//클래슀 ν…œν”Œλ¦Ώ
template <typenmae T>
class ν΄λž˜μŠ€μ΄λ¦„
{
	...// Tλ₯Ό μ–΄λ””μ„œλ“  μ‚¬μš© κ°€λŠ₯
}
Copy
template <typename T>
T get_sum(T x)
{
	x = x + 10;
	return x;
}
template <typename T>
T get_sum(T x, T y)
{
	if (x > y) return x;
	else return y;
}
int main()
{
	cout << get_sum(3) << endl;
	cout << get_sum(1.2, 3.9) << endl;
	return 0;
}
Copy
#include <iostream>
using namespace std;

template <typename T>
class Box {
T data; // TλŠ” νƒ€μž…(type)을 λ‚˜νƒ€λ‚Έλ‹€.
public:
Box() { }

void set(T value) {
data = value;
}

T get() {
return data;
}
------------------------------------
int main()
{
Box<int> box; // ꡬ체화 
box.set(100);
cout << box.get() << endl; //100

Box<double> box1;
box1.set(3.141592);
cout << box1.get() << endl; //3.14~

return 0;
}

 

 

 
 

5. STL

❀️
μ»¨ν…Œμ΄λ„ˆ + 반볡자 + μ•Œκ³ λ¦¬μ¦˜ STL μ•Œκ³ λ¦¬μ¦˜μ€ 반볡자λ₯Ό ν†΅ν•˜μ—¬ μ»¨ν…Œμ΄λ„ˆμ— μ ‘κ·Όν•˜μ—¬ μž‘μ—…μ„ ν•œλ‹€. β—Ό STL은 ν”„λ‘œκ·Έλž˜λ°μ— 맀우 μœ μš©ν•œ μˆ˜λ§Žμ€ μ»¨ν…Œμ΄λ„ˆμ™€ μ•Œκ³ λ¦¬μ¦˜μ„ 제곡 β—Ό STL은 객체 지ν–₯ 기법과 μΌλ°˜ν™” ν”„λ‘œκ·Έλž˜λ° 기법을 μ μš©ν•˜μ—¬μ„œ λ§Œλ“€μ–΄μ‘ŒμœΌλ―€λ‘œ μ–΄λ–€ μžλ£Œν˜•μ— λŒ€ν•΄μ„œλ„ μ‚¬μš©κ°€λŠ₯ β—Ό STL은 μ „λ¬Έκ°€κ°€ λ§Œλ“€μ–΄μ„œ ν…ŒμŠ€νŠΈλ₯Ό 거친 κ²€μ¦λœ 라이브러리

 

 

1. μ•Œκ³ λ¦¬μ¦˜

 

find() 와 find_if()

count()

 

#include<algorithm>
int main()
{
vector<string> vec { "사과", "ν† λ§ˆν† ", "λ°°", "μˆ˜λ°•", "ν‚€μœ„" };
auto it = find(vec.begin(), vec.end(), "μˆ˜λ°•");
if (it != vec.end())
cout << "μˆ˜λ°•μ΄ " << distance(vec.begin(), it) << "에 있
μŠ΅λ‹ˆλ‹€." << endl;
return 0;
}
#include<algorithm>
template <typename T>
bool is_even(const T& num)
{
return (num % 2) == 0;
}
int main()
{
vector<int> vec;
for (int i = 0; i<10; i++)
vec.push_back(i);
size_t n = count_if(vec.begin(), vec.end(), is_even<int>);
cout << "값이 짝수인 μš”μ†Œμ˜ 개수: " << n << endl;
return 0;

binary_search()

for_each()

bool comp(string s1, string s2) {
return (s1 == s2);
}
int main(void) {
vector<string> v = { "one", "two", "three" };
bool result;
result = binary_search(v.begin(), v.end(), "two", comp);
if (result == true)
cout << "λ¬Έμžμ—΄ \"two\" 은 벑터 μ•ˆμ— 있음." << endl;
return 0;
}
void printEven(int n) {
if (n % 2 == 0)
cout << n << ' ';
}
int main(void) {
vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for_each(v.begin(), v.end(), printEven);
cout << endl;
return 0;
}

2.μ»¨ν…Œμ΄λ„ˆ 자료λ₯Ό μ €μž₯ν•˜λŠ” μ°½κ³  μ’…λ₯˜μ— 따라 μž…μΆœλ ₯ 방법이 달라진닀.

1.순차 μ»¨ν…Œμ΄λ„ˆ = μ‹œν€€μŠ€: β–ͺ 자료λ₯Ό 순차적으둜 μ €μž₯

β—Ό 벑터(vector): 동적 λ°°μ—΄μ²˜λŸΌ λ™μž‘ν•œλ‹€. λ’€μ—μ„œ μžλ£Œλ“€μ΄ μΆ”κ°€λœλ‹€. (동적 λ°°μ—΄ , μŠ€λ§ˆνŠΈλ°°μ—΄)

β—Ό 리슀트(list): 벑터와 μœ μ‚¬ν•˜μ§€λ§Œ μ€‘κ°„μ—μ„œ 자료λ₯Ό μΆ”κ°€ν•˜λŠ” μ—°μ‚°μ΄νš¨μœ¨μ μ΄λ‹€.

Copy
//리슀트 

#include <list>
using namespace std;
int main()
{
list<int> my_list={ 10, 20, 30, 40 };
auto it = my_list.begin();
it++;
it++;
my_list.insert(it, 25);
for (auto& n : my_list)
cout << n << " ";
cout << endl
return 0;
}

 

◼데크(dequ): 벑터와 μœ μ‚¬ν•˜μ§€λ§Œ μ•žμ—μ„œλ„ μžλ£Œλ“€μ΄ 좔가될 수 μžˆλ‹€

Copy
//덱 λ¬Έμžμ—΄ μ—°μ‚°

#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> dq = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
dq.pop_front(); // μ•žμ—μ„œ μ‚­μ œ
dq.push_back(11); // λμ—μ„œ μΆ”κ°€
for (auto& n : dq)
cout << n << " ";
cout << endl;
return 0;
}
Copy
//덱 μ—°μ‚°

#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> dq = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
dq.pop_front(); // μ•žμ—μ„œ μ‚­μ œ
dq.push_back(11); // λμ—μ„œ μΆ”κ°€
for (auto& n : dq)
cout << n << " ";
cout << endl;
return 0;
}

2. μ—°κ΄€ μ»¨ν…Œμ΄λ„ˆ = μ—°κ΄€μ‹œν€€μŠ€: β–ͺ 사전과 같은 ꡬ쑰λ₯Ό μ‚¬μš©ν•˜μ—¬μ„œ 자료λ₯Ό μ €μž₯ β–ͺ μ›μ†Œλ“€μ„ κ²€μƒ‰ν•˜κΈ° μœ„ν•œ ν‚€(key) β–ͺ μžλ£Œλ“€μ€ μ •λ ¬

β—Ό 집합(set): 쀑볡이 μ—†λŠ” μžλ£Œλ“€μ΄ μ •λ ¬λ˜μ–΄μ„œ μ €μž₯λœλ‹€.

Copy
#include <set>
int main()
{
set<int> my_set;
my_set.insert(1);
my_set.insert(2);
my_set.insert(3);
auto pos = my_set.find(2);
if (pos != my_set.end())
cout << "κ°’ " << *pos << "κ°€ λ°œκ²¬λ˜μ—ˆμŒ" << endl;
else
cout << "값이 λ°œκ²¬λ˜μ§€ μ•Šμ•˜μŒ" << endl;
return 0 ;
}

β—Ό 맡(map): ν‚€-κ°’(key-value)의 ν˜•μ‹μœΌλ‘œ μ €μž₯λœλ‹€. ν‚€κ°€ μ œμ‹œλ˜λ©΄ ν•΄λ‹Ήλ˜λŠ” 값을 찾을 수 μžˆλ‹€

킀와 κ°’μœΌλ‘œ μ΄λ£¨μ–΄μ ΈμžˆμŒ (각각의 λ”•μ…”λ„ˆλ¦¬μ™€)

μ˜μ–΄μ‚¬μ „ κ΅¬ν˜„ν•¨, 단어λ₯Ό λ°›μ•„μ„œ λ‹¨μ–΄μ˜ μ„€λͺ…을 좜λ ₯함

Copy
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string word;
map<string, string> dic;
dic["boy"] = "μ†Œλ…„";
dic["school"] = "학ꡐ";
dic["office"] = "직μž₯";
dic["house"] = "집";
dic["morning"] = "μ•„μΉ¨";
dic["evening"] = "저녁";

while (true) {
	cout << "단어λ₯Ό μž…λ ₯ν•˜μ‹œμ˜€: ";
	cin >> word;
	if (word == "quit") break;
	string meaning = dic[word];
	if (meaning != "")
	cout << word << "의 μ˜λ―ΈλŠ” " << meaning << endl;
}
return 0;
}
Copy
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main()
{
map<string, string> myMap;
myMap.insert(make_pair("κΉ€μ² μˆ˜", "010-123-5678"));
myMap.insert(make_pair("홍길동", "010-123-5679"));
myMap["졜자영"] = "010-123-5680"
// λͺ¨λ“  μš”μ†Œ 좜λ ₯
for(auto& it : myMap){
cout << it.first << " :: " << it.second << endl;
}
if (myMap.find("κΉ€μ˜ν¬") == myMap.end())
cout << "단어 'κΉ€μ˜ν¬'λŠ” λ°œκ²¬λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. " << endl;
return 0;
}

3. μ»¨ν…Œμ΄λ„ˆ μ–΄λŒ‘ν„° β–ͺ 순차 μ»¨ν…Œμ΄λ„ˆμ— μ œμ•½μ„ κ°€ν•΄μ„œ 데이터듀이 정해진 λ°©μ‹μœΌλ‘œλ§Œ μž…μΆœλ ₯

β—Ό μŠ€νƒ(stack): λ¨Όμ € μž…λ ₯된 데이터가 λ‚˜μ€‘μ— 좜λ ₯λ˜λŠ” 자료 ꡬ쑰 (μ„ μž…ν›„μΆœ)

Copy
#include <stack>
int main()
{
stack<string> st;
string sayings[3] =
{ "The grass is greener on the other side of the fence",
"Even the greatest make mistakes",
"To see is to believe" };
for (auto& s : sayings)
st.push(s);
while (!st.empty()) {
cout << st.top() << endl;
st.pop();
}
return 0;
}

β—Ό 큐(queue): 데이터가 μž…λ ₯된 μˆœμ„œλŒ€λ‘œ 좜λ ₯λ˜λŠ” 자료 ꡬ쑰 (μ„ μž…μ„ μΆœ) β—Ό μš°μ„  μˆœμœ„ν(priority queue): 큐의 μΌμ’…μœΌλ‘œ 큐의 μš”μ†Œλ“€μ΄ μš°μ„  순 μœ„λ₯Ό 가지고 있고 μš°μ„  μˆœμœ„κ°€ 높은 μš”μ†Œκ°€ λ¨Όμ € 좜λ ₯λ˜λŠ” 자료 ꡬ쑰

Copy
include<queue>
int main()
{
queue<int> qu;
qu.push(100);
qu.push(200);
qu.push(300);
while (!qu.empty()) {
cout << qu.front() << endl;
qu.pop();
}
return 0;
}

 

3.반볡자

μΌλ°˜ν™”λœ 포인터(generalized pointer) / μ•Œκ³ λ¦¬μ¦˜μ€ 반볡자λ₯Ό ν†΅ν•˜μ—¬ μ»¨ν…Œμ΄λ„ˆμ— μ ‘κ·Όν•˜μ—¬ μž‘μ—…μ„ ν•œλ‹€.

μš”μ†Œλ₯Ό 순차적으둜 μ²˜λ¦¬ν•˜κΈ°μœ„ν•œ μ»΄ν¬λ„ŒνŠΈ (for λ¬Έμ—μ„œμ˜ i λŠ” λ°˜λ³΅μžμž„

  • Begin() : 맨 μ²˜μŒλΆ€ν„°
  • end() : 맨 λμ—μ„œλΆ€ν„°
  • ++ μ»¨ν…Œμ΄λ„ˆ λ‹€μŒμš”μ†Œ
  • - - μ΄μ „μš”μ†Œ
  • = , ! = 같은 μš”μ†Œλ₯Ό 가리킀고 μžˆλŠ”μ§€ 확인
  • * λ°˜λ³΅μžκ°€ κ°€λ¦¬ν‚€λŠ” μš”μ†Œκ°’μ„ μΆ”μΆœν•˜κΈ° μœ„ν•œ μ—­μ°Έμ‘° μ—°μ‚°μž

 

Copy
//ν•™μƒλ“€μ˜ 성적을 μ €μž₯ν•˜λŠ” 벑터λ₯Ό μƒμ„±ν•΄μ„œ

#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<double> scores (10);
for (int i=0; i<scores.size(); i++)
{
cout<<"성적을 μž…λ ₯ν•˜μ‹œμ˜€:";
cin >> scores[i];
}

double highest =scores[0];
for(int i = 1; i<scores.size(); i++)
	if (scores[i] > highest)
		 highest = scores[i];
cout<<"졜고 성적은" << highest << "μž…λ‹ˆλ‹€ \n";

return 0;
}
Copy
#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<double> scores;
while (true) {
double value = 0.0;
cout << "성적을 μž…λ ₯ν•΄ μ£Όμ„Έμš” :(-1을 치면 μ’…λ£Œ) ";
cin >> value;
if (value < 0.0) break;
scores.push_back(value);
}
double highest = scores[0];
vector<double>::iterator it;
for(it = scores.begin(); it < scores.end(); it++)
if (*it > highest)
highest = *it;
cout << "졜고의 성적은" << highest << "μž…λ‹ˆλ‹€." << endl;
return 0;
}
Copy
//5보닀 큰 μ •μˆ˜
bool is_greater_than_5(int value)
{
return (value > 5);
}
int main()
{
vector<int> numbers{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto count = count_if(numbers.begin(), numbers.end(),is_greater_than_5);
cout << "5보닀 큰 μ •μˆ˜λ“€μ˜ 개수: " << count << endl;
return 0;
}

 

 

6. λžŒλ‹€μ‹ & auto ν•¨μˆ˜

❀️
이름이 μ—†λŠ” ν•¨μˆ˜κ°œμ²΄, λ‚΄ν¬λ˜λŠ” ν•¨μˆ˜ λžŒλ‹€μ‹μ„ μ‚¬μš©ν•˜λ©΄ μ½”λ“œκ°€ 간결해짐 autoν•¨μˆ˜λŠ” νƒ€μž… μΆ”λ‘  방식

 

Copy
//λžŒλ‹€μ‹
#include <iostream>
using namespace std;
int main()
{
auto sum = [](int x, int y) { return x + y; };
cout << sum(1, 2) << endl;
cout << sum(10, 20) << endl;
return 0;
}

 

7. λ‹€ν˜•μ„±(polymorphism)μ΄λž€

❀️
νƒ€μž…μ΄ λ‹€λ₯΄λ©΄ μ„œλ‘œ λ‹€λ₯Έ λ™μž‘μ„ν•¨ 객체 지ν–₯ κΈ°λ²•μ—μ„œ ν•˜λ‚˜μ˜ μ½”λ“œλ‘œ λ‹€μ–‘ν•œ νƒ€μž…μ˜ 객체λ₯Όμ²˜λ¦¬ν•˜λŠ” μ€‘μš”ν•œ κΈ°μ€€ ⇒ 객체 포인터 (ν˜•λ³€ν™˜) 을 ν†΅ν•΄μ„œ 이루어짐

8. 상ν–₯/ν•˜ν–₯ ν˜•λ³€ν™˜

❀️
μžμ‹ 클래슀 쀑 λΆ€λͺ¨ν΄λž˜μŠ€λ‘œ 상속 받은 λΆ€λΆ„λ§Œ ! λΆ€λͺ¨ν΄λž˜μŠ€ν¬μΈν„°λ‘œ κ°€λ¦¬ν‚¬μˆ˜μžˆμŒ!!!!!! → ν•˜ν–₯ν˜•λ³€ν™˜ν•˜λŠ”μ΄μœ  -상ν–₯ ν˜•λ³€ν™˜(upcasting): 객체포인터가 λΆ€λͺ¨ 가리킴 μžμ‹ 클래슀 νƒ€μž…μ„ λΆ€λͺ¨ ν΄λž˜μŠ€νƒ€μž…μœΌλ‘œ λ³€ν™˜ -ν•˜ν–₯ ν˜•λ³€ν™˜(downcasting): λΆ€λͺ¨ 클래슀 νƒ€μž…μ„ μžμ‹ ν΄λž˜μŠ€νƒ€μž…μœΌλ‘œ λ³€ν™˜ ν¬μΈν„°λŠ” μƒμ†λœ(Derived) 클래슀의 κ°μ²΄λ§Œμ„ 가리킬 수 있음
Copy
#include<iostream>
#include<list>
using namespace std;

class RemoteControl {

public:
    virtual void turnOn() = 0;
    virtual void turnOff() = 0;
};

class Television : public RemoteControl {
public:
    void turnOn() {
        cout << "tv on" << endl;
    }
    void turnOff() {
        cout << "tv off" << endl;
    }
    void volCh() {
        cout << "vol ch" << endl;
    }

};
class Radio : public RemoteControl {
public:
    void turnOn() {
        cout << "tv on" << endl;
    }
    void turnOff() {
        cout << "tv off" << endl;
    }
    void volAMFM() {
        cout << "AMFM ch" << endl;
    }
};
void main() {

    RemoteControl* r1 = new Television(); 
    r1->turnOn();

    RemoteControl* r2 = new Radio();
    r2->turnOn();

    //r1->volCh(); λΆ€λͺ¨μ— μ—†λŠ” 것은 ν•˜ν–₯ν˜•λ³€ν™˜μ„ν•΄μ€˜μ•Όλ¨  ㅇㅇ당연함
    ((Television*)r1)->volCh(); 

    /*void volCh() {
    cout<<"RemoteControl vol ch"<<endl;
    } μ•„λ‹˜ λΆ€λͺ¨ ν΄λž˜μŠ€μ— μΆ”κ°€ γ„± */

    return;
}
Copy
class Shape {
protected:
int x, y;

public:
void setOrigin(int x, int y){
this->x = x;
this->y = y;
}
void draw() { 
cout <<"Shape Draw";
}
};

class Rectangle : public Shape {
private: 
int width, height;
public: 
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
void draw() {
cout << "Rectangle Draw";
}
}


class Circle : public Shape {
private: 
int radius;
public: 
void setRadius(int r) {
radius = r;
}
void draw() {
cout << "Circle Draw"<< endl;
}
};


void main() {

    //Shape* pa = new Shape();//본래 동적 객체생성
    //pa->draw(); 

    Shape* pb = new Rectangle();//상ν–₯ν˜•λ³€ν™˜ λΆ€λͺ¨
    pb->draw();

    ((Rectangle*)pb) -> draw () ;// ν•˜ν–₯ν˜•λ³€ν™˜ μžμ‹

    delete pb;

    return;
}
Copy
class Shape {
...
}
class Rectangle : public Shape {
...
}
int main()
{
Shape *ps = new Rectangle(); // 상ν–₯ ν˜•λ³€ν™˜
//μ—¬κΈ°μ„œ psλ₯Ό ν†΅ν•˜μ—¬ Rectangle의 멀버에 μ ‘κ·Όν•˜λ €λ©΄
ps->setOrigin(10, 10);
ps->draw(); //Shape 포인터이기 λ•Œλ¬Έμ— Shape의 draw()κ°€ 호좜
1. Rectangle *pr = (Rectangle *) ps;
2.((Rectangle *)ps)-> draw(); // ν•˜ν–₯ ν˜•λ³€ν™˜
delete ps;

UML +

클래슀λͺ… / 속성 (νŠΉμ§•,λ³€μˆ˜) / μ—°μ‚°(λ©”μ„œλ“œ)

-private +public #protected

λΆ„μ„λ‹¨κ³„μ˜ UML κ³Ό 섀계 λ‹¨κ³„μ˜ UML이 닀름

 

 

 

 

 

9. virtual κ°€μƒν•¨μˆ˜

❀️
μžμ‹ 클래슀 객체λ₯Ό λΆ€λͺ¨ 클래슀 객체둜 μ·¨κΈ‰ν•˜λ©΄ μ„œλ‘œλ‹€λ₯Έ μ’…λ₯˜λ₯Ό ν˜ΈμΆœν•˜λŠ”λ° μœ μš©ν•¨ (λΆ€λͺ¨λ₯Ό κ°€λ¦¬μΌœλ„ μžμ‹μ„ λ°˜ν™˜ν•¨) μ΄λ•Œ!!! 특히 곡용으둜 μ‚¬μš©λ˜λŠ” ν•¨μˆ˜λ“€ κ°€μƒν•¨μˆ˜λ‘œ μ“°λ©΄ μ’‹μŒ ⇒ psκ°€ μ‹€μ œλ‘œ 가리킀고 μžˆλŠ” 객체 νƒ€μž…μ— 따라 μž¬μ •μ˜λœ ν•¨μˆ˜κ°€ 호좜(μžμ‹)됨 ⇒ λ©€λ²„ν•¨μˆ˜μ—λ§Œ μ‚¬μš© κ°€λŠ₯, λ©€λ²„λ³€μˆ˜μ—λŠ” μ‚¬μš© λΆˆκ°€ ⇒λΆ€λͺ¨ν΄λž˜μŠ€μ—μ„œ virtual이면 μžμ‹ν΄λž˜μŠ€μ—μ„œλŠ” virtual을 μ‚¬μš©ν•˜μ§€ μ•Šλ”λΌλ„ μžλ™μœΌλ‘œ virtual ⇒ λΆ€λͺ¨ν΄λž˜μŠ€(Shape)의 포인터(ps)둜 ν˜ΈμΆœμ„ ν•˜λ”λΌλ„, μžμ‹ν΄λž˜μŠ€(Rectangle)μ˜μž¬μ •μ˜λœ ν•¨μˆ˜ draw()κ°€ 호좜되게 ν•˜λ €λ©΄, λΆ€λͺ¨ν΄λž˜μŠ€μ—μ„œ ν•¨μˆ˜ draw()λ₯Ό κ°€μƒν•¨μˆ˜λ‘œ μ •μ˜ν•˜λ©΄ λœλ‹€

 

Copy
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
int x, y;
public:
void setOrigin(int x, int y){
this->x = x;
this->y = y;
}
virtual void draw() {  //가상 ν•¨μˆ˜ μ •μ˜
cout <<"Shape Draw" << endl;
}
};
class Rectangle : public Shape { //μž¬μ •μ˜
void draw() {
cout << "Rectangle Draw" << endl;
}
int main()
{
Shape *ps = new Rectangle();
ps->draw(); // rectangle draw()
delete ps;
Shape *ps1 = new Circle();
ps1->draw();
delete ps1;
return 0;

//λͺ¨λ“  λ„ν˜•μ„ λΆ€λͺ¨ν΄λž˜μŠ€ ν¬μΈν„°λ‘œ 가리킀고 이 ν¬μΈν„°λ‘œ 
//draw() ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ©΄ μž¬μ •μ˜λœ ν•¨μˆ˜κ°€ 호좜됨 (virtaul)
}
Copy
#include<iostream>
#include<list>
using namespace std;
class Shape {
protected:
    int x, y;
public:
    void setOrigin(int x, int y) {
        this->x = x;
        this->y = y;
    }
    virtual void draw() { // κ°€μƒν•¨μˆ˜ 
        cout << "Shape Draw" << endl;
    }
};

class Rectangle : public Shape {
protected:
    int width, height;
public:
    void setWidth(int w) {
        this->width = w;
    }
    void setHegith(int h) {
        this->height = h;
    }
    void draw() {
        cout << "Rectangle Draw" << endl;
    }
};

class Circle : public Shape {
protected:
    int radius;
public:
    void setRadius(int r) {
        this->radius = r;
    }
    void draw() {
        cout << "Circle Draw" << endl;
    }
};

void main() {

    Shape* pc = new Shape();//객체생성
    pc->draw();
    //Shape draw()

    Shape* pb = new Rectangle();
    pb->draw(); //κ°€μƒν•¨μˆ˜λ‘œ rectangle 의 draw () 

    Shape* pa = new Circle();
    pa->draw(); //κ°€μƒν•¨μˆ˜λ‘œ circle 의 draw () 

    Rectangle* pr = (Rectangle*)pb; //ν•˜ν–₯ν˜•λ³€ν™˜1
    pr->draw(); 
    // rectangle draw()
    ((Rectangle*)pb)->draw();//ν•˜ν–₯ν˜•λ³€ν™˜1  
    // rectangle draw()

/*
Rectangle r1;
Shape &s1 = r1;
s1.draw(); 
*/


    return;
}
Copy
λ‹€ν˜•μ„±μ„ μ‚¬μš©ν•˜λŠ” κ³Όμ •μ—μ„œ μ†Œλ©Έμžλ₯Ό virtual둜 해주지 μ•ŠμœΌλ©΄ λ¬Έμ œκ°€ λ°œμƒν•œλ‹€.
ο‚·  (예제) String 클래슀λ₯Ό μƒμ†λ°›μ•„μ„œ 각 μ€„μ˜ μ•žκ³Ό 뒀에 헀더λ₯Ό λΆ™μ΄λŠ” MyString μ΄λΌλŠ” 클래슀λ₯Ό μ •μ˜ν•˜μ—¬ 보자.
ο‚·  String ν΄λž˜μŠ€λŠ” 내뢀에 λ¬Έμžμ—΄μ„ μ €μž₯ν•˜κΈ° μœ„ν•΄ char 배열을 λ™μ μœΌλ‘œ μƒμ„±ν•œλ‹€.
ο‚·  μ†Œλ©Έμžμ—μ„œλŠ” 이 λ™μ μœΌλ‘œ μƒμ„±λœ 곡간을 λ°˜λ‚©ν•œλ‹€.

10. 바인딩 =κ°€μƒν•¨μˆ˜κ°€ 있으면 동적 λ°”μΈλ”©μž„

❀️
바인딩 : ν•¨μˆ˜ ν˜ΈμΆœμ„ ν•¨μˆ˜μ˜ λͺΈμ²΄μ™€ μ—°κ²°ν•˜λŠ” 것 정적바인딩 : 컴파일 λ‹¨κ³„μ—μ„œ λͺ¨λ“  λ°”μΈλ”©μ΄μ™„λ£Œ λ˜λŠ” 것 동적바인딩 = 지연바인딩 : 바인딩 μ‹€ν–‰μ‹œκΉŒμ§€ μ§€μ—°λ˜κ³  μ‹€μ œ ν˜ΈμΆœλ˜λŠ” ν•¨μˆ˜λ₯Ό κ²°μ • (⇒동적바인딩을 μ‚¬μš©ν•˜λ©΄ λ‹€ν˜•μ„±μ„ κ΅¬ν˜„)
Copy
class Triangle: public Shape {
private: 
int base, height;
public: 
void draw() {
cout << "Triangle Draw" << endl;
}
};
int main() {
	Shape *arrayOfShapes[3];
	arrayOfShapes[0] = new Rectangle();
	arrayOfShapes[1] = new Triangle();
	arrayOfShapes[2] = new Circle();
	for (int i = 0; i < 3; i++) {
	arrayOfShapes[i]->draw();
	}
}

 

Copy
#include <iostream>
using namespace std;
class Animal 
{
public: 
virtual void speak() { cout <<"Animal speak()" << endl; }
};

class Dog : public Animal 
{
public: 
void speak() { cout <<"멍멍" << endl; }
};

class Cat : public Animal 
{
public: 
void speak() { cout <<"μ•Όμ˜Ή" << endl; }
};

int main()
{
Dog d;
Animal &a1 = d;
a1.speak();
Cat c;
Animal &a2 = c;
a2.speak();
return 0;
}

11. κ°€μƒμ†Œλ©Έμž

❀️
상ν–₯ν˜•λ³€ν™˜μ‹œμ— λΆ€λͺ¨ μ†Œλ©Έμžμ— κ°€μƒμ†Œλ©Έμž ν•΄μ€˜μ•Όλ¨
Copy
//λ‹€ν˜•μ„± μ‚¬μš©μ‹œ μ†Œλ©Έμž virtual 둜 μ•ˆν•˜λ©΄
//Parent() μ†Œλ©Έμž λ™μž‘
//Child() μ†Œλ©Έμž λ™μž‘ μ•ˆν•¨


#include <iostream>
using namespace std;

class Parent
{
public:
//~Parent() { cout << "Parent μ†Œλ©Έμž" << endl; }
virtual ~Parent() { cout << "Parent μ†Œλ©Έμž" << endl;}
};

class Child : public Parent
{
public:
~Child() { cout << "Child μ†Œλ©Έμž" << endl; }
};

int main()
{
Parent* p = new Child(); // 상ν–₯ ν˜•λ³€ν™˜
delete p;
}

 

12. 순수 κ°€μƒν•¨μˆ˜ ⇒ μΆ”μƒν΄λž˜μŠ€!!

❀️
ν•¨μˆ˜ ν—€λ”λ§Œ μ‘΄μž¬ν•˜κ³  ν•¨μˆ˜μ˜ λͺΈμ²΄λŠ” μ—†λŠ” ν•¨μˆ˜μž„ γ…‡γ…‡
❀️
μΆ”μƒν΄λž˜μŠ€ : 순수 κ°€μƒν•¨μˆ˜λ₯Ό ν•˜λ‚˜λΌλ„ 가지고 있으면 μΆ”μƒν΄λž˜μŠ€μž„ 좔상적인 κ°œλ…μ„ ν‘œν˜„ν•˜λŠ”λ° 적당함 ⇒ 객체 μ„ μ–Έ μ•ˆλ¨ ⇒ 좔상 클래슀λ₯Ό 상속 λ°›μœΌλ©΄ μž¬μ •μ˜ ν•΄μ€˜μ•Όν•¨
Copy
virtual λ°˜ν™˜ν˜• ν•¨μˆ˜μ΄λ¦„ (λ§€κ°œλ³€μˆ˜ 리슀트)  = 0;

virtual voud draw() = 0;
Copy
class Shape {
protected:
int x, y;
public:
…
virtual void draw() = 0; //순수 κ°€μƒν•¨μˆ˜
};

class Rectangle : public Shape {
private: 
int width, height;
public: 
void draw() {
cout << "Rectangle Draw" << endl;
}
};

int main()
{
Shape *ps = new Rectangle(); // OK!
ps->draw(); // Rectangle의draw()κ°€ν˜ΈμΆœλœλ‹€. 
delete ps;
return 0;
}

 

Copy
class Animal {
virtual void move() = 0;
virtual void eat() = 0;
virtual void speak() = 0;
};
class Lion : public Animal {
void move(){
cout << "μ‚¬μžμ˜ move() << endl;
}
void eat(){
cout << "μ‚¬μžμ˜ eat() << endl;
}
void speak(){
cout << "μ‚¬μžμ˜ speak() << endl;
}
};
Copy
class RemoteControl {
// μˆœμˆ˜κ°€μƒν•¨μˆ˜μ •μ˜
virtual void turnON() = 0; // κ°€μ „μ œν’ˆμ„μΌ λ‹€.
virtual void turnOFF() = 0; // κ°€μ „μ œν’ˆμ„λˆλ‹€.
}

class Television : public RemoteControl {
void turnON() 
{
// μ‹€μ œλ‘œTVμ˜μ „μ›μ„μΌœκΈ°μœ„ν•œμ½”λ“œκ°€λ“€μ–΄κ°„λ‹€.
...
}
void turnOFF()
{
// μ‹€μ œλ‘œTVμ˜μ „μ›μ„λ„κΈ°μœ„ν•œμ½”λ“œκ°€λ“€μ–΄κ°„λ‹€.
...
}
}

int main()
{
RemoteControl* r1 = new Television();
r1->turnOn();
RemoteControl* r2 = new Radio();
r2->turnOn();
((Television*)r1)->volCh();
((Radio*)r2)->volAMFM();
delete r1;
delete r2;
return 0;
}

 

13. 슀트림, μž…μΆœλ ₯(파일 읽고 μ“°κΈ°)

❀️
#슀트림 :: μˆœμ„œκ°€ μžˆλŠ” λ°μ΄ν„°μ˜ 연속적인 흐름 ofstream 읽고 μ“°λŠ” ifstream 읽고 좜λ ₯
Copy
//μž…λ ₯
ifstream is;
is.open("score.txt");
int number;
is >> number;
//좜λ ₯
ofstream os;
os.open("result.txt");
os << number

//score.txt
20100001 홍길동 100
20100002 κΉ€μœ μ‹  90
20100003 강감찬 80
//Result.txt
20100001 홍길동 100
Copy
c > μ‚¬μš©μž > μžμ‹ μ»΄ν“¨ν„°μ΄λ¦„ > source > repos > project

#include<iostream>
#include<fstream>

using namespace std;

int main() {
	int number;
	char name[30];
	int score;

	ifstream is;
	is.open("input.txt");

	if (!is) {
		cerr << " 파일 μ˜€ν”ˆμ— μ‹€νŒ¨ " << endl;
		exit(1);
		// λ¬Έμ œμƒκΈ°λ©΄ μ•Œλ €μ€Œ
	}

	is >> number >> name >> score; //is에 값듀이 듀어감(ν•˜λ‚˜) 파일읽음

	ofstream os; // 
	os.open("result.txt");
	
		os << number << " " << name << " " << score << endl; // μ“°λŠ”λΆ€λΆ„μ— 띄어쓰기해주면됨
		is >> number >> name >> score;

		os << number << " " << name << " " << score << endl;

	is.close();
	os.close();

	return 0;
}


//μ‹€ν–‰μ‹œν‚€λ©΄ result 파일 μƒκΈ°λ©΄μ„œ ν•˜λ‚˜κ°€ λ“€μ–΄κ°€μžˆμŒ
Copy
#include<iostream>
#include<fstream>

using namespace std;

int main() {
	int number;
	char name[30];
	int score;

	ifstream is;
	is.open("input.txt");

	if (!is) {
		cerr << " 파일 μ˜€ν”ˆμ— μ‹€νŒ¨ " << endl;
		exit(1);
		// λ¬Έμ œμƒκΈ°λ©΄ μ•Œλ €μ€Œ
	}



	ofstream os; // 
	os.open("result.txt");
	for (int i = 0; i < 3; i++) { //자기 κ°’ λ²”μœ„ λ„˜μ§€ μ•ŠκΈ°
		is >> number >> name >> score; //is에 값듀이 듀어감(ν•˜λ‚˜) 파일읽음
		os << number << " " << name << " " << score << endl; // μ“°λŠ”λΆ€λΆ„μ— 띄어쓰기해주면됨
	}
	is.close();
	os.close();

	return 0;
}
Copy
//파일 μ“°κΈ°

int main()
{
ofstream os{ "numbers.txt" };
if (!os) {
cerr << "파일 μ˜€ν”ˆμ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€" << endl;
exit(1);
}
for(int i=0;i<100; i++)
os << i <<" ";
return 0;
// 객체 osκ°€ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ ofstream μ†Œλ©Έμžκ°€ νŒŒμΌμ„ λ‹«λŠ”λ‹€.
Copy
int main()
{
ofstream os{ "numbers.txt" };
if (!os) {
cerr << "파일 μ˜€ν”ˆμ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€" << endl;
exit(1);
}
for(int i=0;i<100; i++)
os << i <<" ";
return 0;
// 객체 osκ°€ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ ofstream μ†Œλ©Έμžκ°€ νŒŒμΌμ„ λ‹«λŠ”λ‹€.
}

 

Copy
//파일λͺ¨λ“œ 예제
int main()
{
using namespace std;
ofstream os(“input.txt", ios::app);
if (!os)
{
cerr << "파일 μ˜€ν”ˆμ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€" << endl;
exit(1);
}
os << "μΆ”κ°€λ˜λŠ” 쀄 #1" << endl;
os << "μΆ”κ°€λ˜λŠ” 쀄 #2" << endl;
return 0;
}
Copy
#include<iostream>
#include<fstream>

using namespace std;

int main() {
	
	ofstream os{ "input.txt", ios::app }; //appλŠ” 뒀에 값에 μΆ”κ°€ 
		//μ΄ˆλ‘μƒ‰μ€ μ˜ˆμ•½μ–΄ 
		if (!os) {
			cerr << "νŒŒμΌμ˜€ν”ˆ μ‹€νŒ¨" << endl;
			exit(1);
	}

	os << "μΆ”κ°€ 쀄 #1" << endl;
	os << "μΆ”κ°€ 쀄 #2" << endl;

	os.close();

}
Copy
#include<iostream>
#include<fstream>

using namespace std;

int main() {
	
	ifstream is { "input.txt"}; //appλŠ” 뒀에 값에 μΆ”κ°€ 
		//μ΄ˆλ‘μƒ‰μ€ μ˜ˆμ•½μ–΄ 
		if (!is) {
			cerr << "νŒŒμΌμ˜€ν”ˆ μ‹€νŒ¨" << endl; //cerr μ—λŸ¬μ½”λ“œμž‘μ„λ•Œ
			exit(1);
	}
		int hour;
		double temp;
		while (is >> hour >> temp) {
			cout << hour << "μ‹œ : μ˜¨λ„ :" << temp << endl;
		}
	is.close();

}
Copy
//데이터 처리 예제

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream is{ “input.txt" };
if (!is) { // ! μ—°μ‚°μž μ˜€λ²„λ‘œλ”©
cerr << "파일 μ˜€ν”ˆμ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€" << endl;
exit(1);
}
int hour;
double temperature;
while (is >> hour >> temperature) {
cout << hour << "μ‹œ: μ˜¨λ„ "<< temperature << endl;
}
return 0;
}

//벑터 이용 예제

#include<vector>
class TempData {
public:
int hour;
double temperature;
};
int main()
{
ifstream is{ “input.txt" };
if (!is) { // ! μ—°μ‚°μž μ˜€λ²„λ‘œλ”©
cerr << "파일 μ˜€ν”ˆμ— μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€" << endl;
exit(1);
}
vector<TempData> temps;

int hour;
double temperature;
while (is >> hour >> temperature) {
temps.push_back(TempData{ hour, temperature });
}
for ( TempData t : temps) {
cout << t.hour << "μ‹œ: μ˜¨λ„ " << t.temperature << endl;
}
return 0;
}
Copy
#include<iostream>
#include<list>
#include<iostream>
#include<fstream>

using namespace std;


void main() {


    ifstream is{ "input.txt" };
    ofstream os{ "output.txt" , ios::app}; /

    if (!is) {
        cerr << "νŒŒμΌμ˜€ν”ˆμ‹€νŒ¨" << endl;
        exit(1);
    }

    int hour;
    double temp;

    while (is >> hour >> temp) {
        os << hour << "μ‹œ : μ˜¨λ„ :" << temp << endl;
    }
    is.close();
    os.close();


    //읽고 μ“°κΈ° (input μ—μ„œ 읽고 output μ—μ„œ μ“°κΈ°)

    return;
}
Copy
#include<vector>

int main (){

class TempData{
public:
		int hour;
		double temp;
}
	ifstream is { "input.txt"}; //appλŠ” 뒀에 값에 μΆ”κ°€ 
		//μ΄ˆλ‘μƒ‰μ€ μ˜ˆμ•½μ–΄ 
		if (!is) {
			cerr << "νŒŒμΌμ˜€ν”ˆ μ‹€νŒ¨" << endl; //cerr μ—λŸ¬μ½”λ“œμž‘μ„λ•Œ
			exit(1);
	}
vector< TempData > temps; //클래슀 자체λ₯Ό 집어넣을 수 있음

		int hour;
		double temp;
		while (is >> hour >> temp) {
			temps.push_back(TempData {hour,temp});
		}

for(TempData t:temps) //반볡자
	cout<<t.hour<<"μ‹œ:μ˜¨λ„"<<t.temp<<endl;

	is.close();


return 0;
}



벑터λ₯Ό λ§Œλ“€μ–΄μ„œ 값을 κ°€μ Έμ™€μ„œ 값을 집어 λ„£μŒ
벑터에 λ‹€ μ§‘μ–΄λ„£μŒ μ‹œκ°„ μ˜¨λ„ μ‹œκ°„ μ˜¨λ„ μ‹œκ°„ μ˜¨λ„
ν΄λž˜μŠ€λΌμ„œ & 빠짐 (반볡자)λ₯Ό 톡해 값을 κ°€μ Έμ˜΄
Copy
#include<vector>

int main (){

class TempData{
public:
		int hour;
		double temp;
}


	ifstream is { "input.txt"}; //μ†ŒμŠ€νŒŒμΌκ³Ό 같이 μžˆλŠ” κ²½λ‘œμ—μ΄γ…†γ…‡μ•Όν•¨
		//μ΄ˆλ‘μƒ‰μ€ μ˜ˆμ•½μ–΄ 
		if (!is) {
			cerr << "νŒŒμΌμ˜€ν”ˆ μ‹€νŒ¨" << endl; 
			exit(1);
	}

int num;
while (is){
is>>num;
cout<<num << " ";
}

cout << endl;
is.close();

return 0;
}


14. μ’Œν‘œ(μ ˆλŒ€κ²½λ‘œ/μƒλŒ€κ²½λ‘œ)

ν•¨μˆ˜μ— 파일λͺ…λ§Œ μ§€μ •ν•˜λ©΄ ν˜„μž¬ μž‘μ—… 디렉터리(working directory)μ—μ„œ νŒŒμΌμ„ μ—°λ‹€

Copy
fopen("hello.txt", "w") ν˜„μž¬ 파일 μ˜€ν”ˆ
- μ ˆλŒ€ 경둜 : ifstream is("c:\\in\\input");
- μƒλŒ€ 경둜 : ifstream is{"in\\input.txt"};
							μƒλŒ€ κ²½λ‘œλŠ” μž‘μ—… 디렉터리λ₯Ό κΈ°μ€€μ˜ 파일
- μƒλŒ€κ²½λ‘œ κΈ°μ€€ ..\\ λŠ” λΆ€λͺ¨λ””렉토리λ₯Ό λœ»ν•¨
• ifstream is{ "..\\in\\input.txt" };
- 

λ¦¬λˆ…μŠ€ 및 OS XλŠ” 경둜λ₯Ό ν‘œν˜„ν•  λ•Œ \ λŒ€μ‹  /λ₯Ό μ‚¬μš©
• fopen("/home/project/hello.txt", "w");


..\\
..\\..\\

 

15. ν”„λžœλ“œ

❀️
μ™ΈλΆ€μ˜ ν΄λž˜μŠ€λ‚˜ ν•¨μˆ˜κ°€ μžμ‹ μ˜ λ‚΄λΆ€ 데이터에 μ ‘κ·Όν•  수 μžˆλ„λ‘ ν—ˆκ°€ν•˜λŠ” νŠΉμˆ˜ν•œ ν•¨μˆ˜ ν”„λ Œλ“œ ν•¨μˆ˜μ˜ μ›ν˜•μ€ 클래슀 μ•ˆμ— 포함 κ·Έ 클래슀의 λͺ¨λ“  λ©€λ²„ν•¨μˆ˜λŠ” μžμ‹ μ˜ λ‚΄λΆ€ 데이터λ₯Ό μ°Έμ‘°ν•  수 μžˆλ‹€. β—Ό ν•¨μˆ˜μ˜ μ›ν˜•μ—μ„œλ§Œ μ‚¬μš© β—Ό ν•˜μ§€λ§Œ 멀버 ν•¨μˆ˜λŠ” μ•„λ‹ˆλ‹€. β—Ό ν”„λ Œλ“œ ν•¨μˆ˜μ˜ λ³Έμ²΄λŠ” μ™ΈλΆ€μ—μ„œ λ”°λ‘œ μ •μ˜ β—Ό ν”„λ Œλ“œ ν•¨μˆ˜λŠ” 클래슀 λ‚΄λΆ€μ˜ λͺ¨λ“  멀버 λ³€μˆ˜,ν•¨μˆ˜λ₯Ό μ‚¬μš©,호좜 κ°€λŠ₯ ⇒ 두 객체λ₯Ό λΉ„κ΅ν• λ•Œ μ‚¬μš©
Copy
class MyClass
{
	friend void sub();
//friend ν΄λž˜μŠ€μ΄λ¦„ ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜λͺ©λ‘);
...
};
Copy
#include <iostream>
#include <string>
class Company {
private:
	int sales;
	int profitl
// sub()λŠ”Company의 μ „μš©λ©€λ²„μ— μ ‘κ·Όν•  수 μžˆλ‹€. 
pubilc:
	friend void sub(Company& c);

company() : sales(0), profit(100) {}

}; //μ»΄νΌλ‹ˆ μ•ˆμ— 친ꡬ λ§Œλ“¬

void sub(Company& c) { //c객체가  μ ‘κ·Όκ°€λŠ₯ν•˜λ‹ˆ 싀행됨
cout<< c.profit << endl;
}

int main {

Company c1; //μ‹€ν–‰μ‹œν‚€λ©΄μ„œ
sub(c1); // 객체 μ§‘μ–Έν—ˆμŒλ…€

return 0;
}

 

μ†ŒλŸ‰μ˜ λ©”λͺ¨λ¦¬ : μŠ€νƒ λ©”λͺ¨λ¦¬ / λΉ λ₯΄λ‹€

λŒ€λŸ‰μ˜ λ©”λͺ¨λ¦¬ : νž™ λ©”λͺ¨λ¦¬ / κ΄‘ν™œν•œ λ©”λͺ¨λ¦¬ μ‚¬μš©κ°€λŠ₯