例9.1 在例8.3基礎上定義構(gòu)造成員函數(shù)。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time( ) //定義構(gòu)造成員函數(shù),函數(shù)名與類名相同
{hour=0; //利用構(gòu)造函數(shù)對對象中的數(shù)據(jù)成員賦初值
minute=0;
sec=0;
}
voidset_time( ); //函數(shù)聲明
voidshow_time( ); //函數(shù)聲明
private:
inthour; //私有數(shù)據(jù)成員
intminute;
intsec;
};
voidTime∷set_time( ) //定義成員函數(shù),向數(shù)據(jù)成員賦值
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime∷show_time( ) //定義成員函數(shù),輸出數(shù)據(jù)成員的值
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
intmain( )
{
Time t1; //建立對象t1,同時調(diào)用構(gòu)造函數(shù)t1.Time( )
t1.set_time( ); //對t1的數(shù)據(jù)成員賦值
t1.show_time( ); //顯示t1的數(shù)據(jù)成員的值
Time t2; //建立對象t2,同時調(diào)用構(gòu)造函數(shù)t2.Time( )
t2.show_time( ); //顯示t2的數(shù)據(jù)成員的值
return0;
}
例9.10 引用靜態(tài)數(shù)據(jù)成員。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(int,int);
intvolume( );
staticintheight; //把height定義為公用的靜態(tài)的數(shù)據(jù)成員
intwidth;
intlength;
};
Box∷Box(intw,intlen) //通過構(gòu)造函數(shù)對width和length賦初值
{width=w;
length=len;
}
intBox∷volume( )
{return(height*width*length);
}
intBox∷height=10; //對靜態(tài)數(shù)據(jù)成員height初始化
intmain( )
{
Box a(15,20),b(20,30);
cout<<a.height<<endl; //通過對象名a引用靜態(tài)數(shù)據(jù)成員
cout<<b.height<<endl; //通過對象名b引用靜態(tài)數(shù)據(jù)成員
cout<<Box∷height<<endl; //通過類名引用靜態(tài)數(shù)據(jù)成員
cout<<a.volume( )<<endl; //調(diào)用volume函數(shù),計算體積,輸出結(jié)果
}
例9.11 靜態(tài)成員函數(shù)的應用。
#include <iostream>
usingnamespacestd;
classStudent //定義Student類
{public:
Student(intn,inta,floats):num(n),age(a),score(s){ } //定義構(gòu)造函數(shù)
voidtotal( );
staticfloataverage( ); //聲明靜態(tài)成員函數(shù)
private:
intnum;
intage;
floatscore;
staticfloatsum; //靜態(tài)數(shù)據(jù)成員
staticintcount; //靜態(tài)數(shù)據(jù)成員
};
voidStudent∷total( ) //定義非靜態(tài)成員函數(shù)
{sum+=score; //累加總分
count++; //累計已統(tǒng)計的人數(shù)
}
float Student∷average( ) //定義靜態(tài)成員函數(shù)
{return(sum/count);
}
floatStudent∷sum=0; //對靜態(tài)數(shù)據(jù)成員初始化
intStudent∷count=0; //對靜態(tài)數(shù)據(jù)成員初始化
intmain( )
{Student stud[3]={ //定義對象數(shù)組并初始化
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++) //調(diào)用3次total函數(shù)
stud[i].total( );
cout<<"the average score of "<<n<<" students is "<<Student∷average( )<<endl;
//調(diào)用靜態(tài)成員函數(shù)
return0;
}
例9.12 友元函數(shù)的簡單例子。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time(int,int,int);
friendvoiddisplay(Time &); //聲明display函數(shù)為Time類的友元函數(shù)
private: //以下數(shù)據(jù)是私有數(shù)據(jù)成員
inthour;
intminute;
intsec;
};
Time∷Time(inth,intm,ints) //構(gòu)造函數(shù),給hour,minute,sec賦初值
{hour=h;
minute=m;
sec=s;
}
voiddisplay(Time& t) //這是友元函數(shù),形參t是Time類對象的引用
{cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}
intmain( )
{ Time t1(10,13,56);
display(t1);
return0; //調(diào)用display函數(shù),實參t1是Time類對象
}
例9.13 友元成員函數(shù)的簡單應用。
在本例中除了介紹有關友元成員函數(shù)的簡單應用外,還將用到類的提前引用聲明,請讀者注意。
#include <iostream>
usingnamespacestd;
classDate; //對Date類的提前引用聲明
classTime //定義Time類
{public:
Time(int,int,int);
voiddisplay(Date &); //display是成員函數(shù),形參是Date類對象的引用
private:
inthour;
intminute;
intsec;
};
classDate //聲明Date類
{public:
Date(int,int,int);
friendvoidTime∷display(Date &); //聲明Time中的display函數(shù)為友元成員函數(shù)
private:
intmonth;
intday;
intyear;
};
Time∷Time(inth,intm,ints) //類Time的構(gòu)造函數(shù)
{hour=h;
minute=m;
sec=s;
}
voidTime∷display(Date &d) //display的作用是輸出年、月、日和時、分、秒
{cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; //引用Date類對象中的私有數(shù)據(jù)
cout<<hour<<":"<<minute<<":"<<sec<<endl; //引用本類對象中的私有數(shù)據(jù)
}
Date∷Date(intm,intd,inty) //類Date的構(gòu)造函數(shù)
{month=m;
day=d;
year=y;
}
intmain( )
{Time t1(10,13,56); //定義Time類對象t1
Date d1(12,25,2004); //定義Date類對象d1
t1.display(d1); //調(diào)用t1中的display函數(shù),實參是Date類對象d1
return0;
}
例9.14 聲明一個類模板,利用它分別實現(xiàn)兩個整數(shù)、浮點數(shù)和字符的比較,求出大數(shù)和小數(shù)。
#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,用于兩個整數(shù)的比較
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,用于兩個浮點數(shù)的比較
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。求它們的體積。編一個基于對象的程序,在類中用帶參數(shù)的構(gòu)造函數(shù)。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(int,int,int); //聲明帶參數(shù)的構(gòu)造函數(shù)
intvolume( ); //聲明計算體積的函數(shù)
private:
intheight;
intwidth;
intlength;
};
Box∷Box(inth,intw,intlen) //在類外定義帶參數(shù)的構(gòu)造函數(shù)
{height=h;
width=w;
length=len;
}
intBox∷volume( ) //定義計算體積的函數(shù)
{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的基礎上,定義兩個構(gòu)造函數(shù),其中一個無參數(shù),一個有參數(shù)。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box( ); //聲明一個無參的構(gòu)造函數(shù)
Box(inth,intw,intlen):height(h),width(w),length(len){ }
//聲明一個有參的構(gòu)造函數(shù),用參數(shù)的初始化表對數(shù)據(jù)成員初始化
intvolume( );
private:
intheight;
intwidth;
intlength;
};
Box∷Box( ) //定義一個無參的構(gòu)造函數(shù)
{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程序中的構(gòu)造函數(shù)改用含默認值的參數(shù),長、寬、高的默認值均為10。
在例9.3程序的基礎上改寫如下:
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(inth=10,intw=10,intlen=10); //在聲明構(gòu)造函數(shù)時指定默認參數(shù)
intvolume( );
private:
intheight;
intwidth;
intlength;
};
Box∷Box(inth,intw,intlen) //在定義函數(shù)時可以不指定默認參數(shù)
{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 包含構(gòu)造函數(shù)和析構(gòu)函數(shù)的C++程序。
#include<string>
#include<iostream>
usingnamespacestd;
classStudent //聲明Student類
{public:
student(intn,string nam,chars ) //定義構(gòu)造函數(shù)
{num=n;
name=nam;
sex=s;
cout<<"Constructor called."<<endl; //輸出有關信息
}
~Student( ) //定義析構(gòu)函數(shù)
{cout<<"Destructor called."<<endl;} //輸出有關信息
voiddisplay( ) //定義成員函數(shù)
{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的數(shù)據(jù)
Student stud2(10011,"Zhang_fun",'m'); //定義對象stud2
stud2.display( ); //輸出學生2的數(shù)據(jù)
return0;
}
例9.6 對象數(shù)組的使用方法。
#include <iostream>
usingnamespacestd;
classBox
{public:
Box(inth=10,intw=12,intlen=15): height(h),width(w),length(len){ }
//聲明有默認參數(shù)的構(gòu)造函數(shù),用參數(shù)初始化表對數(shù)據(jù)成員初始化
intvolume( );
private:
intheight;
intwidth;
intlength;
};
intBox∷volume( )
{return(height*width*length);
}
intmain( )
{ Box a[3]={ //定義對象數(shù)組
Box(10,12,15), //調(diào)用構(gòu)造函數(shù)Box,提供第1個元素的實參
Box(15,18,20), //調(diào)用構(gòu)造函數(shù)Box,提供第2個元素的實參
Box(16,20,26) //調(diào)用構(gòu)造函數(shù)Box,提供第3個元素的實參
};
cout<<"volume of a[0] is "<<a[0].volume( )<<endl; //調(diào)用a[0]的volume函數(shù)
cout<<"volume of a[1] is "<<a[1].volume( )<<endl; //調(diào)用a[1] 的volume函數(shù)
cout<<"volume of a[2] is "<<a[2].volume( )<<endl; //調(diào)用a[2] 的volume函數(shù)
}
例9.7 有關對象指針的使用方法。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time(int,int,int);
inthour;
intminute;
intsec;
voidget_time( ); //聲明公有成員函數(shù)
};
Time∷Time(inth,intm,ints)
{hour=h;
minute=m;
sec=s;
}
voidTime∷get_time( ) //定義公有成員函數(shù)
{cout<<hour<<":"<<minute<<":"<<sec<<endl;}
intmain( )
{Time t1(10,13,56); //定義Time類對象t1
int*p1=&t1.hour; //定義指向整型數(shù)據(jù)的指針變量p1,并使p1指向t1.hour
cout<<*p1<<endl; //輸出p1所指的數(shù)據(jù)成員t1.hour
t1.get_time( ); //調(diào)用對象t1的成員函數(shù)get_time
Time *p2=&t1; //定義指向Time類對象的指針變量p2,并使p2指向t1
p2->get_time( ); //調(diào)用p2所指向?qū)ο?即t1)的get_time函數(shù)
void(Time∷*p3)( ); //定義指向Time類公用成員函數(shù)的指針變量p3
p3=&Time∷get_time; //使p3指向Time類公用成員函數(shù)get_time
(t1.*p3)( ); //調(diào)用對象t1中p3所指的成員函數(shù)(即t1.get_time( ))
}
例9.8 對象的常引用。
#include <iostream>
usingnamespacestd;
classTime
{public:
Time(int,int,int);
inthour;
intminute;
intsec;
};
Time∷Time(inth,intm,ints) //定義構(gòu)造函數(shù)
{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); //聲明有默認參數(shù)的構(gòu)造函數(shù)
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;
}
