日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[YTU]_2625( 构造函数和析构函数)

發布時間:2025/5/22 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [YTU]_2625( 构造函数和析构函数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Description

在建立類對象時系統自動該類的構造函數完成對象的初始化工作,
當類對象生命周期結束時,系統在釋放對象空間之前自動調用析構函數。

此題要求:
根據主程序(main函數)和程序執行結果,結合構造函數和析構函數相關知識,在下面的程序段基礎上完成整個設計。

提示:(1)需要自定義復數類Complex,并在類中添加適當的構造函數和析構函數。
????????? (2)只提交begin到end部分的代碼

#include <iostream>
using namespace std;

//將程序需要的其他成份寫在下面,只提交begin到end部分的代碼
//******************** begin ********************


//********************* end ********************

int main()
{
double real,image;

cin>>real>>image;

Complex c1(real,image);

Complex c2=c1;

return 0;
}

程序輸入輸出樣例如 Sample Input 和 Sample Output 所示。

Input

?一個復數的實部和虛部

Output

?調用相關構造函數和析構函數的運行結果(需要自己分析),參照Sample Output 所示。

Sample Input

1.5 2.6

Sample Output

(1.5,2.6i) is constructed! (1.5,2.6i) is copy constructed! destructed! destructed!

HINT

(1)需要自定義復數類Complex,并在類中添加適當的構造函數和析構函數。

(2)只提交begin到end部分的代碼

#include <iostream> using namespace std; class Complex { public:Complex(double r,double i):real(r),image(i){cout<<'('<<real<<','<<image<<"i) is constructed!"<<endl;}~Complex(){cout<<"destructed!"<<endl;}Complex(const Complex &c); private:double real;double image;};Complex::Complex(const Complex &c){real=c.real;image=c.image;cout<<'('<<real<<','<<image<<"i) is copy constructed!"<<endl;} int main() {double real,image;cin>>real>>image;Complex? c1(real,image);Complex? c2=c1;return 0; }

轉載于:https://www.cnblogs.com/sxy201658506207/p/7586370.html

總結

以上是生活随笔為你收集整理的[YTU]_2625( 构造函数和析构函数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。