Chapter III Experiments

Keywords: P4

The purpose and requirements of the experiment

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

2. Timing and sequence of calling constructors and destructors.

3. To grasp the definition of the object and the timing and method of its initialization.

Experimental content

1. The following program sy3_1.cpp has errors with ERROR statements. Without deleting and adding lines of code, correct the erroneous statements to make them run correctly.

The operation procedure is as follows:

  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. }  
The mistakes are as follows:
The amendment procedure is as follows:
  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();  
  17.     return 0;  
  18. }  
The results are as follows:


2. Debugging the following procedures:
  1. #include<iostream>    
  2. using namespace std;    
  3. class TPoint    
  4. {    
  5. public:    
  6.     TPoint(int x=0,int y=0){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.     TPoint p4,p5(2);    
  26.     cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";    
  27.     return 0;    
  28. }    

In the program, the constructor of TPoint class with two parameters is modified, and the following statement is added to the body of the function:

      cout<<"Constructor is called\n";

(1) Write out the output result of the program and explain the output result;

The procedure is as follows:
  1. #include<iostream>  
  2. using namespace std;  
  3. class TPoint  
  4. {  
  5. public:  
  6.     TPoint(int x=0,int y=0){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<<"Constructor is called\n";  
  19.     cout<<"Copy-initialization Constructor is called\n";  
  20. }  
  21. int main()  
  22. {  
  23.     TPoint p1(4,9);  
  24.     TPoint p2(p1);  
  25.     TPoint p3=p2;  
  26.     TPoint p4,p5(2);  
  27.     cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";  
  28.     return 0;  
  29. }  

The results are as follows:

(2) Conduct debugging according to the following requirements:

In the main function body, add the following statement:

      TPoint p4,p5(2)cpp] view plai

  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.     cout<<"Constructor is called\n";    
  20. }    
  21. int main()    
  22. {    
  23.     TPoint P4,P5(2);    
  24.     TPoint p1(4,9);    
  25.     TPoint p2(p1);    
  26.     TPoint p3=p2;    
  27.     cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";    
  28.     return 0;    
  29. }    

What will happen to the debugger? Why? How to solve it? (Tip: Modify the existing constructors appropriately) Analyse how to use different constructors to create different objects combined with the results of operation.

Phenomenon:

Why: Because there is no constructor defined in the class without parameters and with one parameter;

How to solve this problem: changing the constructor with two parameters to the default constructor is to change TPoint(int x,int y) to TPoint(int x=0,int y=0); during the operation, TPoint p1(4,9) and TPoint p4,p5(2); calling the constructor, while TPoint p2(p1) and TPoint p3=p2 are using copy constructors. As follows:

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

Result:

3. Make the following modifications to the main function of Li3_11.cpp in the textbook:

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

(2) After the statement pa2=new Heapclass, add the statement pa3=new Heapclass(5);

(3) Change the statement if (! PA1 |! Pa2) to if (! PA1 |! Pa2 |! Pa3)

(4) After the statement delete pa2, add the statement delete pa3;

Write out the output of the program and explain the output.

The procedure is as follows:

  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 Mcmory!"<<endl;  
  34.        return 0;  
  35.    }  
  36.    cout<<"Exit main"<<endl;  
  37.    delete pa1;  
  38.    delete pa2;  
  39.    delete pa3;  
  40.    return 0;  
  41. }  

The results are as follows:


Interpretation: Pa1, pa2, and pa3 are two object pointers to Heapclass class. When they can be allocated enough memory, they are assigned values using the operator new, and the objects they refer to are initialized. Release the objects pointed to by these three pointers using delete, and output "Out of Memory" because pa1, pa2, or pa3 cannot be allocated enough memory.

4. Define a Rectangle class. Private data members are rectangular lengths and wids. Parametric constructors set len and wid to 0. Parametric constructors set len and wid to corresponding shape parameters. In addition, it includes finding rectangular circumference, rectangular area, rectangular length and width, modifying rectangular length and width to corresponding shape parameters, and outputting rectangular ruler. Public member functions such as inch. The format of output rectangular size is required to be "length: length, width: width".
The procedure is as follows:
  1. #include<iostream>    
  2. using namespace std;    
  3. class Rectangle    
  4. {    
  5. public:    
  6.    Rectangle()    
  7.    {    
  8.        len=0;    
  9.        wid=0;    
  10.    }    
  11.    Rectangle(double Len,double Wid)    
  12.    {    
  13.        len=Len;    
  14.        wid=Wid;    
  15.     }    
  16.    double Circumference()    
  17.    {    
  18.        return 2*(len+wid);    
  19.     }    
  20.    double Area()    
  21.    {    
  22.        return len*wid;    
  23.     }    
  24.    double getl()    
  25.    {    
  26.        return len;    
  27.     }    
  28.    double getw()    
  29.    {    
  30.        return wid;    
  31.     }    
  32.    void charge(double a,double b)    
  33.    {    
  34.        len=a;    
  35.        wid=b;    
  36.     }    
  37.    void s()    
  38.    {    
  39.        cout<<"length:"<<len<<"  "<<"width:"<<wid<<endl;    
  40.     }    
  41. private:    
  42.     int len,wid;    
  43. };    
  44. int main()    
  45. {    
  46.   Rectangle q;    
  47.   Rectangle h(5.0,2.0);    
  48.   cout<<"q Rectangular size:"<<endl;    
  49.   q.s();    
  50.   cout<<"h Rectangular size:"<<endl;    
  51.   h.s();    
  52.   cout<<"h Circumference:"<<h.Circumference()<<endl;    
  53.   cout<<"h Area:"<<h.Area()<<endl;    
  54.   cout<<"h Length:"<<h.getl()<<endl;    
  55.   cout<<"h Width:"<<h.getw()<<endl;    
  56.   h.charge(8.0,6.0);    
  57.   cout<<"Modified rectangle size:"<<endl;    
  58.    h.s();    
  59.    return 0;    
  60. }    

The results are as follows:


Analysis and discussion

1. Access rights of private members in a class.

Answer: Private members are hidden data that can only be referenced by member functions or friend functions of the class.

2. Calling order of constructor and destructor.

Answer: Constructors are called when creating objects, destructors are called when releasing objects, releasing memory allocated by constructors, and constructors are invoked in exactly the opposite order as destructors.

3. When does object initialization take place? How to proceed? (Tip: Pay attention to the discussion of general objects and heap objects)

Answer: General objects: When creating objects, they can be initialized with constructors or copy functions.

Heap object: use operator new to allocate memory and call constructor to initialize.

Posted by paruby on Thu, 16 May 2019 14:33:20 -0700