例9.1 在例8.3基础上定义构造成员函数。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time( ) //定义构造成员函数,函数名与类名相同
{hour=0; //利用构造函数对对象中的数据成员赋初值
minute=0;
sec=0;
}
voidset_time( ); //函数声明
voidshow_time( ); //函数声明
private:
inthour; //私有数据成员
intminute;
intsec;
};
voidTime∷set_time( ) //定义成员函数,向数据成员赋值
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime∷show_time( ) //定义成员函数,输出数据成员的值
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
intmain( )
{
Time t1; //建立对象t1,同时调用构造函数t1.Time( )
t1.set_time( ); //对t1的数据成员赋值
t1.show_time( ); //显示t1的数据成员的值
Time t2; //建立对象t2,同时调用构造函数t2.Time( )
t2.show_time( ); //显示t2的数据成员的值
return0;
}
例9.10 引用静态数据成员。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(int,int);
intvolume( );
staticintheight; //把height定义为公用的静态的数据成员
intwidth;
intlength;
};
Box∷Box(intw,intlen) //通过构造函数对width和length赋初值
{width=w;
length=len;
}
intBox∷volume( )
{return(height*width*length);
}
intBox∷height=10; //对静态数据成员height初始化
intmain( )
{
Box a(15,20),b(20,30);
cout<<a.height<<endl; //通过对象名a引用静态数据成员
cout<<b.height<<endl; //通过对象名b引用静态数据成员
cout<<Box∷height<<endl; //通过类名引用静态数据成员
cout<<a.volume( )<<endl; //调用volume函数,计算体积,输出结果
}
例9.11 静态成员函数的应用。
#include <iostream>
usingnamespacestd;
classStudent //定义Student类
{public:
Student(intn,inta,floats):num(n),age(a),score(s){ } //定义构造函数
voidtotal( );
staticfloataverage( ); //声明静态成员函数
private:
intnum;
intage;
floatscore;
staticfloatsum; //静态数据成员
staticintcount; //静态数据成员
};
voidStudent∷total( ) //定义非静态成员函数
{sum+=score; //累加总分
count++; //累计已统计的人数
}
float Student∷average( ) //定义静态成员函数
{return(sum/count);
}
floatStudent∷sum=0; //对静态数据成员初始化
intStudent∷count=0; //对静态数据成员初始化
intmain( )
{Student stud[3]={ //定义对象数组并初始化
Student(1001,18,70),
Student(1002,19,78),
Student(1005,20,98)
};
intn;
cout<<"please input the number of students:";
cin>>n; //输入需要求前面多少名学生的平均成绩
for(inti=0;i<n;i++) //调用3次total函数
stud[i].total( );
cout<<"the average score of "<<n<<" students is "<<Student∷average( )<<endl;
//调用静态成员函数
return0;
}
例9.12 友元函数的简单例子。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time(int,int,int);
friendvoiddisplay(Time &); //声明display函数为Time类的友元函数
private: //以下数据是私有数据成员
inthour;
intminute;
intsec;
};
Time∷Time(inth,intm,ints) //构造函数,给hour,minute,sec赋初值
{hour=h;
minute=m;
sec=s;
}
voiddisplay(Time& t) //这是友元函数,形参t是Time类对象的引用
{cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}
intmain( )
{ Time t1(10,13,56);
display(t1);
return0; //调用display函数,实参t1是Time类对象
}
例9.13 友元成员函数的简单应用。
在本例中除了介绍有关友元成员函数的简单应用外,还将用到类的提前引用声明,请读者注意。
#include <iostream>
usingnamespacestd;
classDate; //对Date类的提前引用声明
classTime //定义Time类
{public:
Time(int,int,int);
voiddisplay(Date &); //display是成员函数,形参是Date类对象的引用
private:
inthour;
intminute;
intsec;
};
classDate //声明Date类
{public:
Date(int,int,int);
friendvoidTime∷display(Date &); //声明Time中的display函数为友元成员函数
private:
intmonth;
intday;
intyear;
};
Time∷Time(inth,intm,ints) //类Time的构造函数
{hour=h;
minute=m;
sec=s;
}
voidTime∷display(Date &d) //display的作用是输出年、月、日和时、分、秒
{cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; //引用Date类对象中的私有数据
cout<<hour<<":"<<minute<<":"<<sec<<endl; //引用本类对象中的私有数据
}
Date∷Date(intm,intd,inty) //类Date的构造函数
{month=m;
day=d;
year=y;
}
intmain( )
{Time t1(10,13,56); //定义Time类对象t1
Date d1(12,25,2004); //定义Date类对象d1
t1.display(d1); //调用t1中的display函数,实参是Date类对象d1
return0;
}
例9.14 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
#include <iostream>
usingnamespacestd;
template<classnumtype> //定义类模板
classCompare
{public:
Compare(numtype a,numtype b)
{x=a;y=b;}
numtype max( )
{return(x>y)?x:y;}
numtype min( )
{return(x<y)?x:y;}
private:
numtype x,y;
};
intmain( )
{Compare<int> cmp1(3,7); //定义对象cmp1,用于两个整数的比较
cout<<cmp1.max( )<<" is the Maximum of two integer numbers."<<endl;
cout<<cmp1.min( )<<" is the Minimum of two integer numbers."<<endl<<endl;
Compare<float> cmp2(45.78,93.6); //定义对象cmp2,用于两个浮点数的比较
cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min( )<<" is the Minimum of two float numbers."<<endl<<endl;
Compare<char> cmp3('a','A'); //定义对象cmp3,用于两个字符的比较
cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;
cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;
return0;
}
例9.2 有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,14,20。求它们的体积。编一个基于对象的程序,在类中用带参数的构造函数。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(int,int,int); //声明带参数的构造函数
intvolume( ); //声明计算体积的函数
private:
intheight;
intwidth;
intlength;
};
Box∷Box(inth,intw,intlen) //在类外定义带参数的构造函数
{height=h;
width=w;
length=len;
}
intBox∷volume( ) //定义计算体积的函数
{return(height*width*length);
}
intmain( )
{Box box1(12,25,30); //建立对象box1,并指定box1长、宽、高的值
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15,30,21); //建立对象box2,并指定box2长、宽、高的值
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
return0;
}
例9.3 在例9.2的基础上,定义两个构造函数,其中一个无参数,一个有参数。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box( ); //声明一个无参的构造函数
Box(inth,intw,intlen):height(h),width(w),length(len){ }
//声明一个有参的构造函数,用参数的初始化表对数据成员初始化
intvolume( );
private:
intheight;
intwidth;
intlength;
};
Box∷Box( ) //定义一个无参的构造函数
{height=10;
width=10;
length=10;
}
intBox∷volume( )
{return(height*width*length);
}
intmain( )
{
Box box1; //建立对象box1,不指定实参
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15,30,25); //建立对象box2,指定3个实参
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
return0;
}
例9.4 将例9.3程序中的构造函数改用含默认值的参数,长、宽、高的默认值均为10。
在例9.3程序的基础上改写如下:
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(inth=10,intw=10,intlen=10); //在声明构造函数时指定默认参数
intvolume( );
private:
intheight;
intwidth;
intlength;
};
Box∷Box(inth,intw,intlen) //在定义函数时可以不指定默认参数
{height=h;
width=w;
length=len;
}
intBox∷volume( )
{return(height*width*length);
}
intmain( )
{
Box box1; //没有给实参
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15); //只给定一个实参
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
Box box3(15,30); //只给定2个实参
cout<<"The volume of box3 is "<<box3.volume( )<<endl;
Box box4(15,30,20); //给定3个实参
cout<<"The volume of box4 is "<<box4.volume( )<<endl;
return0;
}
例9.5 包含构造函数和析构函数的C++程序。
#include<string>
#include<iostream>
usingnamespacestd;
classStudent //声明Student类
{public:
student(intn,string nam,chars ) //定义构造函数
{num=n;
name=nam;
sex=s;
cout<<"Constructor called."<<endl; //输出有关信息
}
~Student( ) //定义析构函数
{cout<<"Destructor called."<<endl;} //输出有关信息
voiddisplay( ) //定义成员函数
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl<<endl; }
private:
intnum;
charname[10];
charsex;
};
intmain( )
{Student stud1(10010,"Wang_li",'f'); //建立对象stud1
stud1.display( ); //输出学生1的数据
Student stud2(10011,"Zhang_fun",'m'); //定义对象stud2
stud2.display( ); //输出学生2的数据
return0;
}
例9.6 对象数组的使用方法。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(inth=10,intw=12,intlen=15): height(h),width(w),length(len){ }
//声明有默认参数的构造函数,用参数初始化表对数据成员初始化
intvolume( );
private:
intheight;
intwidth;
intlength;
};
intBox∷volume( )
{return(height*width*length);
}
intmain( )
{ Box a[3]={ //定义对象数组
Box(10,12,15), //调用构造函数Box,提供第1个元素的实参
Box(15,18,20), //调用构造函数Box,提供第2个元素的实参
Box(16,20,26) //调用构造函数Box,提供第3个元素的实参
};
cout<<"volume of a[0] is "<<a[0].volume( )<<endl; //调用a[0]的volume函数
cout<<"volume of a[1] is "<<a[1].volume( )<<endl; //调用a[1] 的volume函数
cout<<"volume of a[2] is "<<a[2].volume( )<<endl; //调用a[2] 的volume函数
}
例9.7 有关对象指针的使用方法。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time(int,int,int);
inthour;
intminute;
intsec;
voidget_time( ); //声明公有成员函数
};
Time∷Time(inth,intm,ints)
{hour=h;
minute=m;
sec=s;
}
voidTime∷get_time( ) //定义公有成员函数
{cout<<hour<<":"<<minute<<":"<<sec<<endl;}
intmain( )
{Time t1(10,13,56); //定义Time类对象t1
int*p1=&t1.hour; //定义指向整型数据的指针变量p1,并使p1指向t1.hour
cout<<*p1<<endl; //输出p1所指的数据成员t1.hour
t1.get_time( ); //调用对象t1的成员函数get_time
Time *p2=&t1; //定义指向Time类对象的指针变量p2,并使p2指向t1
p2->get_time( ); //调用p2所指向对象(即t1)的get_time函数
void(Time∷*p3)( ); //定义指向Time类公用成员函数的指针变量p3
p3=&Time∷get_time; //使p3指向Time类公用成员函数get_time
(t1.*p3)( ); //调用对象t1中p3所指的成员函数(即t1.get_time( ))
}
例9.8 对象的常引用。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time(int,int,int);
inthour;
intminute;
intsec;
};
Time∷Time(inth,intm,ints) //定义构造函数
{hour=h;
minute=m;
sec=s;
}
voidfun(Time &t) //形参t是Time类对象的引用
{t.hour=18;}
intmain( )
{Time t1(10,13,56); // t1是Time类对象
fun(t1); //实参是Time类对象,可以通过引用来修改实参t1的值
cout<<t1.hour<<endl; //输出t1.hour的值为18
return0;
}
例9.9 对象的赋值。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(int=10,int=10,int=10); //声明有默认参数的构造函数
intvolume( );
private:
intheight;
intwidth;
intlength;
};
Box∷Box(inth,intw,intlen)
{height=h;
width=w;
length=len;
}
intBox∷volume( )
{return(height*width*length); //返回体积
}
intmain( )
{Box box1(15,30,25),box2; //定义两个对象box1和box2
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
box2=box1; //将box1的值赋给box2
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
return0;
}
