Insert 연산자

이동규·2024년 11월 26일

C++

목록 보기
3/16

<< 출력하는 연산자이다.c++ 에서는 +- 연산자를 사용해서 프로그래머가 연산자의 동작을 바꿀수있다.

#include <iostream>

using namespace std;
int main(int argc, char const *argv[])
{
    /* code */
    int integer = 10;
    float decimal = 1.5f;
    char letter = 'A';
    int a[] = {1, 2, 3};
    char string[] = "hello world";
    cout << integer << " " << letter << a[0] << string << endl;

    return 0;
}

#include <iostream>
using namespace std;

class Complex {
public:
    double real, imag;

    Complex(double r, double i) : real(r), imag(i) {}

    // + 연산자 오버로딩
    Complex operator+(const Complex& other) {
        return Complex(real + other.real, imag + other.imag);
    }

    void display() {
        cout << real << " + " << imag << "i" << endl;
    }
};

int main() {
    Complex c1(2.0, 3.0);
    Complex c2(1.5, 2.5);

    Complex c3 = c1 + c2; // + 연산자 오버로딩 사용
    c3.display();        // 출력: 3.5 + 5.5i

    return 0;
}

0개의 댓글