實(shí)驗(yàn)3
驗(yàn)證性實(shí)驗(yàn)部分
在面向?qū)ο蟪绦蛟O(shè)計(jì)中,程序模塊是由類構(gòu)成的。類是對(duì)問(wèn)題的抽象描述,在類這種抽象機(jī)制中的一個(gè)特定實(shí)體即為對(duì)象。而利用特定的值去構(gòu)造對(duì)象,將對(duì)象初始化為特定狀態(tài)即為構(gòu)造函數(shù),使用已經(jīng)存在的對(duì)象去初始化同類的新對(duì)象即為復(fù)制構(gòu)造函數(shù),析構(gòu)函數(shù)則是用來(lái)完成生存期即將結(jié)束的對(duì)象在被刪除前的一些清理工作。
編輯實(shí)驗(yàn)部分
1
#include<iostream> using namespace std; class Rectangle{ public: Rectangle(float l,float w); Rectangle(Rectangle &r); float area(); ~Rectangle(){} private: float length,width; }; Rectangle::Rectangle(float l,float w){ length=l;width=w; } Rectangle::Rectangle(Rectangle &r){ length=r.length; width=r.width; } float Rectangle::area(){ return length*width; } int main(){ float length,width; cout<<"Enter the length and width of the rectangle:"; cin>>length>>width; Rectangle s(length,width); float areaofrectangle=s.area(); cout<<"The area of the rectangle is:"<<areaofrectangle<<endl; return 0; }

2
#include<iostream> using namespace std; class Complex{ public: Complex(double a,double b); Complex(double a); Complex(Complex &s); void add(Complex &c); void show(); ~Complex(){} private: double real,imaginary; }; Complex::Complex(double a,double b){ real=a; imaginary=b; } Complex::Complex(double a){ real=a; imaginary=0; } void Complex::add(Complex &c){ real=real+c.real; imaginary=imaginary+c.imaginary; } void Complex::show(){ cout<<real<<"+"<<imaginary<<"i"; } Complex::Complex(Complex &s){ real=s.real; imaginary=s.imaginary; cout<<"Calling the copy constructor"<<endl; } int main(){ Complex c1(3,5); Complex c2(4.5); c1.add(c2); c1.show(); return 0; }

實(shí)驗(yàn)總結(jié)與體會(huì)
學(xué)好計(jì)算機(jī)語(yǔ)言需要讀懂書本,多動(dòng)手
浙公網(wǎng)安備 33010602011771號(hào)