【c++】實現一個複數類

來源:互聯網
上載者:User

標籤:c++   建構函式   拷貝構造   

// 實現一個複數類(complex)//要求:1:實現不帶參 / 帶參數 /帶預設參數的幾種建構函式。//      2:實現拷貝構造 / 解構函式。#include <iostream>using namespace std;// 帶參數的建構函式class complex{private:double _real;double _image;public:void print();complex(double _r, double _i);~complex();};complex::complex(double _r, double _i){_real = _r;_image = _i;cout<<"the parameterless constructor"<<endl;}/* 不帶參數的建構函式class complex{private:double _real;double _image;public:void print();complex();~complex();};complex::complex(){_real = 0;_image = 0;cout<<"with no arguments constructor"<<endl;}*//* 帶預設參數的建構函式class complex{private:double _real;double _image;public:void print();complex(double _real = 0,double _image = 0);~complex();};complex::complex(double _r, double _i){_real = _r;_image = _i;cout<<"with the default constructor"<<endl;}*//* 拷貝構造class complex{private:double _real;double _image;public:void print();complex(double _r, double _i){_real = _r;_image = _i;}complex(complex const & comple);~complex();};complex::complex(complex const & comple){_real = comple._real;_image = comple._image;cout<<"copy structure"<<endl;}*/complex::~complex(){cout << "destructor" << endl;}void complex::print(){cout << "複數是:" << _real << "+" <<_image << "i" << endl;}int main(){double a, b;//double a;cout << "請輸入兩個數:";cin >> a >> b;//cin >> a;//complex comp(a);complex comp(a,b);//complex comp(1, 2);//complex comp;//complex comp1(comp);comp.print();//comp1.print();return 0;}







【c++】實現一個複數類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.