return0;
}
intadd(intx,inty)
{intc;
c=x+y;
return(c);
}
10:
#include
usingnamespacestd;
intmain()
{voidsort(intx,inty,intz);
intx,y,z;
cin>>x>>y>>z;
sort(x,y,z);
return0;
}
voidsort(intx,inty,intz)
{
inttemp;
if(x>y){temp=x;x=y;y=temp;}2-4-12-4-22-5-12-5-2Boxbox1Boxbox1core;
intk=0;
for(inti=1;i<5;i++)
if(arr[i].score>max_score){max_score=arr[i].score;k=i;}
cout<}
6:
#include
usingnamespacestd;
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
voidchange(intn,floats){num=n;score=s;}
voiddisplay(){cout<private:
intnum;
floatscore;
};
intmain()
{Studentstud(101,;
();
(101,;
();
return0;
}
7:
解法一
#include
usingnamespacestd;
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
voidchange(intn,floats){num=n;score=s;}
voiddisplay(){cout<otal();
Product:
:
display();
return0;
}
10:
#include
usingnamespacestd;
classDate;
classTime
{public:
Time(int,int,int);
friendvoiddisplay(constDate&,constTime&);
private:
inthour;
intminute;
intsec;
};
Time:
:
Time(inth,intm,ints)
{hour=h;
minute=m;
sec=s;
}
classDate
{public:
Date(int,int,int);
friendvoiddisplay(constDate&,constTime&);
private:
intmonth;
intday;
intyear;
};
Date:
:
Date(intm,intd,inty)
{month=m;
day=d;
year=y;
}
voiddisplay(constDate&d,constTime&t)
{
cout<<<<"/"<<<<"/"<<<cout<<<<":
"<<<<":
"<<<}
intmain()
{
Timet1(10,13,56);
Dated1(12,25,2004);
display(d1,t1);
return0;
}
11:
#include
usingnamespacestd;
classTime;
classDate
{public:
Date(int,int,int);
friendTime;
private:
intmonth;
intday;
intyear;
};
Date:
:
Date(intm,intd,inty):
month(m),day(d),year(y){}
classTime
{public:
Time(int,int,int);
voiddisplay(constDate&);
private:
inthour;
intminute;
intsec;
};
Time:
:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
voidTime:
:
display(constDate&d)
{
cout<<<<"/"<<<<"/"<<<cout<"<"<}
intmain()
{
Timet1(10,13,56);
Dated1(12,25,2004);
(d1);
return0;
}
12:
#include
usingnamespacestd;
template
classCompare
{public:
Compare(numtypea,numtypeb);
numtypemax();
numtypemin();
private:
numtypex,y;
};
template
Compare:
:
Compare(numtypea,numtypeb)
{x=a;y=b;}
template
numtypeCompare:
:
max()
{return(x>y)?
x:
y;}
template
numtypeCompare:
:
min()
{return(xx:
y;}
intmain()
{Comparecmp1(3,7);
cout<<()<<"istheMaximumoftwointegernumbers."<cout<<()<<"istheMinimumoftwointegernumbers."<Comparecmp2,;
cout<<()<<"istheMaximumoftwofloatnumbers."<cout<<()<<"istheMinimumoftwofloatnumbers."<Comparecmp3('a','A');
cout<<()<<"istheMaximumoftwocharacters."<cout<<()<<"istheMinimumoftwocharacters."<return0;
}
第四章
1:
#include
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
doubleget_real();
doubleget_imag();
voiddisplay();
private:
doublereal;
doubleimag;
};
doubleComplex:
:
get_real()
{returnreal;}
doubleComplex:
:
get_imag()
{returnimag;}
voidComplex:
:
display()
{cout<<"("<Complexoperator+(Complex&c1,Complex&c2)
{
returnComplex()+(),()+());
}
intmain()
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
();
return0;
}
2:
#include
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
Complexoperator+(Complex&c2);
Complexoperator-(Complex&c2);
Complexoperator*(Complex&c2);
Comp