Experiment 3 Constructor and Destructor

Experimental purpose and requirements

  1. Familiar with the definition format of a class and the access rights of members in the class.

  2. Timing and sequence of calling constructors and destructors.

  3. Master the definition of objects and the timing and methods of initialization of objects.

Experimental content

  1. The statement marked with ERROR in the following program sy3_1.cpp is wrong. Without deleting and adding lines of code, correct the erroneous statement to make it run correctly.

Run the program as follows:

#include<iostream>  
using namespace std;  
class Aa  
{  
  public:  
      Aa(int i=0){a=i;cout<<"Constructor"<<a<<endl;}  
      ~Aa(){cout<<"Destructor"<<a<<endl;}  
      void print(){cout<<a<<endl;}  
  private:  
    int a;  
};  
intmain()  
{  
    Aa a1(1),a2(2);  
    a1.print();  
   cout<<a2.a<<endl;//ERROR  
    return 0;  
}

Error message:

Modify as follows:

#include<iostream>  
using namespace std;  
class Aa  
{  
  public:  
      Aa(int i=0){a=i;cout<<"Constructor"<<a<<endl;}  
      ~Aa(){cout<<"Destructor"<<a<<endl;}  
      void print(){cout<<a<<endl;}  
  private:  
    int a;  
};  
intmain()  
{  
    Aa a1(1),a2(2);  
    a1.print();  
    a2.print();  
    return 0;  
}

operation result:


2. Debug the following programs:

#include<iostream>    
using namespace std;    
class TPoint    
{    
public:    
    TPoint(int x=0,int y=0){X=x,Y=y;}    
    TPoint(TPoint &p);    
    ~TPoint(){cout<<"Destructor is called\n";}    
    int getx(){return X;}    
    int gety(){return Y;}    
private:    
    int X,Y;    
};    
TPoint::TPoint(TPoint &p)    
{    
    X=p.X;    
    Y=pY;    
    cout<<"Copy-initialization Constructor is called\n";    
}    
intmain()    
{    
    TPoint p1(4,9);    
    TPoint p2(p1);    
    TPoint p3=p2;    
    TPoint p4,p5(2);    
    cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";    
    return 0;    
}    

In the program, modify the constructor with two parameters of the TPoint class, and add the following statement in the function body:

      cout<<"Constructor is called\n";

(1) Write the output of the program and interpret the output;

The procedure is as follows:
#include<iostream>    
using namespace std;    
class TPoint    
{    
public:    
    TPoint(int x=0,int y=0){X=x,Y=y;}    
    TPoint(TPoint &p);    
    ~TPoint(){cout<<"Destructor is called\n";}    
    int getx(){return X;}    
    int gety(){return Y;}    
private:    
    int X,Y;    
};    
TPoint::TPoint(TPoint &p)    
{    
    X=p.X;    
    Y=pY;    
    cout<<"Copy-initialization Constructor is called\n";    
}    
intmain()    
{    
    TPoint p1(4,9);    
    TPoint p2(p1);    
    TPoint p3=p2;    
    TPoint p4,p5(2);    
    cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";    
    return 0;    
}  

In the program, modify the constructor with two parameters of the TPoint class, and add the following statement in the function body:

      cout<<"Constructor is called\n";

(1) Write the output of the program and interpret the output;

The procedure is as follows:
#include<iostream>  
using namespace std;  
class TPoint  
{  
public:  
    TPoint(int x=0,int y=0){X=x,Y=y;}  
    TPoint(TPoint &p);  
    ~TPoint(){cout<<"Destructor is called\n";}  
    int getx(){return X;}  
    int gety(){return Y;}  
private:  
    int X,Y;  
};  
TPoint::TPoint(TPoint &p)  
{  
    X=p.X;  
    Y=pY;  
    cout<<"Constructor is called\n";  
    cout<<"Copy-initialization Constructor is called\n";  
}  
intmain()  
{  
    TPoint p1(4,9);  
    TPoint p2(p1);  
    TPoint p3=p2;  
    TPoint p4,p5(2);  
    cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";  
    return 0;  
}  

The results are as follows:


(2) Debug according to the following requirements:

       In the body of the main function, add the following statement:

        TPoint p4,p5(2);

What happens to the debugger? Why? How to solve? (Hint: make appropriate modifications to the existing constructors) Combine the running results to analyze how to use different constructors to create different objects.

Phenomenon:

why:

How to solve:

3. Modify the main function of Li3_11.cpp in the textbook as follows:

(1) Change Heapclass *pa1,*pa2 to Heapclass *pa1,*pa2,*pa3;

(2) Add the statement pa3=new Heapclass(5) after the statement pa2=new Heapclass;

(3) Change the statement if(!pa1||!pa2) to if(!pa1||!pa2||!pa3)

(4) Add the statement delete pa3 after the statement delete pa2;

Write the output of the program and interpret the output.

The procedure is as follows:

#include<iostream>  
using namespace std;  
class Heapclass  
{  
public:  
   Heapclass(int x);  
   Heapclass();  
   ~Heapclass();  
private:  
    int i;  
};  
Heapclass::Heapclass(int x)  
{  
    i=x;  
    cout<<"Contstructor is called. "<<i<<endl;  
}  
Heapclass::Heapclass()  
{  
    cout<<"Default Contstructor is called."<<endl;  
}  
Heapclass::~Heapclass()  
{  
 cout<<"Default is called."<<endl;  
}  
intmain()  
{  
   Heapclass *pa1,*pa2,*pa3;  
   pa1=new Heapclass(4);  
   pa2=new Heapclass;  
   pa3=new Heapclass(5);  
   if(!pa1||!pa2||!pa3)  
   {  
       cout<<"Out of Mcmory!"<<endl;  
       return 0;  
   }  
   cout<<"Exit main"<<endl;  
   delete pa1;  
   delete pa2;  
   delete pa3;  
   return 0;  
}

The results are as follows:

explain:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810278&siteId=291194637