1. 程式人生 > >C++ Premier Plus 6th edition - Programming excercise - Chapter11 - 7

C++ Premier Plus 6th edition - Programming excercise - Chapter11 - 7

complex0.h

#ifndef COMPLEX0_H_
#define COMPLEX0_H_

class Complex0
{
private:
    double real;
    double imag;

public:
    Complex0()
    {
        real = imag = 0.0;
    };// default constructor
    Complex0(double x, double y); // self-defined consturctor

    // member functions for reload operator + - *
Complex0 operator+(Complex0& obj) const; Complex0 operator-(Complex0& obj) const; Complex0 operator*(Complex0& obj) const; Complex0 operator*(double n) const; Complex0 operator~() const; ~Complex0() {};// distructor // friend, support reload operator << and >>
friend std::ostream& operator<<(std::ostream & os, const Complex0& obj); // in case cin >> obj1 >> obj2 friend bool operator>>(std::istream& os, Complex0& obj); // support 2*objective, using revolk member funcion * friend Complex0 operator*(
double n, Complex0& obj) { return obj * n; }; }; #endif /* tips: delcare a complex0,including 2 data members place rules of how to use them into public */

complex0.cpp

#include<iostream>
#include"complex0.h"

Complex0::Complex0(double x, double y)
{
    real = x;
    imag = y;
}// self-defined consturctor

// member functions for reload Complex0::operator + - *
Complex0 Complex0::operator+(Complex0& obj) const
{
    return Complex0(real+obj.real,imag+obj.imag);
}

Complex0 Complex0::operator-(Complex0& obj) const
{
    return Complex0(real - obj.real, imag - obj.imag);
}

Complex0 Complex0::operator*(Complex0& obj) const
{
    double x;
    double y;
    x = real * obj.real - imag * obj.imag;
    y = real * obj.imag + imag * obj.real;
    return Complex0(x, y);
}

Complex0 Complex0::operator*(double n) const
{
    return Complex0(n*real, n*imag);
}

Complex0 Complex0::operator~() const
{
    return Complex0(real, (-1)*imag);
}

// friend, support reload operator << and >>
std::ostream& operator<<(std::ostream & os, const Complex0& obj)
{
    os << "(" << obj.real << "," << obj.imag << "i)";
    return os;
}

bool operator>>(std::istream& is, Complex0& obj)
{
    std::cout << "Enter value for real: ";
    is >> obj.real;

    // support q to quit
    if (std::cin.fail())
    {
        std::cin.clear();
        while (std::cin.get() != '\n')
            continue;//support cin.get() in main to avoid windown disappear
        return false;
    }
    else
    {
        std::cout << "Enter value for imaginary: ";
        is >> obj.imag;
        return true;
    }
}

main.cpp

// main()
#include <iostream>
using namespace std;
#include "complex0.h" // to avoid confusion with complex.h
int main()
{
    Complex0 a(3.0, 4.0); // initialize to (3,4i)
    Complex0 c;
    cout << "Enter a complex number (q to quit):\n";
    while (cin >> c)
    {
        cout << "c is " << c << '\n';
        cout << "complex conjugate is " << ~c << '\n';
        cout << "a is " << a << '\n';
        cout << "a + c is " << a + c << '\n';
        cout << "a - c is " << a - c << '\n';
        cout << "a * c is " << a * c << '\n';
        cout << "2 * c is " << 2 * c << '\n';
        cout << "Enter a complex number (q to quit):\n";
    }
    cout << "Done!\n";

    // optional
    cin.get();

    return 0;
}