try-catch, 다중 try-catch

#include<iostream>
using namespace std;

void main() {
	int pizza = 0;
	int per = 0;
	int sum = 0;

	cout << "피자 갯수 : ";
	cin >> pizza;
	
	try {
		cout << "사람의 수 : ";
		cin >> per;
		if (per == 0)
			throw per;

		sum = pizza / per;
		cout << "한 사람당 피자는 " << sum << "개 입니다" << endl;
	}
	catch (int e) { // ... -> 모든 예외를 잡음.
		cout << "사람이 0명 입니다" << endl;
	}

}
#include<iostream>
using namespace std;

int sub(int per) {
	if (per == 0)
		throw per;

	int sum = pizza / per;
	return sum;
}

void main() {
	int pizza = 0;
	int per = 0;
	int sum = 0;

	cout << "피자 갯수 : ";
	cin >> pizza;
	
	try {
		cout << "사람의 수 : ";
		cin >> per;
		sum = sub(per);
		
		sum = pizza / per;
		cout << "한 사람당 피자는 " << sum << "개 입니다" << endl;
	}
	catch (int e) { // ... -> 모든 예외를 잡음.
		cout << "사람이 0명 입니다" << endl;
	}

}
#include<iostream>
using namespace std;

void main() {
	int pizza = 0;
	int per = 0;
	int sum = 0;

	cout << "피자 갯수 : ";
	cin >> pizza;
	
	try {
		cout << "사람의 수 : ";
		cin >> per;
		if (per == 0)
			throw per;
		int sum = pizza / per;
		cout << "한 사람당 피자는 " << sum << "개 입니다" << endl;
	}
	catch (...) { 
		cout << "사람이 0명 입니다" << endl;
	}

}
#include<iostream>
using namespace std;

void main() {
	int pizza = 0;
	int per = 0;
	int sum = 0;

	cout << "피자 갯수 : ";
	cin >> pizza;
	
	try {
		cout << "사람의 수 : ";
		cin >> per;
		if (per == 0)
			throw per;
		if (per < 0)
			throw "negative";
		int sum = pizza / per;
		cout << "한 사람당 피자는 " << sum << "개 입니다" << endl;
	}
	catch (int e) {
		cout << "정수형 사람 수는" << e << "입니다" << endl;
	}
	catch (double e) { 
		cout << "더블형 사람 수는" << e << "입니다" << endl;
	}
	catch (const char* e) {
		cout << "문자형 사람 수는" << e << "입니다" << endl;
	}
	catch (...) {
		cout << "어떤 에러든 잡습니다." << endl;
	}
}

템플릿 프로그래밍

#include<iostream>
using namespace std;

template<typename T>
T sum(T x) {
	return x + 10;
}

void main() {
	int num = 10;
	cout << sum(num) << endl;
}
#include<iostream>
using namespace std;

template<typename T>
T get_max(T x, T y) {
	if (x > y) return x;
	else return y;
}

void main() {
	int num1 = 10;
	int num2 = 20;
	double num3 = 10.5;
	double num4 = 20.3;
	cout << get_max(num1, num2) << endl;
	cout << get_max(num3, num4) << endl;
}
#include<iostream>
using namespace std;

template<typename T, typename U>
T get_max(T x, U y) {
	if (x > y) return x;
	else return y;
}

void main() {
	int num1 = 10;
	int num2 = 20;
	double num3 = 10.5;
	double num4 = 20.3;
	cout << get_max(num1, num3) << endl;
	cout << get_max(num3, num4) << endl;
}

<aside> ➡️ 10 20.3

</aside>

→ 10 이랑 10.3 비교하면 당연히 10.3 이 큰데 왜 10이 나올까? 뭐가 문제일까?

: num은 정수죠, num3는 더블이야, 그치만 리턴값이 T, 즉 int로 나오죠. U는 소숫점이 짤려서 10이랑 10 비교해서 10이 나온 겁니다.

#include<iostream>
using namespace std;

template<typename T, typename U>
T get_max(T x, U y) {
	cout << "템플릿 프로그램" << endl;
	if (x > y) return x;
	else return y;
}

int get_max(int x, double y) {
	cout << "일반 프로그램" << endl;
	if (x > y) return x;
	else return y;
}

void main() {
	int num1 = 10;
	int num2 = 20;
	double num3 = 10.5;
	double num4 = 20.3;

	cout << get_max(num1, num3) << endl;
	cout << get_max(num3, num4) << endl;
}

클래스 템플릿

#include<iostream>
using namespace std;

template<typename T>
class Box {
private:
	T data;
	int num;
public:
	Box(T v, int n) {
		data = v;
		num = n;
	}

	void view() {
		cout << "data = " << data << endl;
		cout << "num = " << num << endl;
	}
};

void main() {
	Box<int> b1(10, 20);
	b1.view();
}