码迷,mamicode.com
首页 > 其他好文 > 详细

第八周 项目一 (3)

时间:2015-05-07 18:52:16      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:class

(3)定义一个定义完整的类(是可以当作独立的产品发布,成为众多项目中的“基础工程”)。这样的类在(2)的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d; c+d和d+c的结果为“将d视为实部为d的复数同c相加”,其他-、*、/运算符类似。

/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*dood luck
*文件名称:d.cpp
*作    者:张旺华
*完成日期:2015年4月29日
*版 本 号:v1.0
*
*/
#include<iostream>
using namespace std;
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    friend Complex operator+(const Complex &c1,const Complex &c2);
    friend Complex operator-(const Complex &c1,const Complex &c2);
    friend Complex operator*(const Complex &c1,const Complex &c2);
    friend Complex operator/(const Complex &c1,const Complex &c2);
    friend Complex operator+(const double &b,const Complex &c2);
    friend Complex operator-(const double &b,const Complex &c2);
    friend Complex operator*(const double &b,const Complex &c2);
    friend Complex operator/(const double &b,const Complex &c2);
    friend Complex operator+(const Complex &c1,const double &b);
    friend Complex operator-(const Complex &c1,const double &b);
    friend Complex operator*(const Complex &c1,const double &b);
    friend Complex operator/(const Complex &c1,const double &b);
    void display();
private:
    double real;
    double imag;
};
//下面定义成员函数
Complex operator+(const double &b,const Complex &c2)
{
    Complex c(b,0);
    return c+c2;
}
Complex operator-(const double &b,const Complex &c2)
{
    Complex c(b,0);
    return c-c2;
}
Complex operator*(const double &b,const Complex &c2)
{
    Complex c(b,0);
    return c*c2;
}
Complex operator/(const double &b,const Complex &c2)
{
    Complex c(b,0);
    return c/c2;
}
Complex operator+(const Complex &c1,const double &b)
{
    Complex c(b,0);
    return c1+c;
}
Complex operator-(const Complex &c1,const double &b)
{
    Complex c(b,0);
    return c1-c;
}
Complex operator*(const Complex &c1,const double &b)
{
    Complex c(b,0);
    return c1*c;
}
Complex operator/(const Complex &c1,const double &b)
{
    Complex c(b,0);
    return c1/c;
}
void Complex ::display()
{
    cout<<"("<<real<<","<<imag<<")"<<endl;
}
Complex operator+(const Complex &c1,const Complex &c2)
{
    Complex a;
    a.real=c1.real+c2.real;
    a.imag=c1.imag+c2.imag;
    return a;
}
Complex operator-(const Complex &c1,const Complex &c2)
{
    Complex a;
    a.real=c1.real-c2.real;
    a.imag=c1.imag-c2.imag;
    return a;
}
Complex operator*(const Complex &c1,const Complex &c2)
{
    Complex a;
    a.real=c1.real*c2.real+c1.imag*c2.imag;
    a.imag=c1.imag+c2.real+c1.real*c2.imag;
    return a;
}
Complex operator/(const Complex &c1,const Complex &c2)
{
    Complex a;
    double k;
    k=1/(c2.real*c2.real+c2.imag*c2.imag);
    a.real=(c1.real*c2.real+c1.imag*c2.imag)*k;
    a.imag=(c1.imag+c2.real-c1.real*c2.imag)*k;
    return a;
}

//下面定义用于测试的main()函数
int main()
{
     Complex c1(3,4),c2(5,-10),c3;
    double d=11;
    cout<<"c1=";
    c1.display();
    cout<<"c2=";
    c2.display();
    cout<<"d="<<d<<endl<<endl;
    cout<<"下面是重载运算符的计算结果: "<<endl;
    c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    cout<<"c1+d=";
    (c1+d).display();
    cout<<"d+c1=";
    (d+c1).display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    cout<<"c1-d=";
    (c1-d).display();
    cout<<"d-c1=";
    (d-c1).display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    cout<<"c1*d=";
    (c1*d).display();
    cout<<"d*c1=";
    (d*c1).display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    cout<<"c1/d=";
    (c1/d).display();
    cout<<"d/c1=";
    (d/c1).display();
    return 0;
}
运行结果:
技术分享

学习心得:

一直想找出一种比较简单的来实现重载,一直找不到,就先用这种方法吧,随着后来的学习会知道的。


第八周 项目一 (3)

标签:class

原文地址:http://blog.csdn.net/wh201458501106/article/details/45564115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!