elseif(b>c)
cout<
else
cout<}
intmain()
{
max(2,5);
max(7,5,3);
return0;
}
4、#include
usingnamespacestd;
voidswap1(inta,intb)
{
inttemp;
temp=a;
a=b;
b=temp;
}
voidswap2(int*a,int*b)
{
inttemp;
temp=*a;
*a=*b;
*b=temp;
}
voidswap3(int&a,int&b)
{
inttemp;
temp=a;
a=b;
b=temp;
}
intmain()
{
intx,y;
x=5;y=7;
swap1(x,y);
cout<<"x="<x=5;y=7;
swap2(&x,&y);
cout<<"x="<x=5;y=7;
swap3(x,y);
cout<<"x="<return0;
}
5、#include
#include
usingnamespacestd;
classstudent
{
private:
intnum;
stringname;
intage;
charsex;
public:
voidset()
{
cout<<"请输入:
学号,姓名,年龄,性别(form)"<cin>>num>>name>>age>>sex;
}
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"年龄:
"<cout<<"性别:
"<}
};
intmain()
{
students;
s.set();
s.display();
return0;
}
6、#include
#include
usingnamespacestd;
classstudent
{
private:
intnum;
stringname;
intage;
charsex;
public:
/*voidset()
{
cout<<"请输入:
学号,姓名,年龄,性别(form)"<cin>>num>>name>>age>>sex;
}
*/
student(intnum,stringname,inta,ints)
{
cout<<"调用构造函数num="<this->num=num;
this->name=name;
age=a;
sex=s;
}天猫优惠券,我就要券
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"年龄:
"<cout<<"性别:
"<}
~student()
{
cout<<"析构函数学号:
"<}
};
intmain()
{
students(1001,"张三",25,'f'),s1(1002,"李四",24,'m');
//s.set();
s.display();
s1.display();
return0;
}
7、#include
#include
usingnamespacestd;
classstudent
{
private:
intnum;
stringname;
intage;
charsex;
public:
/*voidset()
{
cout<<"请输入:
学号,姓名,年龄,性别(form)"<cin>>num>>name>>age>>sex;
}
*/
student()
{
cout<<"调用构造函数num="<<0<num=0;
name="";
age=18;
sex='f';
}
student(intnum,stringname,inta,ints)
{
cout<<"调用构造函数num="<this->num=num;
this->name=name;
age=a;
sex=s;
}
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"年龄:
"<cout<<"性别:
"<}
~student()
{
cout<<"析构函数学号:
"<}
};
intmain()
{
students(1001,"张三",25,'f'),s1(1002,"李四",24,'m'),s2;
//s.set();
s.display();
cout<s1.display();
cout<s2.display();
cout<return0;
}
8、#include
#include
usingnamespacestd;
天猫优惠券,我就要券
classstudent
{
private:
intnum;
stringname;
intage;
charsex;
public:
student(intnum=0,stringname="",inta=18,ints='f')
{
cout<<"调用构造函数num="<this->num=num;
this->name=name;
age=a;
sex=s;
}
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"年龄:
"<cout<<"性别:
"<}
~student()
{
cout<<"
}
};
intmain()
{
students(1001,"张三",25,'f'),s1(1002,"李四",24,'m'),s2;
//s.set();
s.display();
cout<s1.display();
cout<s2.display();
cout<return0;
}
9、#include
#include
usingnamespacestd;
classstudent
{
private:
intnum;
stringname;
intage;
charsex;
public:
student(intnum=0,stringname="",inta=18,ints='f')
{
cout<<"调用构造函数num="<this->num=num;
this->name=name;
age=a;
sex=s;
}
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"年龄:
"<cout<<"性别:
"<}
~student()
{
cout<<"析构函数学号:
"<}
};
intmain()
{
students[3]={student(1001,"张三",25,'f'),student(1002,"李四",24,'m')};
for(inti=0;i<3;i++)
{
s[i].display();
cout<}
student*p;
p=newstudent(1003,"王五",23,'f');
p->display();
cout<deletep;
return0;
}
10、#include
#include
usingnamespacestd;
classstudent
{
private:
intnum;
stringname;
intage;
charsex;
public:
student(intnum=0,stringname="",inta=18,ints='f')
{
cout<<"调用构造函数num="<this->num=num;
this->name=name;
age=a;
sex=s;
}
student(student&s)
{
num=s.num;
name=s.name;
age=s.age;
sex=s.sex;
}
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"年龄:
"<cout<<"性别:
"<}
~student()
{
cout<<"析构函数学号:
"<}
};
intmain()
{
students1(1001,"张三",25,'f');
students2(s1);
s1.display();
s2.display();
return0;
}
11、
#include
#include
usingnamespacestd;
classstudent
{
private:
intnum;
stringname;
floatscore;
staticfloatsum;
staticintcount;
public:
student(intnum=0,stringname="",floats=80)
{
this->num=num;
this->name=name;
score=s;
count++;
}
voiddisplay()
{
cout<<"学号:
"<cout<<"姓名:
"<cout<<"成绩:
"<}
voidtotal();
staticfloataverage();
};
floatstudent:
:
sum=0.0;
intstudent:
:
count=0;
voidstudent:
:
total()
{
sum+=score;
}
floatstudent:
:
average()
{
returnsum/count;
}
intmain()
{
students[3]={student(1001,"张三",87),student(1002,"李四"),student(1003,"王五",90)};
for(inti=0;i<3;i++)
{
s[i].total();
}
cout<<"平均成绩:
"<:
average()<return0;
}
12、
#include
//#include
//usingnamespacestd;
classTime
{
private:
inthour;
intminute;
intsecond;
public:
Time(inth=12,intm=0,ints=0)
{
hour=h;
minute=m;
second=s;
}
friendvoiddisplay(Time&);
};
voiddisplay(Time&t)
{
cout<}
intmain()
{
Timet,t1(13,12,22);
display(t);
display(t1);
return0;
}
13、
(1)
//方法1:
多次重载+
#include
//#include
//usingnamespacestd;
classComplex
{
private:
floatreal;
floatimage;
public:
Complex(floatreal,floatimage)
{
this->real=real;
this->image=image;
}
Complex()
{
real=0;
image=0;
}
friendComplexoperator+(Complex&a,Complex&b);
friendComplexoperator+(Complex&a,floatb);
friendComplexoperator+(floata,Complex&b);
friendostream&operator<<(ostream&output,Complexa);
friendistream&operator>>(istream&input,Complexa);
};
Complexoperator+(Complex&a,Complex&b)
{
returnComplex(a.real+b.real,a.image+b.image);
}
Complexoperator+(Complex&a,floatb)
{
returnComplex(a.real+b,a.image);
}
Complexoperator+(floata,Complex&b)
{
returnComplex(b.real+a,b.image);
}
ostream&operator<<(ostream&output,Complexa)
{
if(a.image>0)
{
output<}
else
{
output<}
returnoutput;
}
istream&operator>>(istream&input,Complexa)
{
input>>a.real>>a.image;
returninput;
}
intmain()
{
Complexa(3,4),b(1,2),c,d,e,f;
c=a+b;
d=a+5;
e=3+b;
cout<cin>>f;
cout<return0;
}
//方法2:
使用转换构造函数实现
#include
classComplex
{
private:
floatreal;
floatimage;
public:
Complex(floatreal,floatimage)
{
this->real=real;
this->image=image;
}
Complex()
{
real=0;
image=0;
}
//定义转换构造函数
Complex(floatr)
{
real=