实验3,构造函数与析构函数

一、实验目的和要求

1、熟悉类的定义格式和类中成员的访问权限。

2、构造函数与析构函数的调用时机与顺序。

3、掌握对象的定义以及对象的初始化的时机与方法。

二、实验内容

1、下面程序sy3_1.cpp中用ERROR表明的语句有错,在不删除和增加代码行的情况下,改正错误语句,使其正确运行。

  1. #include<iostream>  
  2. using namespace std;  
  3. class Aa  
  4. {  
  5.     public:  
  6.     Aa(int i =0){a=i;cout<<"Constructor"<<a<<endl;}  
  7.     ~Aa(){cout<<"Destructor"<<a<<endl;}  
  8.     void print(){cout<<a<<endl;}  
  9.     private:  
  10.     int a;  
  11. };  
  12. int main()  
  13. {  
  14. Aa a1(1),a2(2);  
  15. a1.print();  
  16. cout<<a2.a<<endl;//ERROR  
  17. return 0;  
  18. }  
运行显示错误:


修改:

  1. #include<iostream>  
  2. using namespace std;  
  3. class Aa  
  4. {  
  5.     public:  
  6.     Aa(int i =0){a=i;cout<<"Constructor"<<a<<endl;}  
  7.     ~Aa(){cout<<"Destructor"<<a<<endl;}  
  8.     void print(){cout<<a<<endl;}  
  9.     private:  
  10.     int a;  
  11. };  
  12. int main()  
  13. {  
  14. Aa a1(1),a2(2);  
  15. a1.print();  
  16. a2.print();//ERROR  
  17. return 0;  
  18. }  

2、调试下列程序。

  1. #include<iostream>  
  2. using namespace std;  
  3. class TPoint  
  4. {  
  5.     public:  
  6.     TPoint(int x,int y){X=x;Y=y;}  
  7.     TPoint(TPoint &p);  
  8.     ~TPoint(){cout<<"Destructor is called\n";}  
  9.     int getx(){return X;}  
  10.     int gety(){return Y;}  
  11.     private:  
  12.     int X,Y;  
  13. };  
  14. TPoint::TPoint(TPoint &p)  
  15. {  
  16. X=p.X;  
  17. Y=p.Y;  
  18. cout<<"Copy-initialization Constructor is called\n";  
  19. }  
  20. int main()  
  21. {  
  22. TPoint p1(4,9);  
  23. TPoint p2(p1);  
  24. TPoint p3=p2;  
  25. cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";  
  26. return 0;  
  27. }  

(2)按下列要求进行调试:

        在主函数体内,添加下列说明语句:

        TPoint P4,P5(2);

调试程序会出现什么现象?为什么?如何解决?(提示:对已有的构造函数进行适当修改)结合运行结果分析如何使用不同的构造函数创建不同的对象。

3、对教材中Li3_11.cpp的主函数做如下修改:

(1)将Heapclass *pa1,*pa3改为Heapclass *pa1,*pa2,*pa3;

(2)在语句pa2=new Heapclass;后增加语句pa3=new Heapclass(5);

(3)在语句if(!pa1||!pa2)改为if(!pa1||!pa2||!pa3);

(4)在语句delete pa2;后增加语句delete pa3;

写出程序的输出结果,并解释输出结果。

  1. #include<iostream>  
  2. using namespace std;  
  3. class Heapclass  
  4. {  
  5.     public:  
  6.     Heapclass(int x);  
  7.     Heapclass();  
  8.     ~Heapclass();  
  9.     private:  
  10.     int i;  
  11. };  
  12. Heapclass::Heapclass(int x)  
  13. {  
  14.     i=x;  
  15.     cout<<"Contstructor is called."<<i<<endl;  
  16. }  
  17. Heapclass::Heapclass()  
  18. {  
  19. cout<<"Default Contstructor is called."<<endl;  
  20. }  
  21. Heapclass::~Heapclass()  
  22. {  
  23. cout<<"Default is called."<<endl;  
  24. }  
  25. int main()  
  26. {  
  27. Heapclass *pa1,*pa2,*pa3;  
  28. pa1=new Heapclass(4);  
  29. pa2=new Heapclass;  
  30. pa3=new Heapclass(5);  
  31. if(!pa1||!pa2||!pa3);  
  32. {  
  33.     cout<<"Out of Memory!"<<endl;  
  34.     return 0;  
  35. }  
  36. cout<<"Exit main"<<endl;  
  37. delete pa1;  
  38. delete pa2;  
  39. delete pa3;  
  40. return 0;  
  41. }  


4、请定义一个矩形类(Rectangle),私有数据成员为矩形的长度(len)和宽带(wid),无参构造函数置len和wid为0,有参构造函数置len和wid为对应形参的值,另外还包括求矩形周长、求矩形面积、取矩形长度和宽度、修改矩形长度和宽度为对应形参的值、输出矩形尺寸等公有成员函数。要求输出矩形尺寸的格式为“length:长度,width:宽度”。(sy3_3.cpp)

  1. /sy3_3.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  4. class Rectangle  
  5. {  
  6.     public:  
  7.     Rectangle(){len=0;wid=0;}  
  8.     Rectangle(double Len,double Wid){len=Len;wid=Wid;}  
  9.     double Circumference(){return 2*(len+wid);}  
  10.     double Area(){return len*wid;}  
  11.     double getl(){return len;}  
  12.     double getw(){return wid;}  
  13.     void charge(double a,double b){len=a;wid=b;}  
  14.     print(){cout<<"length:"<<len<<"width:"<<wid;}  
  15.     private:  
  16.     int len,wid;  
  17. };  
  18. int main()  
  19. {  
  20. Rectangle p1;  
  21. Rectangle p2(4.0,5.0);  
  22. cout<<"p1的矩形尺寸:";  
  23. p1.print();  
  24. cout<<"p2的矩形尺寸:";  
  25. p2.print();  
  26. cout<<"p2周长:"<<p2.Circumference()<<endl;  
  27. cout<<"p2面积:"<<p2.Area()<<endl;  
  28. cout<<"p2的长度:"<<p2.getl()<<endl;  
  29. cout<<"p2的宽度:"<<p2.getw()<<endl;  
  30. p2.charge(5.0,6.0);  
  31.   
  32. cout<<"修改后的矩形尺寸";  
  33. p2.print();  
  34. return 0;  
  35. }  


三、分析与讨论

1、类中私有成员的访问权限。

2、构造函数与析构函数的调用顺序。

3、何时进行对象初始化?如何进行?(提示:注意分一般对象与堆对象讨论)

四、实验小结

         通过本次实验,我感觉自己收获还是很大的,通过实验熟悉了类的定义格式和类中成员的访问权限,还知道了构造函数和析构函数的调用时机与顺序以及知道了对象的定义和对象的初始化的时机与方法。


猜你喜欢

转载自blog.csdn.net/shenyang_666/article/details/79955186
今日推荐