객체 지향 프로그래밍(also known as OOP)에서 데이터를 포함하는 개별적인 개체(entity)로서 취급되는 변수
클래스를 이용하여 정의된 변수
객체 지향 프로그래밍
객체 위주로 프로그램 작성
클래스는 프로그램을 블록으로 묶은 것이며(Encapsulation), 클래스 변수는 객체(Object)이므로 클래스를 사용한 프로그래밍
using namespace std;
class Test_class { // user자료형
public:
int test_a_int,test_b_int,result_int; // 변수선언
void input_test_method() { // 입력값 받기
cout << "input vaule of int a : ";
cin >> test_a_int;
cout << "input vaule of int b : ";
cin >> test_b_int;
}
void plus_test_method() { // 연산
result_int = test_a_int + test_b_int;
}
void output_test_method() { // 출력
cout << result_int << endl;
}
}; // ;꼭 붙여줘야함
void main() {
Test_class test_object; // class 변수 선언 == Object 생성
test_object.input_test_method();
// ex) input 5, 8
test_object.plus_test_method();
// ex) 5 + 8 = 13 therefore, saved result value is 13.
test_object.output_test_method();
// ex) output : 13
}
접근지정자
private : 클래스 내부에서만 접근
protected : 클래스 내부와 상속받은 클래스 내부에서 접근
pubilc : 클래스 내부와 외부에서 접근
class Test_class {
private:
int test_int = 10; // 클래스 내부에서만 사용할 변수
public:
int test_member_variable_int; // 클래스 외부에서도 사용할 변수
void multiply_test() {
for (int i = 1; i < test_int; i++) {
assert(test_member_variable_int < 10);
cout << test_member_variable_int << " * " << i << " = "; // 출력폼 지정
cout << test_member_variable_int * i << endl;
}
}
void multiply_test(int a, int b) { // 오버로딩 테스트
cout << a << " * " << b << " = ";
cout << a * b << endl;
}
// 상속은 나중에 상속 파트에서 작성
};
void main() {
Test_class* dynamic_test_object;
dynamic_test_object = new Test_class; // 동적할당
cout << "input int value [1-9]{1} : ";
cin >> dynamic_test_object->test_member_variable_int; // 값 저장
dynamic_test_object->multiply_test(); // 함수 호출
dynamic_test_object->multiply_test(7,7);
delete dynamic_test_object; // 해제
}