728x90
static 변수
-컴파일시 변수의 값이 결정됨
-static 선언문은 런타임시 실행되지 않음
-프로그램이 끝날때까지 값을 유지함
static 없을 경우
static 있는 경우
클래스에서 static 변수는 하나만 만들어지고 모든 객체가 공유한다.
static 변수는 선언하고 클래스 외부에서 정의를 해야한다.
정리
멤버변수는 객체마다 따로 생성
멤버함수는 모든 객체가 공유
static 변수는 모든 객체가 공유, 선언하고 클래스 외부에서 정의를 해야함
템플릿(template), 제네릭
자료형이 나중에 결정된다.
오버로딩 -> 제네릭 변환
#include <iostream>
using std::cout;
using std::endl;
int Max(int i, int j)
{
return i > j ? i : j;
}
double Max(double i, double j)
{
return i > j ? i : j;
}
char Max(char i, char j)
{
return i > j ? i : j;
}
int main()
{
cout << "Max값은=" << Max(1, 2) << endl;
cout << "Max값은=" << Max(7.5, 3.6) << endl;
cout << "Max값은=" << Max('A', 'B');
return 0;
}
// 제네릭 사용
#include <iostream>
using std::cout;
using std::endl;
template <class T> T Max(T i, T j)
{
return i > j ? i : j;
}
int main()
{
cout << "Max값은=" << Max(1, 2) << endl;
cout << "Max값은=" << Max(7.5, 3.6) << endl;
cout << "Max값은=" << Max('A', 'B');
return 0;
}
제네릭 기본형
1: template <class T> 리턴형 함수이름
2: template <typename T> 리턴형 함수이름
둘중 아무거나 써도된다.
class 제네릭 사용
#include <iostream>
using std::cout;
using std::endl;
template <class T> class CCC1
{
T x;
T y;
public:
CCC1(T xx, T yy) { x = xx; y = yy; }
void Print() { cout << x << ',' << y << endl; }
};
int main()
{
CCC1<int> c1(10, 20);
CCC1<double> c2(3.5, 5.5);
CCC1<char> c3('I', 'd');
c1.Print();
c2.Print();
c3.Print();
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
template <class T1, class T2>
class CCC1
{
T1 x;
T2 y;
public:
CCC1(T1 xx, T2 yy) { x = xx; y = yy; }
void Print() { cout << x << ',' << y << endl; }
};
int main()
{
CCC1<int, int> c1(10, 20);
CCC1<double, double> c2(3.5, 5.5);
CCC1<char, std::string> c3('I', "Love You!");
c1.Print();
c2.Print();
c3.Print();
return 0;
}
STL라이브러리
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <double> x; //int x[10]와 차이
// 여러 개 int형을 가지고 노는 배열 공간을 만들고 싶어요
x.push_back(1.1);
x.push_back(2.2);
x.push_back(10.4);
for (int i = 0; i < x.size(); i++)
cout << x[i] << endl;
return 0;
}
//typedef basic_string<char> string;
//http://www.cplusplus.com/reference/string/string/
//https://en.cppreference.com/w/cpp/string/basic_string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "안녕!";
cout << str << endl;
str.push_back('H');
str.push_back('i');
str.push_back('a');
cout << str << endl; //안녕!Hi
for (int i = 0; i < str.size(); i++)
cout << str[i];
return 0;
}
런타임시 오류를 잡는 기법
예외 처리X
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void Div(double ja, double mo)
{
cout << "결과:" << ja / mo << endl;
}
int main()
{
double x, y;
cout << "분자를 입력하세요=";
cin >> x;
cout << "분모를 입력하세요=";
cin >> y;
Div(x, y);
return 0;
}
예외처리O
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void Div(double ja, double mo)
{
try
{
if (mo == 0 || ja == 0) throw mo;
cout << "결과:" << ja / mo << endl;
}
catch (double mo)
{
cout << "분자나 분모가 0";
}
}
int main()
{
double x, y;
cout << "분자를 입력하세요=";
cin >> x;
cout << "분모를 입력하세요=";
cin >> y;
Div(x, y);
return 0;
}
예외가 발생할 수 있는 문장을 try 블럭안에 넣는다.
throw를 사용해 예외가 발생한 값을 catch 블럭에 던짐.
catch 블럭은 여러개 사용할 수 있고 throw 타입에 맞는 catch 블럭이 실행됨
이 소스들은 한성현 교수님의 소스를 일부 편집, 수정하여 만들었습니다.
'C++' 카테고리의 다른 글
의존관계 (0) | 2023.12.16 |
---|---|
C++ 프로그래밍 15주차 (0) | 2023.12.13 |
C++프로그래밍 13주차 (1) | 2023.11.29 |
C++프로그래밍 12주차 (2) | 2023.11.22 |
C+프로그래밍 11주차 (0) | 2023.11.15 |