Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
982 B
1
Indexable
Never
#include <iostream>

class Complex {
private:
    double real;
    double imag;

public:
    // Default constructor
    Complex() : real(0), imag(0) {}

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

    // Copy constructor
    Complex(const Complex& c) {
        real = c.real;
        imag = c.imag;
    }

    // Display method
    void display() {
        if (imag >= 0) {
            std::cout << real << " + " << imag << "i" << std::endl;
        } else {
            std::cout << real << " - " << -imag << "i" << std::endl;
        }
    }
};

int main() {
    // Create objects using different constructors
    Complex c1;            // Default constructor
    Complex c2(1.2, 3.4);  // Parameterized constructor
    Complex c3(c2);        // Copy constructor

    // Display objects
    std::cout << "c1 = ";
    c1.display();
    std::cout << "c2 = ";
    c2.display();
    std::cout << "c3 = ";
    c3.display();

    return 0;
}