# 클래스 심화

## Final 클래스

final로 선언된 클래스는 더이상 상속 할 수 없다.

```cpp
class A{};
class B : A {};
class C final : B {}; // final 클래스 C
class D : C {}; // 에러 C클래스는 더이상 상속할수 없다.
```

final 은 키워드가 아니고 식별자 이므로 변수명 등으로 사용가능하나 사용하지 않는것이 좋음!

## 이름 은폐(name hiding)

동일한 이름의 함수가 어떠한 영역에 선언되면 선언한 영역의 바깥영역의 함수는 은폐된다.

### 일반적인 다중정의

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

void f(int x) {
	cout << "f(int x) --> " << x << endl;
}
void f(double x) {
	cout << "f(double x) --> " << x << endl;
}
int main() {
	f(10);
	f(20.0);
}
```

![](/files/-LqEpuulzPR1ul4nJPZw)

### 전역함수를 가리는 함수

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

void f(int x) {
	cout << "f(int x) --> " << x << endl;
}
void f(double x) {
	cout << "f(double x) --> " << x << endl;
}
int main() {
	void f(int x); // 전역변수에 있는 f(int x) 함수의 원형 선언
	f(10);
	f(20.0); // 묵시적 형변환이 되므로 f(int x) 호출
	::f(30.0); // 전역함수를 호출
}
```

main 함수내 f 함수를 선언하여 전역함수 f를 가리게&#x20;

![](/files/-LqEqXEmaV2vtZwyTyGw)

#### 형변환이 되지 않는 경우에는 함수 호출을 할 수 없음

```
include <iostream>
using namespace std;

void f(int x) {
	cout << "f(int x) --> " << x << endl;
}
void f(const char* x) {
	cout << "f(const char* x) --> " << x << endl;
}
int main() {
	void f(int x);  // 전역변수에 있는 f(int x) 함수의 원형 선언
	f(10);
	f("abc"); // 에러 형변환 할수 없음
	::f("abc"); // 전역 함수 호출
}
```

### 클래스 상속에서 이름 은폐

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

class A {
public:
	void f(int x) {
		cout << "A::f(int x) --> " << x << endl;
	}
	void f(const char* x) {
		cout << "A::f(const char* x) --> " << x << endl;
	}
};

class B : public A {
public:
	void f(double x) { // A클래스의 f함수를 은폐
		cout << "B::f(double x) --> " << x << endl;
	}
};

int main() {
	B objB;
	objB.f(10.0); 
	objB.f(20); // 묵시적 형변환
	// objB.f("dksldk"); // 에러 형변환 할 수 없음
	objB.A::f(30); // A의 f함수를 명시적 호출
}
```

![](/files/-LqExjUuQOTlVRRp91_R)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ezcode.gitbook.io/bnb-cpp/inheritance/undefined.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
