3.2 实训——构造函数与析构函数的应用
3.2.2 实训内容与步骤
1.思考:(1)#include <iostream>
using namespace std;
class Time{
private:
int hour,minute,second;
public:
void disp();
};
void Time::disp(){
cout<<hour<<"小时"<<minute<<"分钟"<<second<<"秒"<<endl;
}
int main(){
Time time;
time.disp();
}
答: 对象TIME的对象成员没有初始化,所以没有值输出
(2)#include <iostream>
using namespace std;
class Time{
private:
int hour,minute,second;
public:
void disp();
Time();
};
void Time::disp(){
cout<<hour<<"小时"<<minute<<"分钟"<<second<<"秒"<<endl;
}
Time::Time(){
}
int main(){
Time time;
time.disp();
}
答:有,进行函数的初始化。结果无变化。
(3)#include <iostream>
using namespace std;
class Time{
private:
int hour,minute,second;
public:
void disp();
Time();
};
void Time::disp(){
cout<<hour<<"小时"<<minute<<"分钟"<<second<<"秒"<<endl;
}
Time::Time(){
hour=0;
minute=0;
second=0;
}
int main(){
Time time;
time.disp();
}
输出结果:0小时0分钟0秒
(4)#include <iostream>
using namespace std;
class Time{
private:
int hour,minute,second;
public:
void disp();
Time(int hour,int minute,int second);
};
void Time::disp(){
cout<<hour<<"小时"<<minute<<"分钟"<<second<<"秒"<<endl;
}
Time::Time(int hour,int minute,int second){
hour=hour;
minute=minute;
second=second;
}
int main(){
Time time(1,2,3);
time.disp();
}
仍然没有任何输出
2. #include<iostream>
using namespace std;
class student{
private:
int stunum;
};
int main(){
student s;
return 0;
}
没有调用构造函数
(1). #include<iostream>
using namespace std;
class student{
public:
student(){}
private:
int stunum;
};
int main(){
student s;
return 0;
}
输出结果还是一样的,但这一次调用了构造函数
(2). #include<iostream>
using namespace std;
class student{
public:
student(int num){
stunum=num;
cout<<"用户写的构造函数被调用"<<endl;
}
private:
int stunum;
};
int main(){
student stu(1001);
return 0;
}
输出结果:
用户写的构造函数被调用
(3)#include<iostream>
using namespace std;
class student{
private:
int stunum;
public:
student(int num){
stunum=num;
cout<<"用户写的构造函数被调用"<<endl;
}
student(){
cout<<"用户写的无参构造函数被调用"<<endl;
}
};
int main(){
student stu(1002);
student s;
return 0;
}//调用了构造函数
输出结果:
用户写的构造函数被调用
用户写的无参构造函数被调用
3. #include<iostream>
using namespace std;
class Tdate{
public:
Tdate(){
cout<<"a"<<endl;
}
Tdate(int d){
cout<<"b"<<endl;
}
Tdate(int m,int d){
cout<<"c"<<endl;
}
Tdate(int m,int d,int y){
cout<<"d"<<endl;
}
};
int main(){
Tdate dday(1,2,1998);
Tdate aday;
Tdate bday(10);
return 0;
}
输出结果:
d
a
b
4. #include<iostream>
using namespace std;
class Test{
private:
int x;
public:
Test(){
cout<<"对象地址:"<<this<<",构造函数被调用"<<endl;
x=0;
}
~Test(){
cout<<"对象地址:"<<this<<",析构函数被调用"<<endl;
}
void print(){
cout<<"数据成员:x="<<x<<endl;
}
};
int main(){
Test obj1,obj2;
obj1.print();
obj2.print();
return 0;
}
(1)析构函数主要作用就是释放资源,避免内存泄漏。在对象消亡时,自动完成清除工作,即在系统释放对象前做一些清理工作,如利用delete运算符释放临时分配的内存。
构造函数和析构函数的名字和类名一样,没有返回值,不过析构函数要在前面加上 ~
(2)构造函数调用顺序:按照对象定义的先后顺序调用构造函数,以创建新对象。
析构函数调用顺序:先创建的对象后被析构,后创建的对象先被析构。
5. #include<iostream>
using namespace std;
class A{
public:
A(){
cout<<"我爱我的家乡"<<endl;
}
~A(){
cout<<"我爱我的祖国"<<endl;
}
};
A me;
int main(){
}
-
一种
-
所有全局对象都在主函数main之前被构造,主函数结束后被析构。
6. #include<iostream>
using namespace std;
class Box{
public:
Box(int,int,int);
int volume();
private:
int height;int width;int length;
};
Box::Box(int h,int w,int len){
height=h;
width=w;
length=len;
}
int Box::volume(){
return (height*width*length);
}
int main(){
Box box1(12,25,30);
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15,30,21);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
return 0;
}
输出结果:
The volume of box1 is 9000
The volume of box2 is 9450
(1)#include<iostream>
using namespace std;
class Box{
public:
Box(int,int,int);
int volume();
Box();
private:
int height;int width;int length;
};
Box::Box(){
height=10;
width=10;
length=10;
}
Box::Box(int h,int w,int len){
height=h;
width=w;
length=len;
}
int Box::volume(){
return (height*width*length);
}
int main(){
Box box1(12,25,30);
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15,30,21);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
return 0;
}
输出结果:
The volume of box1 is 9000
The volume of box2 is 9450
(2)#include<iostream>
using namespace std;
class Box{
public:
Box(int,int,int);
int volume();
Box();
private:
int height;int width;int length;
};
Box::Box(){
height=10;
width=10;
length=10;
}
Box::Box(int h,int w,int len){
height=h;
width=w;
length=len;
}
int Box::volume(){
return (height*width*length);
}
int main(){
Box box1(12,20,25);
cout<<"The volume of box1 is"<<box1.volume()<<endl;
Box box2(10,14,20);
cout<<"The volume of box2 is"<<box2.volume()<<endl;
Box box3(box1);
cout<<"The volume of box3 is"<<box3.volume()<<endl;
return 0;
}
输出结果:
The volume of box1 is6000
The volume of box2 is2800
The volume of box3 is6000
7. (1)#include<iostream>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;
y=yy;
}
Point(Point &p);
int GetX(){
return x;
}
int GetY(){
return y;
}
private:
int x,y;
};
Point::Point(Point &p){
x=p.x;
y=p.y;
cout<<"拷贝构造函数被调用"<<endl;
}
int main(void){
Point A(1,2);
Point B(A);
cout<<B.GetX()<<endl;
cout<<B.GetY()<<endl;
return 0;
}
输出结果:
拷贝构造函数被调用
1
2
(2)#include<iostream>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;
y=yy;
}
Point(Point &p);
int GetX(){
return x;
}
int GetY(){
return y;
}
private:
int x,y;
};
Point::Point(Point &p){
x=p.x;
y=p.y;
cout<<"拷贝构造函数被调用"<<endl;
}
void fun1(Point p){
cout<<p.GetX()<<endl;
}
int main(void){
Point A(1,2);
fun1(A);
return 0;
}
输出结果:
拷贝构造函数被调用
1
(3)#include<iostream>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
x=xx;
y=yy;
}
Point(Point &p);
int GetX(){
return x;
}
int GetY(){
return y;
}
private:
int x,y;
};
Point::Point(Point &p){
x=p.x;
y=p.y;
cout<<"拷贝构造函数被调用"<<endl;
}
Point fun1(){
cout<<"fun1函数"<<endl;
Point B(3,4);
return B;
}
void fun2(Point &p){
cout<<"fun2函数"<<endl;
cout<<p.GetX()<<endl;
}
int main(void){
Point A(1,2);
fun1();
fun2(A);
return 0;
}
输出结果:
fun1函数
fun2函数
1
思考题:
#include<iostream>
using namespace std;
class Date{
public:
Date(int y=2012,int m=1,int d=1);
void Display();
void AddOneDay();
void SetDay(int y,int m,int d);
protected:
bool Legal(int y,int m,int d);
bool IsLeapYear(int y);
int year;
int month;
int day;
};
Date::Date(int y,int m,int d){
year=y;
month=m;
day=d;
}
void Date::Display(){
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
void Date::AddOneDay(){
if(Legal(year,month,day+1))
day++;
else if(Legal(year,month+1,1))
month++,day=1;
else if(Legal(year+1,1,1))
day=1,month=1,year++;
}
void Date::SetDay(int y,int m,int d){
if(Legal(y,m,d))
day=d,month=m,year=y;
}
bool Date::Legal(int y,int m,int d){
if(y>9999||y<1||d<1||m<1||m>12)
return false;
int dayLimit=31;
switch(m){
case 4: case 6: case 9: case 11: dayLimit--;
}
if(m==2)
dayLimit=IsLeapYear(y)?29:28;
return (d>dayLimit)?false:true;
}
bool Date::IsLeapYear(int y){
return !(y%4)&&(y%100)||!(y%400);
}
int main(){
Date t;
t.Display();
t.AddOneDay();
t.Display();
t.SetDay(2012,2,28);
t.Display();
t.AddOneDay();
t.Display();
t.SetDay(2011,3,5);
t.Display();
t.AddOneDay();
t.Display();
}
输出结果:
2012/1/1
2012/1/2
2012/2/28
2012/2/29
2011/3/5
2011/3/6
8.
#include<iostream>
using namespace std;
class MyClass{
public:
MyClass(int len){
array=new int[len];
arraySize=len;
for(int i=0;i<arraySize;i++)
array[i]=i+1;
}
~MyClass(){
delete []array;//
}
void Print() const{
for(int i=0;i<arraySize;i++)
cout<<array[i]<<' ';//
cout<<endl;
}
private:
int *array;
int arraySize;
};
int main(){
MyClass obj(10);//
obj.Print();
return 0;
}
输出结果:
1 2 3 4 5 6 7 8 9 10
9. #include<iostream>
using namespace std;
class A{
public:
A(){
x=0;cout<<"CA0"<<endl;
}
A(int i){
x=i;cout<<"CA1"<<endl;
}
A(A &r){
x=r.x;cout<<"CA2"<<endl;
}
~A(){
cout<<"CA"<<endl;
}
private:
int x;
};
void f1(A m){
}
void f2(A &m){
}
int main(){
A a;
A b(a);
f1(a);
f2(a);
}
输出结果:
CA0
CA2
CA2
CA
CA
CA
10. #include<iostream>
using namespace std;
class Tdate{
public:
Tdate(){
cout<<"A"<<endl;
}
Tdate(Tdate & t){
cout<<"B"<<endl;
}
~Tdate(){
cout<<"C"<<endl;
}
private:
int x;
};
int main(){
Tdate days[10];
return 0;
}
输出结果:
A
A
A
A
A
A
A
A
A
A
C
C
C
C
C
C
C
C
C
C
11. #include<iostream>
using namespace std;
class A{
public:
A(){
cout<<"构造A"<<endl;
}
~A(){
cout<<"析构A"<<endl;
}
};
class C{
public:
C(){
cout<<"构造C"<<endl;
}
~C(){
cout<<"构造C"<<endl;
}
private:
A a;
int i;
};
int main(){
C c;
return 0;
}
输出结果:
构造A
构造C
构造C
析构A
12. #include<iostream>
using namespace std;
class Point{
public:
Point(int xx=0,int yy=0){
X=xx;
Y=yy;
}
Point(Point &p);
int GetX(){ return X; }
int GetY(){ return Y; }
private:
int X,Y;
};
Point::Point(Point &p)//定义拷贝构造函数
{
X=p.X;
Y=p.Y;
}
int main(){
Point A(1,2);
Point B(A);//用对象A初始化新建对象B
cout<<B.GetX()<<endl;
}
输出结果:
1
13. #include<iostream>
using namespace std;
class Vector{
public:
Vector(int s=100);
Vector(Vector &v);
~Vector();
protected:
int size;
int *buffer;
};
Vector::Vector(int s){
buffer=new int[size=s];
for(int i=0;i<size;i++)
buffer[i]=i*i;
}
Vector::~Vector(){
delete []buffer;
}
Vector::Vector(Vector &v){
size=v.size;
buffer=new int[size];
for(int i=0;i<size;i++)
buffer[i]=v.buffer[i];
}
int main(){
}
14. #include<iostream>
using namespace std;
class A {
public:
A(int s=100);
A(A &v);
~A();
protected:
int *pa;
};
A::A(int s){
pa=new int;
*pa=s;
}
A::~A(){
delete []pa;
cout<<"析构"<<endl;
}
A::A(A &v){
pa=new int;
*pa=*v.pa;
cout<<"拷贝构造"<<endl;
}
int main(){
A a;
A b(a);
return 0;
}
输出结果:
拷贝构造
析构
析构
15. #include <iostream>
#include <string>
using namespace std;
class bankAccount{
private:
int AccountNo; //帐号id设置为6位
string Password; //密码为6位
double Balance; //余额
double money;
public:
bankAccount();
void setId(int i){
AccountNo=i;
}
void setPwd(string p){
Password=p;
}
void setBalance (double );
void setmoney(double m){
money=m;
}
double getBalance ();
int getId();
string getPwd();
double getmoney();
int withDraw(double m);
void deposit(double m);
void display();
};
bankAccount::bankAccount(){
AccountNo=0;
Password[8]=0;
Balance=0;
}
int bankAccount::getId(){
return AccountNo;
}
string bankAccount::getPwd(){
return Password;
}
double bankAccount::getBalance(){
return Balance;
}
double bankAccount::getmoney(){
return money;
}
int bankAccount::withDraw(double m){
if(money <m)
{
cout<<"余额不足"<<endl;
return 0;
}
else
{
money -= m;
cout<<"取款成功,账户余额:"<<money<<endl;
return 1;
}
}
void bankAccount::deposit(double m)
{
money+= m ;
cout<<"存款成功,账户余额为:"<<money<<endl;
}
void bankAccount::display()
{
cout<<"欢迎您使用CK银行存款机!"<<endl;
}
int main()
{
bankAccount a1,a2;
a1.display();
int id;
string pwd;
double m;
cout<<"请依次输入账号编号,密码,余额"<<endl;
cin>>id>>pwd>>m ;
a1.setId(id);
a1.setmoney(m);
a1.setPwd(pwd);
a1.display();
cout<<"请输入存钱数目:";
cin>>m;
a1.deposit(m);
a1.display();
cout<<"请输入取款数目:";
cin>>m;
a1.withDraw(m);
a1.display();
return 0;
}
输出结果:
欢迎您使用CK银行存款机!
请依次输入账号编号,密码,余额
001 123456 200
欢迎您使用CK银行存款机!
请输入存钱数目:999
存款成功,账户余额为:1199
欢迎您使用CK银行存款机!
请输入取款数目:100
取款成功,账户余额:1099
欢迎您使用CK银行存款机!