侯捷《面向对象高级编程》代码

1. complex类和测试程序

1.1 complex-test.cpp

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

using namespace std;

ostream& operator << (ostream& os, const complex& x)
{
    return os << '(' << real(x) << ',' << imag(x) << endl;
}

int main()
{
    complex c1(2, 1);
    complex c2(4, 0);

    cout << c1 << endl;
    cout << c2 << endl;

    cout << c1 + c2 << endl;
    cout << c1 - c2 << endl;
    cout << c1 * c2 << endl;
    cout << c1 / c2 << endl;

    cout << conj(c1) << endl;
    cout << conj(c2) << endl;
    cout << polar(10, 4) << endl;

    cout << (c1 += c2) << endl;

    cout << (c1 == c2) << endl;
    cout << (c1 != c2) << endl;
    cout << +c2 << endl;
    cout << -c2 << endl;

    cout << (c2 - 2) << endl;
    cout << (5 + c2) << endl;

    return 0;
}

1.2 complex.h 

#ifndef MYCOMPLEX_H
#define MYCOMPLEX_H

#include <cmath>

class complex;
complex& __doapl(complex* ths, const complex& r);
complex& __doami(complex* ths, const complex& r);
complex& __doaml(complex* ths, const complex& r);

class complex
{
public:
    complex(double r = 0, double i = 0): re(r), im(i) {}
    complex& operator += (const complex&);
    complex& operator -= (const complex&);
    complex& operator *= (const complex&);
    complex& operator /= (const complex&);
    double real() const
    {
        return re;
    }
    double imag() const
    {
        return im;
    }

private:
    double re;
    double im;

    friend complex& __doapl(complex*, const complex&);
    friend complex& __doami(complex*, const complex&);
    friend complex& __doaml(complex*, const complex&);
};

inline complex&
__doapl(complex* ths, const complex& r)
{
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}

inline complex&
complex::operator += (const complex& r)
{
    return __doapl(this, r);
}

inline complex&
__doami(complex* ths, const complex& r)
{
    ths->re -= r.re;
    ths->im -= r.im;
    return *ths;
}

inline complex&
complex::operator -= (const complex& r)
{
    return __doami(this, r);
}

inline complex&
__doaml(complex* ths, const complex& r)
{
    double f = ths->re * r.re - ths->im * r.im;
    ths->im = ths->re * r.im + ths->im * r.re;
    ths->re = f;
    return *ths;
}

inline complex&
complex::operator *= (const complex& r)
{
    return __doaml(this, r);
}

inline double
imag(const complex& x)
{
    return x.imag();
}

inline double
real(const complex& x)
{
    return x.real();
}

inline complex
operator + (const complex& x, const complex& y)
{
    return complex(real(x) + real(y), imag(x) + imag(y));
}

inline complex
operator + (const complex& x, double y)
{
    return complex(real(x) + y, imag(x));
}

inline complex
operator + (double x, const complex& y)
{
    return complex(x + real(y), imag(y));
}

inline complex
operator - (const complex& x, const complex& y)
{
    return complex(real(x) - real(y), imag(x) - imag(y));
}

inline complex
operator - (const complex& x, double y)
{
    return complex(real(x) - y, imag(x));
}

inline complex
operator - (double x, const complex& y)
{
    return complex(x - real(y), imag(y));
}

inline complex
operator * (const complex& x, const complex& y)
{
    return complex(real(x) * real(y) - imag(x) * imag(y),
            real(x) * imag(y) + imag(x) * real(y));
}

inline complex
operator * (const complex& x, double y)
{
    return complex(real(x) * y, imag(x) * y);
}

inline complex
operator * (double x, const complex& y)
{
    return complex(x * real(y), x * imag(y));
}

inline complex
operator / (const complex& x, double y)
{
    return complex(real(x) / y, imag(x) / y);
} 

inline complex
operator + (const complex& x)
{
    return x;
}

inline complex
operator - (const complex& x)
{
    return complex(-real(x), -imag(x));
}

inline bool
operator == (const complex& x, const complex& y)
{
    return real(x) == real(y) && imag(x) == imag(y);
}

inline bool
operator == (const complex& x, double y)
{
    return real(x) == y && imag(x) == 0;
}

inline bool
operator != (const complex& x, const complex& y)
{
    return real(x) != real(y) || imag(x) != imag(y);
}

inline bool
operator != (const complex& x, double y)
{
    return real(x) != y || imag(x) != 0;
}

inline bool
operator != (double x, const complex& y)
{
    return x != real(y) || imag(y) != 0;
}

inline complex
polar(double r, double t)
{
    return complex(r * cos(t), r * sin(t));
}

inline complex
conj (const complex& x)
{
    return complex(real(x), -imag(x));
}

inline double
norm(const complex& x)
{
    return real(x) * real(x) + imag(x) * imag(x);
}

#endif

2. String类和测试程序

2.1 String-test.cpp

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

using namespace std;

int main()
{
    String s1("hello");
    String s2("world");

    String s3(s2);
    cout << s3 << endl;

    s3 = s1;
    cout << s3 << endl;
    cout << s2 << endl;
    cout << s1 << endl;
}

2.2 String.h

#ifndef MYSTRING_H
#define MYSTRING_H

#include <cstring>
#include <iostream>

class String
{
public:
    String(const char* cstr = 0);
    String(const String& str);
    String& operator = (const String& str);
    ~String();
    char* get_c_str() const
    {
        return m_data;
    }

private:
    char* m_data;
};

inline String::String(const char* cstr)
{
    if (cstr)
    {
        m_data = new char[strlen(cstr) + 1];
        strcpy(m_data, cstr);
    }
    else
    {
        m_data = new char[1];
        *m_data = '\0';
    }
}

inline
String::~String()
{
    delete[] m_data;
}

inline
String::String(const String& str)
{
    m_data = new char[strlen(str.m_data) + 1];
    strcpy(m_data, str.m_data);
}

inline
String& String::operator=(const String& str)
{
    if(this == &str)
    {
        return *this;
    }

    delete[] m_data;
    m_data = new char[strlen(str.m_data) + 1];
    strcpy(m_data, str.m_data);
    return *this;
} 

ostream& operator<<(ostream& os, const String& str)
{
    os << str.get_c_str();
    return os;
}

#endif

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值