Untitled

 avatar
unknown
plain_text
2 years ago
982 B
3
Indexable
#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;
}
Editor is loading...