C++_OOP2
53371 ワード
Hello C++I/O
出力(cout)
#include <iostream>
// io : input, output
// 입출력을 흘러가게 하는 역할을 하는 라이브러리
int main()
{
std::cout << "Hello C++" << std::endl;
// std::cout 객체 : c++에서 화면에 출력시 사용하는 출력 객체
// std라는 이름 공간에 존재하는 cout 객체를 사용하겠다는 의미
return 0;
}
Hello C++
::
:scope演算子、領域決定演算子#include <iostream>
int main()
{
int a = 10;
float b = 3.14f;
char c = 'A';
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
return 0;
}
10
3.14
A
#include <iostream>
using namespace std;
int main()
{
int a = 10;
float b = 3.14f;
char c = 'A';
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
using namespace std;
で簡単に記述できます.
ネーミングスペース(namespace)
これは、特定のスペースに名前を指定することを意味します.#include <iostream>
namespace A
{
void func(void)
{
std::cout << "A학급 주성" << std::endl;
}
}
namespace B
{
void func(void)
{
std::cout << "B학급 주성" << std::endl;
}
}
int main()
{
A::func();
return 0;
}
A학급 주성
入力(cin)
#include <iostream>
using namespace std;
int main()
{
int a;
float b;
char c;
cin >> a;
cout << "정수값 출력 : " << a << endl;
cin >> b;
cout << "정수값 출력 : " << b << endl;
cin >> c;
cout << "문자열 출력 : " << c << endl;
return 0;
}
1
정수값 출력 : 1
1.4
정수값 출력 : 1.4
a
문자열 출력 : a
C++の新しいルール
変数の宣言
Cは変数を宣言してコードを記述する必要があり、C++は変数を中間的に宣言することができる.
パラメータのデフォルト設定
#include <iostream>
// io : input, output
// 입출력을 흘러가게 하는 역할을 하는 라이브러리
int main()
{
std::cout << "Hello C++" << std::endl;
// std::cout 객체 : c++에서 화면에 출력시 사용하는 출력 객체
// std라는 이름 공간에 존재하는 cout 객체를 사용하겠다는 의미
return 0;
}
Hello C++
#include <iostream>
int main()
{
int a = 10;
float b = 3.14f;
char c = 'A';
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
return 0;
}
10
3.14
A
#include <iostream>
using namespace std;
int main()
{
int a = 10;
float b = 3.14f;
char c = 'A';
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
#include <iostream>
namespace A
{
void func(void)
{
std::cout << "A학급 주성" << std::endl;
}
}
namespace B
{
void func(void)
{
std::cout << "B학급 주성" << std::endl;
}
}
int main()
{
A::func();
return 0;
}
A학급 주성
#include <iostream>
using namespace std;
int main()
{
int a;
float b;
char c;
cin >> a;
cout << "정수값 출력 : " << a << endl;
cin >> b;
cout << "정수값 출력 : " << b << endl;
cin >> c;
cout << "문자열 출력 : " << c << endl;
return 0;
}
1
정수값 출력 : 1
1.4
정수값 출력 : 1.4
a
문자열 출력 : a
変数の宣言
Cは変数を宣言してコードを記述する必要があり、C++は変数を中間的に宣言することができる.
パラメータのデフォルト設定
void func(int a = 10, int b = 20);
func(100, 200); // 전달인자의 값은 각각 100과 200
func(50); // 전달인자의 값은 50, 20
func(); // 전달인자의 값은 10, 20
パラメータのデフォルト値を設定すると、呼び出し部分でパラメータを省略してもデフォルト値が設定されます.#include <iostream>
using namespace std;
void func(int a = 10, int b = 20);
int main()
{
func(); // 전달인자x
func(100);
func(100, 200);
return 0;
}
void func(int a, int b)
{
cout << "두 전달인자 출력: " << a << " " << b << endl;
}
두 전달인자 출력: 10 20
두 전달인자 출력: 100 20
두 전달인자 출력: 100 200
グローバル変数とゾーン変数
C言語では、グローバル変数とゾーン変数の名前が同じである場合、ゾーン変数はグローバル変数よりも優先されます.
#include <stdio.h>
int nTemp = 10;
int main()
{
{
int nTemp = 20;
printf("nTemp의 값은 : %d이다.\n", nTemp);
}
printf("nTemp의 값은 : %d이다.\n", nTemp);
}
nTemp의 값은 : 20이다.
nTemp의 값은 : 10이다.
C++言語では、グローバル変数とゾーン変数が存在する場合、ゾーン決定演算子::
によってゾーン内にグローバル変数を出力することができる.#include <iostream>
using namespace std;
int nTemp = 10;
int main()
{
{
int nTemp = 20;
cout << "nTemp = " << nTemp << endl;
cout << "::nTemp = " << ::nTemp << endl;
}
cout << "nTemp = " << nTemp << endl;
}
nTemp = 20
::nTemp = 10
nTemp = 10
行内関数
これは、コード線自体が呼び出し部分に含まれていることを意味し、
#define
マクロの欠点を補い、通常の関数呼び出しの負荷を軽減する.最近、コンパイラの性能はとても良くて、インライン関数を追加する必要がなくて、コンパイラに必要な機能の上でインライン関数を使うことができます.かふか
Cの2つの数値の合計を返す関数については、パラメータとして整数、実数、文字値を入力する関数を個別に宣言する必要があります.
#include <stdio.h>
int AddI(int a, int b);
double AddD(double a, double b);
char AddC(char a, char b);
int main()
{
int i;
double d;
char c;
i = AddI(10, 20);
d = AddD(3.14, 1.59);
c = AddC('A', 1);
printf("정수값 출력 : %d\n", i);
printf("실수값 출력 : %f\n", d);
printf("문자값 출력 : %c\n", c);
}
int AddI(int a, int b)
{
return a + b;
}
double AddD(double a, double b)
{
return a + b;
}
char AddC(char a, char b)
{
return a + b;
}
정수값 출력 : 30
실수값 출력 : 4.730000
문자값 출력 : B
C++では、リロードして同じ名前で関数を作成できます.#include <iostream>
using namespace std;
int Add(int a, int b);
double Add(double a, double b);
char Add(char a, char b);
int main()
{
int i;
double d;
char c;
i = Add(10, 20);
d = Add(3.14, 1.59);
c = Add('A', char(1));
printf("정수값 출력 : %d\n", i);
printf("실수값 출력 : %f\n", d);
printf("문자값 출력 : %c\n", c);
}
int Add(int a, int b)
{
return a + b;
}
double Add(double a, double b)
{
return a + b;
}
char Add(char a, char b)
{
return a + b;
}
정수값 출력 : 30
실수값 출력 : 4.730000
문자값 출력 : B
テンプレートコードを使用すると、作成が簡単になります.通常はフレームワーク開発者またはエンジン開発者が使用します.#include <iostream>
using namespace std;
template <class T> T Add(T a, T b);
int main()
{
int i;
double d;
char c;
i = Add(10, 20);
d = Add(3.14, 1.59);
c = Add('A', char(1));
printf("정수값 출력 : %d\n", i);
printf("실수값 출력 : %f\n", d);
printf("문자값 출력 : %c\n", c);
}
template <class T> T Add(T a, T b)
{
return a + b;
}
クラス(Class)
ユーザー定義の抽象データ型
クラスはC言語の構造体と似ており、メンバー変数とメンバー関数から構成されています.class 클래스 이름
{
접근 지정자:
데이터형 멤버변수;
접근 지정자:
데이터형 멤버함수;
}
アクセス指定子
クラスは、メンバー宣言時にアクセス権を設定します.private
:アクセス不可public
:アクセス可能
メンバー関数へのアクセス
#include <iostream>
using namespace std;
// 클래스 정의
class MousePoint
{
private : // 외부에서 접근하지 못하게 함
int x, y;
public :
void SetXY(int nX, int nY);
};
// 멤버함수 구현
void MousePoint::SetXY(int nX, int nY)
{
x = nX;
y = nY;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
// 객체 생성
int main()
{
MousePoint point; // 객체
point.SetXY(10, 20);
}
x = 10
y = 20
MousePoint
クラスでは、メンバー変数x, y
がprivate
にブロックされるため、オブジェクトから変数にアクセスするには、メンバー関数SetXY
を通過する必要がある.
作成者と消滅者
コンストラクタ
初期化
初期化は、基本データを宣言しながら初期値を入力します.int a = 5;
float b = 10.5;
int c[5] = {1, 2, 3, 4, 5};
変数宣言後に値を割り当てる場合は、初期化とは呼ばれません.int a;
a = 5;
なぜ作成者が必要なのですか?
クラスを作成するときは、毎回繰り返される初期化メンバー関数を作成する必要があります.オブジェクトの作成時に自動的に初期化される専用関数があり、생성자(constructor)
と呼ばれます.
コンストラクション関数の特徴
class 클래스 이름
{
접근 지정자:
데이터형 멤버변수;
접근 지정자:
데이터형 멤버함수;
}
#include <iostream>
using namespace std;
// 클래스 정의
class MousePoint
{
private : // 외부에서 접근하지 못하게 함
int x, y;
public :
void SetXY(int nX, int nY);
};
// 멤버함수 구현
void MousePoint::SetXY(int nX, int nY)
{
x = nX;
y = nY;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
// 객체 생성
int main()
{
MousePoint point; // 객체
point.SetXY(10, 20);
}
x = 10
y = 20
int a = 5;
float b = 10.5;
int c[5] = {1, 2, 3, 4, 5};
int a;
a = 5;
ジェネレータもオーバーロードできることを覚えておいてください.
消滅者(destructor)
オブジェクトが消滅したときに自動的に呼び出される消滅者が存在する.
消滅者の特徴
~
符号#include <iostream>
using namespace std;
// 클래스 정의
class MousePoint
{
private : // 외부에서 접근하지 못하게 함
int x, y;
public :
void SetXY(int nX, int nY);
MousePoint(); // 디폴트 생성자
MousePoint(int nX, int nY); // 생성자
~MousePoint(); // 소멸자
};
void MousePoint::SetXY(int nX, int nY) // 멤버함수
{
x = nX;
y = nY;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
MousePoint::MousePoint() // 디폴트 생성자
{
cout << "생성자 호출" << endl;
x = 10;
y = 20;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
MousePoint::MousePoint(int nX, int nY) // 생성자
{
x = nX;
y = nY;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
MousePoint::~MousePoint() // 소멸자
{
cout << "소멸자 호출" << endl;
}
// 객체 생성
int main()
{
MousePoint point; // 객체
}
생성자 호출
x = 10
y = 20
소멸자 호출
ジェネレータはオーバーロードできます.クラスのフィーチャーとオブジェクトの使用
オブジェクト配列
複数の変数を使用して配列を宣言するように、オブジェクトは객체 배열
として宣言することもできます.#include <iostream>
using namespace std;
// 클래스 정의
class MousePoint
{
private : // 외부에서 접근하지 못하게 함
int x, y;
public :
void SetXY(int nX, int nY);
MousePoint(); // 디폴트 생성자
MousePoint(int nX, int nY); // 생성자
~MousePoint(); // 소멸자
// private한 x,y값을 출력
int GetX(){return x;}
int GetY(){return y;}
};
// 멤버함수 구현
void MousePoint::SetXY(int nX, int nY)
{
x = nX;
y = nY;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
MousePoint::MousePoint() // 디폴트 생성자
{
cout << "생성자 호출" << endl;
x = 10;
y = 20;
// cout << "x = " << x << endl;
// cout << "y = " << y << endl;
}
MousePoint::MousePoint(int nX, int nY) // 생성자
{
x = nX;
y = nY;
// cout << "x = " << x << endl;
// cout << "y = " << y << endl;
}
MousePoint::~MousePoint() // 소멸자
{
cout << "소멸자 호출" << endl;
}
// 객체 생성
int main()
{
MousePoint pt[3] = {MousePoint(10, 20), MousePoint(30, 40), MousePoint(50, 60)};
// pt[0].x = 10, pt[0].y = 20 ....
for (int i = 0; i < 3; i++)
{
cout << pt[i].GetX() << endl;
cout << pt[i].GetY() << endl << endl;
}
}
10
20
30
40
50
60
소멸자 호출
소멸자 호출
소멸자 호출
オブジェクトポインタ
オブジェクトのアドレス値を格納する変数で、オブジェクトを間接的に参照します.MousePoint *pObj;
MousePoint pt(10,20)
pObj = &pt;
Reference
この問題について(C++_OOP2), 我々は、より多くの情報をここで見つけました
https://velog.io/@gimmicks_/cpp2
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
#include <iostream>
using namespace std;
// 클래스 정의
class MousePoint
{
private : // 외부에서 접근하지 못하게 함
int x, y;
public :
void SetXY(int nX, int nY);
MousePoint(); // 디폴트 생성자
MousePoint(int nX, int nY); // 생성자
~MousePoint(); // 소멸자
// private한 x,y값을 출력
int GetX(){return x;}
int GetY(){return y;}
};
// 멤버함수 구현
void MousePoint::SetXY(int nX, int nY)
{
x = nX;
y = nY;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
MousePoint::MousePoint() // 디폴트 생성자
{
cout << "생성자 호출" << endl;
x = 10;
y = 20;
// cout << "x = " << x << endl;
// cout << "y = " << y << endl;
}
MousePoint::MousePoint(int nX, int nY) // 생성자
{
x = nX;
y = nY;
// cout << "x = " << x << endl;
// cout << "y = " << y << endl;
}
MousePoint::~MousePoint() // 소멸자
{
cout << "소멸자 호출" << endl;
}
// 객체 생성
int main()
{
MousePoint pt[3] = {MousePoint(10, 20), MousePoint(30, 40), MousePoint(50, 60)};
// pt[0].x = 10, pt[0].y = 20 ....
for (int i = 0; i < 3; i++)
{
cout << pt[i].GetX() << endl;
cout << pt[i].GetY() << endl << endl;
}
}
10
20
30
40
50
60
소멸자 호출
소멸자 호출
소멸자 호출
MousePoint *pObj;
MousePoint pt(10,20)
pObj = &pt;
Reference
この問題について(C++_OOP2), 我々は、より多くの情報をここで見つけました https://velog.io/@gimmicks_/cpp2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol