实验三:构造函数与析构函数
一、实验目的和要求
1、熟悉类的定义格式和类中成员的访问权限。
2、构造函数与析构函数的调用时机与顺序。
3、掌握对象的定义以及对象的初始化的时机与方法。
二、实验内容
1、下面程序sy3_1.cpp中用ERROR表明的语句有错,在不删除和增加代码行的情况下,改正错误语句,使其正确运行。
- #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;
- }
修改:
- #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();//ERROR
- return 0;
- }
2、调试下列程序。
- #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<<"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;
- }
(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;
写出程序的输出结果,并解释输出结果。
- #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 Memory!"<<endl;
- return 0;
- }
- cout<<"Exit main"<<endl;
- delete pa1;
- delete pa2;
- delete pa3;
- return 0;
- }
4、请定义一个矩形类(Rectangle),私有数据成员为矩形的长度(len)和宽带(wid),无参构造函数置len和wid为0,有参构造函数置len和wid为对应形参的值,另外还包括求矩形周长、求矩形面积、取矩形长度和宽度、修改矩形长度和宽度为对应形参的值、输出矩形尺寸等公有成员函数。要求输出矩形尺寸的格式为“length:长度,width:宽度”。(sy3_3.cpp)
- /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;
- };
- int main()
- {
- Rectangle p1;
- Rectangle p2(4.0,5.0);
- cout<<"p1的矩形尺寸:";
- p1.print();
- cout<<"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<<"修改后的矩形尺寸";
- p2.print();
- return 0;
- }