C++练习Word格式.docx
《C++练习Word格式.docx》由会员分享,可在线阅读,更多相关《C++练习Word格式.docx(30页珍藏版)》请在冰豆网上搜索。
max<
endl;
intmain()
{Array_maxamax;
amax.set_value();
amax.max_value();
amax.show_value();
return0;
2.定义一个Time类(包括hour,minute,sec),要求输入和输出2个对象t1和t2。
方法一(引用对象)
#include<
structTime//classTime
Public:
{
inthour;
intminute;
intsec;
{voidset_time(Time&
);
voidshow_time(Time&
Timet1;
set_time(t1);
show_time(t1);
Timet2;
set_time(t2);
show_time(t2);
return0;
voidset_time(Time&
t)
t.hour>
t.minute>
t.sec;
voidshow_time(Time&
cout<
t.hour<
t.minute<
t.sec<
方法二:
classTime
{private:
public:
voidset_time();
voidshow_time();
voidTime:
set_time()
hour>
minute>
sec;
show_time()
hour<
minute<
sec<
t1.set_time();
t1.show_time();
t2.set_time();
t2.show_time();
3.构造函数
(1)
classBox
{public:
Box(intl,intw,inth):
length(l),width(w),height(h){}
intvolume();
private:
intlength,width,height;
intBox:
volume()
return(length*width*height);
{Boxbox1(10,10,10);
Thevolumeofbox1is:
box1.volume()<
Boxbox2(12,12,12);
Thevolumeofbox2is:
box2.volume()<
(2)
Box(intl=10,intw=10,inth=10);
//在声明构造函数时指定默认参数
Box:
Box(intl,intw,inth)//在定义构造函数时可不必再指定参数的默认值
{length=l;
width=w;
height=h;
{Boxbox1;
Boxbox2(12);
Boxbox3(18,10);
Thevolumeofbox3is:
box3.volume()<
Boxbox4(15,12,20);
Thevolumeofbox4is:
box4.volume()<
4.对象数组
{public:
Box(intl,intw,inth):
intlength;
intwidth;
intheight;
return(length*width*height);
{Boxa[3]=
Box(25,10,12),
Box(15,18,20),
Box(16,20,26)
cout<
Thevolumeofa[0]is:
a[0].volume()<
Thevolumeofa[1]is:
a[1].volume()<
Thevolumeofa[2]is:
a[2].volume()<
5.对象指针
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
{Timet1(16,43,20);
int*p1=&
t1.hour;
*p1<
Time*p2=&
t1;
p2->
show_time();
//(*p2).show_time();
void(Time:
*p3)();
p3=&
Time:
show_time;
(t1.*p3)();
6.对象的常引用
Time(inth,intm,ints):
voidfun(Time&
t);
//形参t是Time类对象的引用//函数声明
voidfun(Time&
{t.hour=18;
t.minute=12;
fun(t1);
7.静态数据成员引用
Box(intl=10,intw=10):
length(l),width(w){}
intvolume();
staticintheight;
intlength,width;
height=12;
//静态数据成员可以初始化,但只能在类外进行初始化
{Boxbox1,box2(12,10);
8.静态成员函数引用
classStudent
Student(intn,inta,floats):
num(n),age(a),score(s){}
voidtotal();
staticfloataverage();
intnum;
intage;
floatscore;
staticfloatsum;
staticintscount;
floatStudent:
sum=0;
intStudent:
scount=0;
voidStudent:
total()
sum+=score;
//sum=sum+score;
scount++;
average()
return(sum/scount);
{Studentstu[4]={
Student(1101,18,70),
Student(1103,17,69),
Student(1104,18,78),
Student(1106,19,80)};
intn;
pleaseinputthenumberofstudents:
;
n;
stu[i].total();
Theaveragescoreof"
n<
studentsis:
Student:
average()<
9.将普通函数声明为友元函数
{public:
friendvoiddisplay(Time&
private:
voiddisplay(Time&
intmain()
{Timet1(22,55,30);
display(t1);
}
classDate;
friendvoiddisplay(Time&
voiddisplay(Time&
classDate
Date(inty,intm,intd):
year(y),month(m),day(d){}
friendvoiddisplay(Date&
intyear;
intmonth;
intday;
voiddisplay(Date&
d)
d.year<
/"
d.month<
d.day<
{Timet1(22,55,30);
Dated1(2012,5,18);
display(d1);
10.友元成员函数
voiddisplay(Date&
friendvoidTime:
display(Date&
t1.display(d1);
11.运算符重载
(1)#include<
classComplex
Complex(){real=0;
imag=0;
Complex(intr,inti):
real(r),imag(i){}
Complexoperator+(Complex&
c2);
voiddisplay();
intreal;
intimag;
ComplexComplex:
operator+(Complex&
c2)//operator+是一个函数名,是类Complex的成员函数
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;
voidComplex:
display()
("
real<
"
imag<
i)"
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
c1="
c1.display();
c2="
c2.display();
c1+c2="
c3.display();
(2)重载流插入运算符“<
”:
iostream.h>
Complex(){real=0;
Complex(intr,inti){real=r;
imag=i;
Complexoperator+(Complex&
friendostream&
operator<
(ostream&
Complex&
operator+(Complex&
c2)
returnComplex(real+c2.real,imag+c2.imag);
ostream&
output,Complex&
c)
{output<
c.real;
if(c.imag>
=0)output<
+"
output<
c.imag<
i"
<
returnoutput;
{Complexc1(2,4),c2(6,-10),c3;
c3=c1+c2;
c1<
endl<
c2<
c3<
12.派生类的构造函数和析构函数:
string>
Student(intn,stringnam,floatm)
{num=n;
name=nam;
mark=m;
~Student(){cout<
num<
protected:
stringname;
floatmark;
classStudent1:
protectedStudent
Student1(intn,stringnam,floatm,inta,chars):
Student(n,nam,m)
{age=a;
sex=s;
voidshow()
学号:
姓名:
name<
分数:
mark<
年龄:
age<
性别:
sex<
~Student1(){cout<
charsex;
{Student1stud1(1101,"
zhuxuhong"
100,18,'
m'
Student1stud2(1102,"
zhangxu"
80,20,'
学生1信息:
stud1.show(),cout<
学生2信息:
stud2.show(),cout<
13.有子对象的派生类的构造函数:
voiddisplay()
{
Student1(intn,stringnam,floatm,intn1,stringnam1,floatm1,inta,chars):
Student(n,nam,m),monitor(n1,nam1,m1)
学生信息:
display();
voidshow_monitor()
班长信息:
monitor.display();
Studentmonitor;
98,1107,"
xiaoyin"
stud1.show();
stud1.show_monitor();
14.多层派生时的构造函数:
Student(intn,stringnam)
{cout<