Experiment purpose and requirements
1. Be familiar with the definition format of the class and the access rights of the members in the class.
2. Call timing and sequence of constructor and destructor.
3. Master the definition of object and the timing and method of object initialization.
Experimental content
1. The following procedure sy3_ 1. If the statement marked with ERROR in CPP is wrong, correct the wrong statement and make it run correctly without deleting and adding code lines.
The operation 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; }; int main() { Aa a1(1),a2(2); a1.print(); cout<<a2.a<<endl;//ERROR return 0; }
The error results are as follows:
The correct 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; }; int main() { Aa a1(1),a2(2); a1.print(); a2.print(); return 0; }
The operation results are as follows:
2. Debug the following procedures.
//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=p.Y; cout<<"Copy-initialization Constructor is called\n"; } int main() { 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 TPoint class, and add the following statement in the function body:
cout<<"Constructor is called\n";
(1) Write the output result of the program and explain the output result;
The procedure is as follows:
//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 = p.Y; cout << "Constructor is called\n"; cout << "Copy-initialization Constructor is called\n"; } int main() { TPoint p1(4, 9); TPoint p2(p1); TPoint p3 = p2; cout << "p3=(" << p3.getx() << "," << p3.gety() << ")\n"; return 0; }
The results are as follows:
(2) Commissioning shall be carried out according to the following requirements:
In the main function body, add the following description statement:
TPoint p4,p5(2);
What happens to the debugger? Why? How to solve it? (tip: modify the existing constructors appropriately) analyze how to use different constructors to create different objects in combination with the running results.
Phenomena:
Error reason: because there is no constructor with no parameters and one parameter defined in the class;
The analysis is as follows: if you want to solve this problem, you need to change the constructor with two parameters to the default constructor, that is, you can change TPoint(int x,int y) to TPoint(int x=0,int y=0); During operation, TPoint p1(4,9) and TPoint p4,p5(2); The constructor is called, and TPoint p2(p1) and TPoint p3=p2 use the copy constructor.
3. On Li3 in teaching materials_ 11. The main function of CPP is modified 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 delete pa3 after delete pa2;
Write the output of the program and explain the output.
The procedure is as follows:
//sy3_3.cpp #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; } int main() { 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 operation results are as follows:
Analysis results: in the program, three pointers * pa1,*pa2,*pa3 are applied to point to the newly created object, and a constructor without construction parameters is called to initialize the object, so the first three statements are output; After the experiment, the destructor is called, and then the output results of the last three sentences are generated.
4, Please define a Rectangle class. The private data members are Rectangle length (len) and width (wid), the nonparametric constructor sets len and wid to 0, and the parametric constructor sets len and wid to the values of the corresponding formal parameters. In addition, it also includes public member functions such as calculating the perimeter of the Rectangle, calculating the area of the Rectangle, taking the length and width of the Rectangle, modifying the length and width of the Rectangle to the values of the corresponding formal parameters, and outputting the rectangular size. The format of outputting the rectangular size is Length: length, width: width. (sy3_4.cpp)
The experimental procedure is as follows:
//sy3_4.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;} printf() { cout<<"length:"<<len<<endl; cout<<"width:"<<wid<<endl; } private: int len,wid; }; int main() { Rectangle p1; Rectangle p2(6.0,8.0); cout<<"p1 Rectangle size:\n"; p1.printf(); cout<<"p2 Rectangle size:\n"; p2.printf(); cout<<"p2 Perimeter of:"<<p2.Circumference()<<endl; cout<<"p2 Area:"<<p2.Area()<<endl; cout<<"p2 Length of:"<<p2.getl()<<endl; cout<<"p2 Width of:"<<p2.getw()<<endl; p2.charge(5.0,6.0); cout<<"Size of the modified rectangle:"; p2.printf(); return 0; }
Operation results:
Analysis and discussion
1. Access rights of private members in class;
Only functions in a class can access private members in a class.
2. Call order of constructor and destructor;
When an object is created, it is initialized with a constructor first. After the program ends, the destructor is used to release the memory allocated by the constructor, and the execution order of the destructor is just opposite to that of the constructor.
3. When to initialize objects? How? (Note: the discussion should be divided into general objects and heap objects)
General object: initialize the object when it is created. You can initialize it with constructor or copy constructor.
Heap object: allocate space with new, and then call the constructor for initialization.
Experimental summary
Through this experiment, I have a preliminary understanding and mastery of the definition format of the class and the access rights of the members in the class, the call and order of constructors and destructors, the definition of objects, and the timing and methods of object initialization; Also learned some new knowledge and methods, and learned the call order of constructor and destructor and how to initialize the object. At the same time, I learned some new ideas and methods of writing programs in the process of constantly modifying and reading programs.