}
intmain()
{inta[5]={1,9,0,23,-45};
floatb[5]={2.4,7.6,5.5,6.6,-2.3};
longc[5]={10100,-123567,1198783,-165654,3456};
sort(a);
sort(b);
sort(c);
return0;
}
第二章
1
#include
usingnamespacestd;
classTime
{
public:
voidset_time();
voidshow_time();
private:
//成员改为公用的
inthour;
intminute;
intsec;
};
voidTime:
:
set_time()//在main函数之前定义
{
cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime:
:
show_time()//在main函数之前定义
{
cout<"<"<}
intmain()
{Timet1;
t1.set_time();
t1.show_time();
return0;
}
2:
#include
usingnamespacestd;
classTime
{public:
voidset_time(void)
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidshow_time(void)
{cout<"<"<private:
inthour;
intminute;
intsec;
};
Timet;
intmain()
{
t.set_time();
t.show_time();
return0;
}
3:
#include
usingnamespacestd;
classTime
{public:
voidset_time(void);
voidshow_time(void);
private:
inthour;
intminute;
intsec;
};
voidTime:
:
set_time(void)
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime:
:
show_time(void)
{cout<"<"<Timet;
intmain()
{t.set_time();
t.show_time();
return0;
}
4:
//xt2-4-1.cpp(main.cpp)
#include
usingnamespacestd;
#include"xt2-4.h"
intmain()
{Studentstud;
stud.set_value();
stud.display();
return0;
}
//xt2-4-2.cpp(即student.cpp)
#include"xt2-4.h"//在此文件中进行函数的定义
#include
usingnamespacestd;//不要漏写此行
voidStudent:
:
display()
{cout<<"num:
"<cout<<"name:
"<cout<<"sex:
"<}
voidStudent:
:
set_value()
{cin>>num;
cin>>name;
cin>>sex;
}
5:
//xt2-5-1.cpp(file1.cpp)
#include
#include"xt2-5.h"
intmain()
{Array_maxarrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return0;
}
//xt2-5-2.cpp(arraymax.cpp)
#include
usingnamespacestd;
#include"xt2-5.h"
voidArray_max:
:
set_value()
{inti;
for(i=0;i<10;i++)
cin>>array[i];
}
voidArray_max:
:
max_value()
{inti;
max=array[0];
for(i=1;i<10;i++)
if(array[i]>max)max=array[i];
}
voidArray_max:
:
show_value()
{cout<<"max="<}
6:
解法一
#include
usingnamespacestd;
classBox
{public:
voidget_value();
floatvolume();
voiddisplay();
public:
floatlengh;
floatwidth;
floatheight;
};
voidBox:
:
get_value()
{cout<<"pleaseinputlengh,width,height:
";
cin>>lengh;
cin>>width;
cin>>height;
}
floatBox:
:
volume()
{return(lengh*width*height);}
voidBox:
:
display()
{cout<intmain()
{Boxbox1,box2,box3;
box1.get_value();
cout<<"volmueofbax1is";
box1.display();
box2.get_value();
cout<<"volmueofbax2is";
box2.display();
box3.get_value();
cout<<"volmueofbax3is";
box3.display();
return0;
}
解法二:
#include
usingnamespacestd;
classBox
{public:
voidget_value();
voidvolume();
voiddisplay();
public:
floatlengh;
floatwidth;
floatheight;
floatvol;
};
voidBox:
:
get_value()
{cout<<"pleaseinputlengh,width,height:
";
cin>>lengh;
cin>>width;
cin>>height;
}
voidBox:
:
volume()
{vol=lengh*width*height;}
voidBox:
:
display()
{cout<intmain()
{Boxbox1,box2,box3;
box1.get_value();
box1.volume();
cout<<"volmueofbax1is";
box1.display();
box2.get_value();
box2.volume();
cout<<"volmueofbax2is";
box2.display();
box3.get_value();
box3.volume();
cout<<"volmueofbax3is";
box3.display();
return0;
}
第三章
2:
#include
usingnamespacestd;
classDate
{public:
Date(int,int,int);
Date(int,int);
Date(int);
Date();
voiddisplay();
private:
intmonth;
intday;
intyear;
};
Date:
:
Date(intm,intd,inty):
month(m),day(d),year(y)
{}
Date:
:
Date(intm,intd):
month(m),day(d)
{year=2005;}
Date:
:
Date(intm):
month(m)
{day=1;
year=2005;
}
Date:
:
Date()
{month=1;
day=1;
year=2005;
}
voidDate:
:
display()
{cout<intmain()
{
Dated1(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
d1.display();
d2.display();
d3.display();
d4.display();
return0;
}
3:
#include
usingnamespacestd;
classDate
{public:
Date(int=1,int=1,int=2005);
voiddisplay();
private:
intmonth;
intday;
intyear;
};
Date:
:
Date(intm,intd,inty):
month(m),day(d),year(y)
{}
voidDate:
:
display()
{cout<intmain()
{
Dated1(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
d1.display();
d2.display();
d3.display();
d4.display();
return0;
}
4:
#include
usingnamespacestd;
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
voiddisplay();
private:
intnum;
floatscore;
};
voidStudent:
:
display()
{cout<intmain()
{Studentstud[5]={
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5)};
Student*p=stud;
for(inti=0;i<=2;p=p+2,i++)
p->display();
return0;
}
5:
#include
usingnames