Lab 3 Constructor and Destructor

1. Experimental purpose and requirements
1. Familiar with the definition format of the class and the access rights of the 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.
2. Experimental content

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

//sy3_1.cpp
#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;
}

The results are as follows:

The modification procedure is as follows:

//sy3_1.cpp
#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();//ERROR
return 0;
}
The correct program runs as follows:


2. Debug the following program.
//sy3_2.cpp
#include<iostream>
using namespace std;
class TPoint
{
    public:
    TPoint(int x,int y){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;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return 0;
}

 The result of running the program is as follows:


  In this 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:

//sy3_2.cpp
#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 result of running the modified program is as follows:


(2) Debug according to the following requirements:

        In the main function body, 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.

The following phenomena will occur:


Cause: No constructor for this type is defined.

Solution: Change TPoint(int x,int y) to TPoint(int x=0,int y=0); that is, during operation, TPoint p1(4,9) and TPoint p4,p5(2); call The constructor is used, while TPoint p2(p1) and TPoint p3=p2 use the copy constructor.

3. Modify the main function of Li3_11.cpp in the textbook as follows:
(1) Change Heapclass *pa1,*pa3 to Heapclass *pa1,*pa2,*pa3;
(2) Add a statement after the statement pa2=new Heapclass; pa3=new Heapclass(5);
(3) Change the statement if(!pa1||!pa2) to if(!pa1||!pa2||!pa3);
(4) Add the statement delete after the statement delete pa2; pa3;

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 Memory!"<<endl;
    return 0;
}
cout<<"Exit main"<<endl;
delete pa1;
delete pa2;
delete pa3;
return 0;
}

The results are as follows:


4. Please define a rectangle class (Rectangle), the private data members are the length (len) and the width (wid) of the rectangle, the parameterless constructor sets len and wid to 0, and the parameterized constructor sets len and wid as the corresponding formal parameters It also includes public member functions such as finding the perimeter of the rectangle, finding the area of ​​the rectangle, taking the length and width of the rectangle, modifying the length and width of the rectangle as the values ​​of the corresponding formal parameters, and outputting the size of the rectangle. The format required to output rectangle dimensions is "length: length, width: width". (sy3_3.cpp)

Write the program as follows:

//sy3_3.cpp
#include<iostream>
using namespace std;
class Rectangle
{
    public:
    Rectangle(){len=0;wid=0;}
    Rectangle(double Len,double Wid){len=Len;wid=Wid;}
    double Circumference(){return 2*(len+wid);}
    double Area(){return len*wid;}
    double getl(){return len;}
    double getw(){return wid;}
    void charge(double a,double b){len=a;wid=b;}
    print(){cout<<"length:"<<len<<"width:"<<wid;}
    private:
    int len,wid;
};
intmain()
{
Rectangle p1;
Rectangle p2(4.0,5.0);
cout<<"Rectangle size of p1:";
p1.print();
cout<<"Rectangle size of p2:";
p2.print();
cout<<"p2周长:"<<p2.Circumference()<<endl;
cout<<"p2面积:"<<p2.Area()<<endl;
cout<<"p2的长度:"<<p2.getl()<<endl;
cout<<"p2的宽度:"<<p2.getw()<<endl;
p2.charge(5.0,6.0);

cout<<"modified rectangle size";
p2.print();
return 0;
}

The result of running the program is as follows:


Third, analysis and discussion
1, the access rights of private members in the class.
  Answer: Only functions in the class can access private members in the class

2. The calling sequence of constructor and destructor.

 Answer: In general, the order in which destructors are called is opposite to the order in which constructors are called; the first constructor called, its corresponding destructor is called last, and the last constructor called, its corresponding destructor is called last. The destructor is called first.

3. When to initialize objects? how to proceed? (Hint: pay attention to the discussion of general objects and heap objects)

 Answer: General object: Initialize the object when it is created, you can use the constructor or the copy constructor to initialize it.

Fourth, the experimental summary

      This experiment is about constructors and destructors. First, we need to be familiar with the definition format of the class and the access rights of members in the class. Then we need to understand the calling timing and sequence of constructors and destructors. Finally, we need to master the definition of objects. And the timing and method of object initialization. The procedures involved this time are a bit difficult. It is really not easy to understand each program clearly. Some problems cannot be solved by myself. I have checked some information on the Internet for the programs I don’t understand, but the programs that are too complicated require More time to figure out, this also means that I know that my programming ability still needs to be greatly improved, and I will try my best to change my shortcomings in the next study.



Guess you like

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