写过的真正意义上的第一个C++程序

新浪微博 QQ空间

博客的发布记录为:
By YANQUN | Published: 二月 24, 2008 | Edit
 
记得当时刚过完年回到学校,看了一个寒假的书,迫不及待的想实践一下,于是参照《Exceptional C++》上面反复拿出来举例的Complex类,自己重新写了一个,虽然不是独创的。首先规划好了想要实现哪些接口,然后一条一条地到《C++ Primer》中去找相应的注意事项和一般约定。整个代码几乎是自己独立写出的。后来在面试过程中,还重新将这份代码又写了一遍提交给考官,呵呵,当时给他的感觉估计还好,但是他哪知道我只会这几行代码 :-)
 
感觉现在写代码的风格几乎都是那个时候定型的。喜欢比较整洁的风格,看到乱七八糟的代码总有重构的冲动。

#include <iostream>
using namespace std;

class Complex
{
public:
Complex()
{
}//无形参的构造函数
Complex(double _r, double _i = 0) : _real(_r), _img(_i)
{
}//constructor fun.
Complex(const Complex&);//copy constructor fun.
Complex& operator=(const Complex&);//赋值操作符
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
Complex& operator/=(const Complex&);//复合赋值操作符
Complex& operator++(); //前缀式的自增操作符
Complex operator++(int);//后缀式
Complex& operator–();
Complex operator–(int);
ostream& ops(ostream&) const;
istream& ips(istream&);//输入操作需要更改类的数据,其辅助函数就不能定义为const的
~Complex()
{
}
private:
double _real, _img;
};

//定义赋值操作符
Complex& Complex::operator=(const Complex& cr)
{
this->_real = cr._real;
this->_img = cr._img;
return *this;
}

//定义输入输出操作
ostream& Complex::ops(ostream& os) const
{
if (_img > 0)
{
return os << _real << " + " << _img << "i";
}
else if (_img == 0)
{
return os << _real;
}
else
{
return os << _real << " + " << "(" << _img << ")" << "i";
}
}

ostream& operator<<(ostream& os, const Complex& cr)
{
return cr.ops(os);
//借助于类的成员函数来实现操作符函数访问私有数据。
//由于这里是常引用,辅助的成员函数也应定义为const成员函数。
}

istream& Complex::ips(istream& os)
{
return os >> _real >> _img;
}

istream& operator>>(istream& os, Complex& cr)
//注意这里需要改变形参cr,因此不能将其定为const
{
return cr.ips(os);
//只有非const的对象才能调用非const的成员函数。
}

//定义复制构造函数
Complex::Complex(const Complex& rhs)
{
_real = rhs._real;
_img = rhs._img;
//注意这里直接访问类中的数据成员,而不是通过对象来调用
}

//定义相应的复合赋值运算
Complex& Complex::operator+=(const Complex& cl)
{
_real += cl._real;
_img += cl._img;
return *this;
}

Complex& Complex::operator-=(const Complex& cl)
{
_real -= cl._real;
_img -= cl._img;
return *this;
}

Complex& Complex::operator*=(const Complex& cl)
{
double _t = _real;
_real = (_real * cl._real – _img * cl._img);
_img = (_t * cl._img + _img * cl._real);
return *this;
}

Complex& Complex::operator/=(const Complex& cr)
{
double _r = _real, _i = _img, _m = cr._real* cr._real + cr._img* cr._img;
if (_m == 0)
cout << "Error" << endl;
else
{
_real = (_r * cr._real + _i * cr._img) / _m;
_img = (-_r * cr._img + _i * cr._real) / _m;
}
return *this;
}

//利用复合赋值操作来实现四则远算
Complex operator+(const Complex& cl, const Complex& cr)
{
return Complex(cl) += cr;
}

Complex operator-(const Complex& cl, const Complex& cr)
{
return Complex(cl) -= cr;
}

Complex operator*(const Complex& cl, const Complex& cr)
{
return Complex(cl) *= cr;
}

Complex operator/(const Complex& cl, const Complex& cr)
{
return Complex(cl) /= cr;
}

//先定义前缀式的,后缀式根据此操作实现
Complex& Complex::operator++()
{
++_real;
return *this;
}

Complex Complex::operator++(int)
{
Complex _ct(*this);
++(*this);
return _ct;
}

Complex& Complex::operator--()
{
--_real;
return *this;
}

Complex Complex::operator--(int)
{
Complex _ct(*this);
--(*this);
return _ct;
}

int main()
{
Complex c1(2, 1), c2(4, 3);
Complex c0 = 0;
Complex c4(c1);
Complex c5(2.4, 3.5);
c5 *= c2;
Complex c6;
Complex c3 = c1 + c2;
cout << "c1: " << c1 << endl;
cout << "c2: " << c2 << endl;
cout << "c3: " << c3 << endl;
cout << "c4: " << c4 << endl;
cout << "c5: " << c5 << endl;
cout << "c5++: " << c5++ << endl;
cout << "c5: " << c5 << endl;
cout << "++c5: " << ++c5 << endl;
cout << "--c5: " << --c5 << endl;
cout << "c5: " << c5 << endl;
cout << "c2/c1: " << c2 / c1 << endl;
cout << "c1*c2: " << c1 * c2 << endl;
cout << "c2-c1: " << c2 - c1 << endl;
cout << "c5+c1: " << c5 + c1 << endl;
cout << "Please input c6: ";
cin >> c6;
cout << "c6: " << c6 << endl;
return 0;
}

新浪微博 QQ空间

| 1 分2 分3 分4 分5 分 (4.75- 4票) Loading ... Loading ... | 这篇文章归档在:C/C++, 生活札记, 软件技术 | 标签: , , . | 永久链接:链接 | 评论(2) |

2 条评论

  1. shincepu
    评论于 四月 1, 2014 at 10:50:49 CST | 评论链接

    赞一个。
    提个小问题:
    //定义输入输出操作
    ostream& Complex::ops(ostream& os) const
    这里的else if 中double数直接和0做相等比较了,感觉应该处理下。

    • 评论于 四月 1, 2014 at 14:00:01 CST | 评论链接

      谢谢指出问题!浮点数精度问题,是需要修改一下。

回复 shincepu

邮箱地址不会被泄露, 标记为 * 的项目必填。

8 - 2 = *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <img alt="" src="" class=""> <pre class=""> <q cite=""> <s> <strike> <strong>

返回顶部