码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-01-18 21:09:11      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:c++   拷贝构造   复数类构造函数   运算符重载   析构函数   

#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++:复数类构造函数、拷贝构造、运算符重载、析构函数

标签:c++   拷贝构造   复数类构造函数   运算符重载   析构函数   

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

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