[C]C++程序设计第三版第九章 对象数组

9.6  输入三个立方体体积,用对象数组的方法

 #include<iostream>
using namespace std;
class Box
{
public:
Box(int h =10,int w =12,int len = 15 ):height(h),width(w),length(len){}
int volume();
private:
int height;
int width;
int length;
};
int Box ::volume()
{
return(height*width*length);
}
int main()
{
Box a[3] = {
Box(10,12,15),
Box(15,18,20),
Box(16,20,26)
           };
cout<<"volume of a[0] is"<<a[0].volume()<<endl;
cout<<"volume of a[1] is"<<a[1].volume()<<endl;
cout<<"volume of a[2] is"<<a[2].volume()<<endl;
getchar();
}

9.9 讲一个立方体对象的值赋给另一个立方体对象

 #include<iostream>
using namespace std;
class Box
{
public:
Box(int h =10,int w =12,int len = 15 );
int volume();
private:
int height;
int width;
int length;
};
Box :: Box(int h, int w, int len)
{
height =h;
width  =w;
length =len;
}


int Box ::volume()
{
return(height*width*length);
}
int main()
{
Box box1(15,30,25),box2;

cout<<"The volume of box1 is"<<box1.volume()<<endl;
cout<<"The volume of box2 is"<<box2.volume()<<endl;
getchar();
return 0;
}

请自行分析程序,
如有不懂,敬请留言!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
程序设计技术》(第三版)习题参考答案 习题1 一、单项选择题 DADDD BDCCD 二、填空题 ① 主 ② main ③ x=x*(x+b) ④ 普通、格式、分隔和转义 ⑤ 指定输出数据格式 ⑥ void main() ⑦ "how are you! " 三、阅读程序题 1. 9,11,9,10 2. 2,-1,-2 3. aa bb cc abc A 6 4. 2,2 5. 57 5 7 67.5864000,-789.124023 67.5864000, -7.89e+002 67,86, -789.12,67.5864000, -789.124023, 67.5864000, -789.124023 6.758640e+001, -7.89e+002 A,65,101,41 1234567,4553207,12d687 65535,177777,ffff,65535 COMPUTER, COM. 6. a + c = 102 a + c = f f + m = 17.950000 a + m = 17.650000 c + f = 102.300000 double = 1746.150019 四、程序设计题 //xt010401.cpp #include <stdio.h> #define PI 3.1415926 void main() { double r,h; printf("请输入半径r和高h:"); scanf("%lf,%lf",&r,&h); printf("圆半径: %lf\n",2*PI*r); printf("圆面积:%lf\n",PI*r*r); printf("圆球表面积:%lf\n",4*PI*r*r); printf("圆球体积:%lf\n",4.0/3*PI*r*r); printf("圆柱体积:%lf\n",PI*r*r*h); } //xt010402.cpp #include <stdio.h> void main() { int c; printf("Input a character:"); c=getchar(); printf("%c\t%d\n",c,c); } //xt010403.cpp #include <stdio.h> void main() { int x,y,h=30,f=90; x=(4*h-f)/2; y=(f-2*h)/2; printf("鸡:%d, 兔:%d\n",x,y); } //xt010404.cpp #include <stdio.h> #include <math.h> void main() { double a,b,c,s,area; printf("请输入三角形的三条边长(注意三条边长应能够构成三角形):"); scanf("%lf,%lf,%lf",&a,&b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("三角形的面积是: %lf\n",area); } //xt010405.cpp #include <stdio.h> void main() { double f,c; printf("请输入华氏温度:");

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

quchen528

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值