> For the complete documentation index, see [llms.txt](https://ezcode.gitbook.io/bnb-cpp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ezcode.gitbook.io/bnb-cpp/inheritance/undefined.md). Agents can also search and read this documentation through its [MCP server](https://ezcode.gitbook.io/bnb-cpp/~gitbook/mcp).

# 클래스 심화

## 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);
}
```

![](https://2541775619-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJx2Y5x2vD7FhD7vVhT%2F-LqEpsi8WEPy5n4kmpQw%2F-LqEpuulzPR1ul4nJPZw%2F1.png?alt=media\&token=9b7b4710-db63-4c56-bff7-dee8cc529fa6)

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

```
#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;

![](https://2541775619-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJx2Y5x2vD7FhD7vVhT%2F-LqEpsi8WEPy5n4kmpQw%2F-LqEqXEmaV2vtZwyTyGw%2F2.png?alt=media\&token=b1051ead-fae6-46d6-8315-40d85445d0af)

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

```
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함수를 명시적 호출
}
```

![](https://2541775619-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LJx2Y5x2vD7FhD7vVhT%2F-LqExhCH98qvoDap7yhc%2F-LqExjUuQOTlVRRp91_R%2F3.png?alt=media\&token=6b2a573b-35a5-4de0-8987-21e581537455)
