}
intA:
:
b=0;
intA:
:
c=0;
voidmain()
{
Aa(3);
Ac(7);
Ad(0);
Ab(-1);
b.print();
}
2.编写一个程序,定义一个类,此类包括3个私有数据成员:
colorcast(彩电)、ridge(冰箱)、washer洗衣机以及total(当天总收入)。
彩电目前的价格是4200元/每台、冰箱目前的价格是2800元/每台、洗衣机的价格1500元/每台。
要求实现输入彩电、冰箱、洗衣机的价格和当天所卖出的数量,本程序将计算出百货商店家电部当天的总营业额。
#include
usingnamespacestd;
classshop
{
private:
intcolorcast;
intridge;
intwasher;
public:
voidtotal();
inta,b,c,totalnumber;
voidgetprice();
voidgetnumber();
};
voidshop:
:
getprice()
{
cout<<"请输入彩电,冰箱,洗衣机的价格:
"<intx,y,z;
cin>>x>>y>>z;
colorcast=x;
ridge=y;
washer=z;
}
voidshop:
:
getnumber()
{
cout<<"请输入彩电,冰箱,洗衣机的个数:
"<cin>>a>>b>>c;
}
voidshop:
:
total()
{
cout<<"家电的总营业额为a:
"<totalnumber=colorcast*a+ridge*b+washer*c;
cout<}
voidmain()
{
shopmyshop;
myshop.getprice();
myshop.getnumber();
myshop.total();
}
3.编写一个程序,定义一个汽车类(car),其中包括车型、颜色、车门数、座位数和轴距等属性,包括chage_parameters()和disp_parameters()等函数;disp_parameters()显示车型、颜色、车门数、座位数和轴距属性,函数change_parameters()改变对象的车型、颜色、车门数、座位数和轴距属性,实现并测试这个类。
#include
#include
usingnamespacestd;
classcar
{
private:
stringtype;
stringcolor;
intdoornumber;
intchairnumber;
intzhoucheng;
public:
voiddisp_parameters(char*,char*,int,int,int);
voidchange_parameters(char*,char*,int,int,int);
};
voidcar:
:
disp_parameters(char*a,char*b,intc,intd,inte)
{
type=a;
color=b;
doornumber=c;
chairnumber=d;
zhoucheng=e;
cout<<"更改前为:
"<<"车型í:
"<"<"<"<"<}
voidcar:
:
change_parameters(char*a,char*b,intc,intd,inte)
{type=a;
color=b;
doornumber=c;
chairnumber=d;
zhoucheng=e;
cout<<"更改后为:
"<<"车型í:
"<"<"<"<"<}
voidmain()
{
carmycar;
mycar.disp_parameters("奥拓","黑色?
",4,4,5);
mycar.change_parameters("奥迪","红色",2,2,3);
}
测试结果:
4.编写一个程序,使其可实现输入5个学生学号、姓名、性别和年龄,并按年龄从大到小进行排序输出。
#include
usingnamespacestd;
structstu{
longno;
charname[9];
charsex[5];
intage;};
voidmain()
{stum;
stustudent[5];
for(intb=0;b<5;b++)
{cin>>student[b].no>>student[b].name>>student[b].sex>>student[b].age;
}
for(inti=1;i<5;i++)
{
for(intj=0;j<=4-i;j++)
{
if(student[j].age{
m=student[j];
student[j]=student[j+1];
student[j+1]=m;
}
}
}
for(intk=0;k<5;k++)
{cout<}
}
5.设计一个模拟银行ATM柜台机的程序,包括存款、取款和查询余额三种功能。
提示:
在存款、取款时都需要检查帐号和密码,可以直接引用其成员函数来检查,如果是合法客户,返回true,然后执行增加存款或减少存款的程序。
#include
usingnamespacestd;
classband
{
private:
staticintmoney;
staticinta,b;
public:
intcheck()
{cout<<"请输入账号和密码:
"<intx,y;
for(inti=1;i<=3;i++)
{cin>>x>>y;
if(a==x,b==y)
{return1;
break;}
else
{if(i!
=3){cout<<"输入错误,重新输入";}
else
{cout<<"拒绝服务"<}}
}
voidincrease()
{
cout<<"请输入存款金额:
"<inti;
cin>>i;
money+=i;
}
voidreduce()
{
cout<<"请输入取款金额:
"<intj;
cin>>j;
money-=j;
}
voidcheckmoney()
{
cout<}
};
intband:
:
a=444641;
intband:
:
b=12345;
intband:
:
money=2000;
voidmain()
{
inta;
intm=1;
bandmyband;
do
{
cout<<"请选择服务类型:
1.存款2.取款?
3.查询余额4.结束服务"<cin>>a;
if(a==1)
{
if(myband.check())
{myband.increase();}
}
elseif(a==2)
{
if(myband.check())
{myband.reduce();}
}
elseif(a==3)
{
myband.checkmoney();
}
elseif(a==4)
break;
}while(m==1);
}
6.有一个学生类,包括学号、姓名和成绩,设计一个友员函数,比较学生成绩的高低,并求出最高分和最低分。
#include
usingnamespacestd;
classstudent
{
private:
intno;
stringname;
intscore;
public:
voidget(int,char*,int);
friendvoidcompare(student&a,student&b,student&c);
};
voidcompare(student&a,student&b,student&c)
{
intx,y,m,n;
x=a.score>b.score?
a.score:
b.score;
y=x>c.score?
x:
c.score;
cout<<"最高分为:
"<m=a.scorea.score:
b.score;
n=mm:
c.score;
cout<<"最低分为:
"<}
voidstudent:
:
get(intx,char*y,intz)
{
no=x;
name=y;
score=z;
}
voidmain()
{
studenta,b,c;
a.get(01,"zhang",87);
b.get(02,"wang",97);
c.get(03,"li",65);
compare(a,b,c);
}