首页 > 编程语言 > 详细

C++:复数类构造函数、拷贝构造、运算符重载、析构函数

时间:2016-01-18 21:09:08      阅读:266      评论:0      收藏:0      [点我收藏+]
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;


class Complex
{
public:
    void Set(double real, double image)
    {
        _real = real;
        _image = image;
    }

    //构造函数
     Complex(double real = 1, double image = 2)
    {
         cout << "缺省构造函数" << endl;
        _real = real;
        _image = image;
    }

     //拷贝构造函数
     Complex(Complex& d)
     {
         _real = d._real;
         _image = d._image;
     }


     //析构函数
     ~Complex()
     {
         cout << "析构函数" << endl;
     }

     //

     void Display()
     {
         cout << _real << _image;
     }


     //等于
     bool operator==(const Complex& d)
     {
         return _real  * _real + _image * _image
             == d._real + d._real + d._image * d._image;
     }

     //小于
     bool operator < (const Complex& d)
     {
         return _real  * _real + _image * _image
             < d._real + d._real + d._image * d._image;
     }

     //大于
     bool operator > (const Complex& d)
     {
         return _real  * _real + _image * _image
             > d._real + d._real + d._image * d._image;
     }
private:
    double _real;
    double _image;
};


int main()
{
    Complex d1;
    d1.Display();
    return 0;
}


C++:复数类构造函数、拷贝构造、运算符重载、析构函数

原文:http://10740184.blog.51cto.com/10730184/1736129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!