연산자 중복
C++ 표준 연산자를 객체에 대해서도 적용할 수 있도록 정의하는 것
string 클래스는 연산자 중복을 사용하고 있음
int main() {
string s1 = "Rogue One: ";
string s2 = "A Star Wars Story";
string s3;
s3 = s1 + s2;
cout << "s1=" << s1 << endl;
cout << "s2=" << s2 << endl;
cout << "s1+s2=
" << s3 << endl;
cout << "s1==s2 " << (s1 == s2) << endl;
}
중복할 수 없는 연산자
연산자 | 설명 |
:: | 범위 지정 연산자 |
. | 멤버 선택 연산자 |
.* | 멤버 포인터 연산자 |
?: | 조건 연산자 |
연산자 중복 정의
연산자 | 설명 |
+ | operator+() |
- | operator-() |
* | operator*() |
/ | operator/() |
대입 연산자를 중복
class Box
{
private:
double length, width, height;
public:
// 멤버 초기화 리스트 작성해보자
void display() {
cout << "(" << length << ", " << width << ", " << height << ")" << endl;}
};
int main()
{
Box b1(30.0, 30.0, 60.0), b2;
b1.display();
b2 = b1;
b2.display();
}
반환형이 참조자인 경우
'C++' 카테고리의 다른 글
<C++> 10. 스택 (0) | 2024.04.15 |
---|---|
<C++> 9. 시간 복잡도 (0) | 2024.04.15 |
<C++> 7. 클래스 (0) | 2024.04.01 |
<C++> 6. 배열과 벡터 (1) | 2024.03.26 |
<C++> 5. 함수 (0) | 2024.03.25 |