北邮信通C++上机实验 第七章.docx
《北邮信通C++上机实验 第七章.docx》由会员分享,可在线阅读,更多相关《北邮信通C++上机实验 第七章.docx(22页珍藏版)》请在冰豆网上搜索。
北邮信通C++上机实验第七章
第七章实验题
一.基础题
1.
#include
usingnamespacestd;
classCSample{
doublea;
CSample()
{a=2.5;};
public:
CSample(intval);
~CSample();};
2.
#include
usingnamespacestd;
classcube
{
public:
cube();//构造函数
~cube();//析构函数
intvolume();//定义整形函数
private:
intheight,width,depth;//定义私有数据成员
};
cube:
:
cube()
{
height=100;
depth=4;
width=2;
cout<<"Constructorcalled."<}
cube:
:
~cube()
{
cout<<"Destructorcalled."<system("pause");
}
intcube:
:
volume()
{
returnheight*width*depth;
}
voidmain()
{
cubecubeone;//定义对象
cout<<"立方体的体积为:
"<system("pause");
}
3.
#include
usingnamespacestd;
classCCounter
{
intvalue;
public:
CCounter()
{
cout<<"CCounterConstructor1"<value=0;
}
CCounter(intval)
{
cout<<"CCounterConstructor2"<value=val;
}
~CCounter()
{
cout<<"CCounterDestructor"<system("pause");
}
};
classCExample
{
intvalue;
public:
CCountercar;
CExample()
{
cout<<"CExampleConstructor1"<value=0;
}
CExample(intval)
{
cout<<"CExampleConstructor2"<value=val;
}
~CExample()
{
cout<<"CExampleDestructor"<system("pause");
}
voidDisplay(){cout<<"value="<};
voidmain()
{
CExampleobj(350);
obj.Display();
system("pause");
}
//调用顺序:
CCounter()-CExample(intval)-~CExample()-~CCounter()
4.
#include
usingnamespacestd;
classPoint
{
protected:
intx;
public:
intGetx(){returnx;}
intY;
voidinit(inta,intb){x=a;Y=b;}
intGety(){returnY;}
voidSetx(inta){x=a;}
voidSety(intb){Y=b;}
};
voidmain()
{
PointA,B;
A.init(25,27);
B.init(29,31);
cout<<"A.x="<cout<<"B.x="<cout<<"A.Y="<cout<<"A.y="<cout<<"B.y="<cout<<"B.y="<system("pause");
}
5.
#include
usingnamespacestd;
classbase
{
private:
inti;
staticintk;//定义静态变量k
public:
base(){i=0;k++;}//定义构造函数
voidDisplay();
};
voidbase:
:
Display()
{
k=k+10;
i++;
cout<<"i="<
}
intbase:
:
k=0;//初始化k的值
voidmain()
{
basea,b;
a.Display();
b.Display();
system("pause");
}
运行结果为:
i=1;k=12
i=1;k=22
二.编程题
1.
{
if(x>0)
{
cout<<"It'sapositivenumber"<positive++;
}
elseif(x==0)
{
zero++;
cout<<"It'szero"<}
else
{
negative++;
cout<<"It'sanegativenumber"<}
}
intnum:
:
positive=0;
intnum:
:
negative=0;
intnum:
:
zero=0;
voidmain()
{
intn;
while(cin>>n)
{
numtest(n);
test.decision();
test.print();
}
system("pause");
}
2.
#include
usingnamespacestd;
classdianqi
{
public:
voidgetprice(intx,inty,intz)
{
colorcast=x;
ridge=y;
washer=z;
};
voidtotal(inta,intb,intc)
{
intt=a*colorcast+b*ridge+c*washer;
cout<};
private:
intcolorcast;
intridge;
intwasher;
};
voidmain()
{
dianqidian;
dian.getprice(4200,2800,1500);
cout<<"请分别输入彩电,冰箱,洗衣机的销售数量:
";
inta=0,b=0,c=0;
cin>>a;
cout<<"";
cin>>b;
cout<<"";
cin>>c;
cout<cout<<"总营业额为:
";
dian.total(a,b,c);
system("pause");
}
3.
#include
#include
usingnamespacestd;
classcar
{
public:
voiddisp_parameters()
{
cout<<"型号:
"<cout<<"颜色:
"<cout<<"车门数:
"<cout<<"座位数:
"<cout<<"轴距:
"<}
voidchange(stringa,charb,intc,intd,inte)
{
type=a;
colour=b;
door=c;
seat=d;
distance=e;
}
private:
stringtype;
charcolour;
intdoor;
intseat;
intdistance;
};
voidmain()
{
cargundam;
gundam.change("DoubleOGUNDAM",'b',1,5,6);
gundam.disp_parameters();
system("pause");
}
4.
#include
usingnamespacestd;
classstudent
{
public:
intnum;
charname[20];
charsex;
intage;
};
voidmain()
{
studentstu[5];
studenttemp;
for(inti=0;i<5;i++)
{
cout<<"输入第"<
";
cin>>stu[i].num;
cout<<"输入第"<
";
cin>>stu[i].name;
cout<<"输入第"<
";
cin>>stu[i].sex;
cout<<"输入第"<
";
cin>>stu[i].age;
}
for(inta=1;a<5;a++)
{
for(intb=0;b<5-a;b++)
{
if(stu[b].age{
temp.age=stu[b+1].age;
stu[b+1].age=stu[b].age;
stu[b].age=temp.age;
}
}
}
for(inti=0;i<5;i++)
cout<<"num:
"<"<"<"<system("pause");
}
5.
#include
usingnamespacestd;
classATM
{
public:
voids_balance(void)
{
cout<<"Thebalanceis"<}
voiddeposit(floata)
{
balance+=a;
}
voidwithdraw(floata)
{
balance-=a;
}
intchecking(inta)
{
if(key==a)
return1;
else
return0;
}
private:
intkey=328;
staticfloatbalance;
};
floatATM:
:
balance=0.0f;
voidmain()
{
ATMsys1;
intk,m;
floatnum;
cout<<"Pleaseinputthekey:
";
cin>>k;
if(sys1.checking(k)!
=1)
cout<<"Error"<else
{
cout<<"Pleasechoosethefunction:
"<"
(1)Depositingmoney"<"
(2)Withdrawingmoney"<"(3)Checkthebalance"<while(cin>>m)
{
switch(m)
{
case1:
cout<<"Pleaseinputhowmuchyouwanttodeposit:
";
cin>>num;
sys1.deposit(num);
cout<<"Youhavedeposited"<sys1.s_balance();
cout<<"Pleasechoosethefunction:
"<"
(1)Depositingmoney"<"
(2)Withdrawingmoney"<"(3)Checkthebalance"<break;
case2:
cout<<"Pleaseinputhowmuchyouwanttowithdraw:
";
cin>>num;
sys1.withdraw(num);
cout<<"Youhavewithdrawed"<sys1.s_balance();
cout<<"Pleasechoosethefunction:
"<"
(1)Depositingmoney"<"
(2)Withdrawingmoney"<"(3)Checkthebalance"<break;
case3:
sys1.s_balance();
cout<<"Pleasechoosethefunction:
"<"
(1)Depositingmoney"<"
(2)Withdrawingmoney"<"(3)Checkthebalance"<break;
case4:
cout<<"Pleasechoosethefunction:
"<"
(1)Depositingmoney"<"
(2)Withdrawingmoney"<"(3)Checkthebalance"<break;
default:
cout<<"Error1"<break;
}
}
}
system("pause");
}
6.
#include
#include
usingnamespacestd;
classstudent
{public:
friendvoidsort(student&a,student&b,student&temp);
voidchange(stringa,intb,floatc)
{
name=a;
num=b;
score=c;
}
floatshow()
{
returnscore;
}
private:
intnum;
stringname;
floatscore;
};
voidsort(student&a,student&b,student&temp)
{
if(a.score{
temp=a;
a=b;
b=temp;
}
}
voidmain()
{
studentstu[5];
studenttemp;
stringq="";
intw=0;
floate=0.0f;
for(inti=0;i<5;i++)
{
cout<<"输入第"<
"<cout<<"姓名:
";
cin>>q;
cout<<"学号:
";
cin>>w;
cout<<"成绩:
";
cin>>e;
stu[i].change(q,w,e);
cout<}
for(inti=1;i<5;i++)
{
for(intj=0;j<(5-i);j++)
{
sort(stu[j],stu[j+1],temp);
}
}
cout<<"最高分"<cout<<"最低分"<system("pause");
}