【篇一:《大学物理教程习题答案》上海交通大学出版
社】
???
∴质点的轨道为圆心在(0,0)处,半径为r的圆; ?
???dr?
(2)由v?,有速度:v???rsin?ti??rcos?tj dt
而v?v,有速率:v?[(??rsin?t)2?(?rcos?t)2] ?? 2
??r。 ???
1-2.已知质点位矢随时间变化的函数形式为r?4t2i?(3?2t)j,式中r的单位为m,t的单位为s。求:(1)质点的轨道;(2)从t?0到t?1秒的位移;(3)t?0和t?1秒两时刻的速度。 解:(1)由r?4t2i?(3?2t)j,可知x?4t2 ,y?3?2t 消去t得轨道方程为:x?(y?3)2,∴质点的轨道为抛物线。 ?
???dr?
(2)由v?,有速度:v?8ti?2j dt
1?????
vdt?(8ti?2j)dt?4i?2j ?0?0? ????
(3)t?0和t?1秒两时刻的速度为:v(0)?2j,v(1)?8i?2j 。 ???
1-3.已知质点位矢随时间变化的函数形式为r?t2i?2tj,式中r的单位为m,t的单位为s.求:(1)任 ? ??
?从t?0到t?1秒的位移为:?r? 1
一时刻的速度和加速度;(2)任一时刻的切向加速度和法向加速度。 ??
??????dr?dv
解:(1)由v?,有:v?2ti?2j,a?,有:a?2i; dt dt
(2)而v?v,有速率:v?[(2t)2?22] ∴at? dvdt ?? 2 ?? 222
a?at?an有: an? ?
1-4.一升降机以加速度a上升,在上升过程中有一螺钉从天花板上松落,升降机的天花板与底板相距为d,求螺钉从天花板落到底板上所需的时间。
解法一:以地面为参照系,坐标如图,设同一时间内螺钉下落的距离为y1,升降机上升的高度为y2,运动方程分别为 y1?v0t? 1 212
y2?v0t?at (2) 2
gt(1) 2
y1?y2?d(3) (注意到y1为负值,有y1??y1) 联立求解,有:t?
解法二:以升降机为非惯性参照系,则重力加速度修正为g?g?a, 利用d? 1 12
gt,有:t? 2 ?
1-5.一质量为m的小球在高度h处以初速度v0水平抛出,求: (1)小球的运动方程;
(2)小球在落地之前的轨迹方程; (3)落地前瞬时小球的
?drdt h ,
?dvdt , dvdt 。
解:(1)如图,可建立平抛运动学方程: ??1?2
x?v0t ,y?h?gt ,∴r?v0ti?(h?gt)j; 22 1 2
(2)联立上面两式,消去t得小球轨迹方程:y?? gx2v 22
;?h(为抛物线方程) ?
????1dr?2
(3)∵r?v0ti?(h?gt)j,∴?v0i?gtj, 2dt?
???dv?
即:v?v0i?gtj,??gj dt ?
??dr
在落地瞬时,有:t? ?v0i?j dt
又∵ v ?? g2tdv ,∴??
dt[v2?(gt)2]2 。
1-6.路灯距地面的高度为h1,一身高为h2的人在路灯下以匀速v1沿直线行走。试证明人影的顶端作匀速运动,并求其速度v2. 证明:设人向路灯行走,t
由相似三角形关系可得:∴x1? h1h1?h2 x2 1 2
x1?x2 x1 ?
h2h1 ,
两边对时间求导有: dx1dt ?
h1dx2h1h1?h2 h1?h2dtdt ?
,考虑到: dx2dt ?v1,
知人影中头的速度:v影? dx2
v1(常数)。 2
1-7.一质点沿直线运动,其运动方程为x?2?4t?2t(m),在 t从0秒到3秒的时间间隔内,则质点走过的路程为多少?
解:由于是求质点通过的路程,所以可考虑在0~3s的时间间隔内,质点速度为0的位置: v? dxdt
?4?4t 若v?0 解得 t?1s, ?x1?x1?x0?(2?4?2)?2?2m
?x3?x3?x1?(2?4?3?2?3)?(2?4?2)??8m 2
?x??x1??x2?10m。
1-8.一弹性球直落在一斜面上,下落高度 ?
平的倾角??30,问它第二次碰到斜面的位远(假设小球碰斜面前后速度数值相等,碰撞时
2
h?20cm,斜面对水 置距原来的下落点多 人射角等于反射角)。
解:小球落地时速度为v0?
2gh,建立沿斜面的直角坐标系,以小球第一次落地点为坐标原点如图示,
vx0?v0cos60→ x?v0cos60t? vy0?v0sin60→ y?v0sin60t? 1212
gcos60t (1) gsin60t(2) 2 02
第二次落地时:y?0,代入(2)式得:t?所以:x?v0cos60t? 2v0g ,
?4h?80cm。 12
gcos60t? 02 2v0g 2 ?
2?2ghg
1-9.地球的自转角速度最大增加到若干倍时,赤道上的物体仍能保持在地球上而不致离开地球?已知现在赤道上物体的向心加速度约为3.4cm/s2,设赤道上重力加速度为9.80m/s2。 解:由向心力公式:f向?m?r,
赤道上的物体仍能保持在地球必须满足:f向?mg,而现在赤道上物体的向心力为:f向?ma ∴ 2 ??0 ? ? ?
?16.98?17
1-10.已知子弹的轨迹为抛物线,初速为v0,并且v0与水平面的夹角为?。试分别求出抛物线顶点及落地点的曲率半径。 解:(1)抛物线顶点处子弹的速度vx?v0cos?,顶点处切向加速度为0,法向加速度为g。 因此有:g? v 2 ?1 ?
(v0cos?) 2 ?1 , ?1?
v0cos? g 22 ;
?角,则:an?gcos?,(2)在落地点时子弹的v 0有:gcos?? v0 2 ?2 则: ?2? v0 2
gcos? 。 1b
?t)ln(1?bt),其中b是与燃料燃烧速率有关的量,u为燃 1-11.一飞行火箭的运动学方程为x?ut?u( 气相对火箭的喷射速度。求:
(1)火箭飞行速度与时间的关系;(2)火箭的加速度。 解:一维运动,直接利用公式:v? dxdt dt
dxdvub
??uln(1?bt) , (2)a??(1)v? dtdt1?bt
,a? dv 有:
1-12.飞机以v0?100m/s的速度沿水平直线飞行,在离地面高h?98m时,驾驶员要把物品投到前方某一地面目标上,问:投放物品时,驾驶员看目标的视线和竖直线应成什么角度?此时目标距飞机下方地点 y多远?
解:设此时飞机距目标水平距离为x有: 3
x?v0t┄①,h? 12
gt┄② xh
?77.5。 2
联立方程解得:x?447m,∴??arctan
1-13.一物体和探测气球从同一高度竖直向上运动,物体初速为v0?49.0m/s,而气球以速度
v?19.6m/s匀速上升,问气球中的观察者在第二秒末、第三秒末、第四秒末测得物体的速度各多少?
解:物体在任意时刻的速度表达式为:vy?v0?gt
故气球中的观察者测得物体的速度?v?vy?v 代入时间t可以得到第二秒末物体速度:?v2?9.8m第三秒末物体速度:?v3?0 第四秒末物体速度:?v4??9.8m 思考题1
1-1.质点作曲线运动,其瞬时速度为v,瞬时速率为v,平均速度为v,平均速率为v,则它们之间的下列四种关系中哪一种是正确的? (a)v?v,v?v;(b)v?v,v?v;(c)v?v,v?v;(d)v?v,v?v 答:(c)
1-2.沿直线运动的物体,其速度大小与时间成反比,则其加速度的大小与速度大小的关系是:(a)与速度大小成正比;(b)与速度大小平方成正比;(c)与速度大小成反比;(d)与速度大小平方成反比。 答:b
1-3.如图所示为a,b两个质点在同一直线上运动的v?t图像,由图可知 (a)两个质点一定从同一位置出发 (b)两个质点都始终作匀加速运动 (c)在t2s末两个质点相遇
(d)在0?t2s时间内质点b可能领先质点a 答:d
1-4.质点的x~t关系如图,图中a,b,c三条线表示三它们属于什么类型的运动?哪一个速度大?哪一个速度小? 答:匀速直线运动;va?vb?vc。
1-5.如图所示,两船a和b相距r,分别以速度va会不会相碰?若不相碰,求两船相靠最近的距离.图答:方法一:如图,以a船为参考系,在该参考系的速度v??vb?va。 v?是船b相对于船a的速度,从船b作一 s
,(向上) s
(向下)。
个速度不同的运动.问
和vb匀速直线行驶,它们
中?和?为已知。 中船a是静止的,而船b条平行于v?方向的直线相靠最近的距离 4
bc,它不与船a相交,这表明两船不会相碰. 由a作bc垂线ac,其长度rmin就是两船 rmin?rsin?
作fd//ab,构成直角三角形def,故有:sin??在三角形bef中,由余弦定理可得:v??rmin?
vbsin??vasin?v?v?2vavbcos(???) 2a 2b
vbsin??vasin? v? , 22
va?vb?2vavbcos(???) r。
方法二:
两船在任一时刻t的位置矢量分别为: ra?(vatcos?)i?(vbtsin?)j
rb?(r?vbtcos?)i?(vbtsin?)j
r?rb-ra?[r?(vbcos??vacos?)t]i?[(vbsin??vasin?)t]j 任一时刻两船的距离为: r?
r?(vbcos??vacos?)t]?[(vbsin??vasin?)t] dr(t)dt ?0 2 2
令:t?
vbcos??vacos?
(vbcos??vacos?)?(vbsin??vasin?) 2 2 r
rmin?
vbsin??vasin?v?v?2vavbcos(???) 2a 2b r。
1-6.若质点限于在平面上运动,试指出符合下列条件的各应是什么样的运动? (a) drdt ?0, ?drdt
(b)?0; dvdt ?0, ?
dvdt
(c)?0; dadt ?0, ?
dadt ?0
答:(1) 质点作圆周运动; (2) 质点作匀速率曲线运动; (3) 质点作抛体运动。 ??
1-7.如图所示,质点在t=0时刻由原点出发作斜抛运动,其速度v?vxi?vyj,回到x轴的时刻为t,则 (a) ? t0 vdt? ? t0
vxdt (b) ? t0 vdt? ? t0 vydt
(c)?vt? t ? t
vxdt (d)?vt? t ? t
vydt
答:a (注意:题目中各处的v 应为矢量!须加上箭 1-8.一质点作斜抛运动,用t1代表落地时, t1 t1 t1 y
头。)
(1)说明下面三个积分的意义:?vxdt, b ?v 0b dt,
?vdt; 0b
(2)用a和b代表抛出点和落地点位置,说明下面三个积分的意义: ?dr, a
?dr, a
?dr。 a t1
答:?vxdt 表示物体落地时x方向的距离, t1 ?v y
dt 表示物体落地时y方向的距离, 5
【篇二:大学化学课后答案(2012年)】
习题答案
1. 答案(1-)(2-)(3+)(4-)
2. 答案(1c)(2d)(3a)(4d)(5abd)(6ad)(7d)(8d) 3. 答案(1)燃烧前后系统的温度(2)水的质量和比热(3)弹式量热计热容 1
?3226?1000j?mo?l
4..答案:根据已知条件列
式 ?0.5g??[4.18j?g?1?k?1?1209g?cb](298.59?296.35)k?1 122g?mol
cb=849j.mol-1 3.8g?1
?2820kj?mol?30%?17.8kj?1
180g?mol5.答案:获得的肌肉活动的能量= 3(?27.6)?(?58.6)?2(38.1)
??16.7kj?mol?1 (-58.6)+2(38.1)+6qp=3(-27.6) qp? 6
8.下列以应(或过程)的qp与qv有区别吗? 简单说明。 (1)2.00mol nh4hs的分解 25℃
nh4hs(s) nh3(g)+h2s(g)
‘
(2)生成1.00mol的hcl
℃ h2(g)+cl2(g)25 2hcl(g) (3)5.00 mol co2(s)(干冰)的升华
-78℃
co2(s) co2(g) (4)沉淀出2.00mol agcl(s) agno3(aq)+nacl(aq) agcl(s)+nano3(aq) 25℃
10.(1)4nh3(g)+3o2(g) = 2n2(g) +6h2o(l) 答案-1530.5kj.mol-1 (2)c2h2(g) + h2(g) = c2h4(g) 答案-174.47kj.mol-1
(3)nh3(g) +稀盐酸答案-86.32kj.mol-1 写出离子反应式。产物是nh4+(aq) (4)fe(s) + cuso4(aq) 答案-153.87kj.mol-1
11.答案 查表?fhm(298.15k)/kj.mol-1 -74.81 0-135.44 -92.31 ?
12.答案 正辛烷的m=114.224g.mol-1 正辛烷完全燃烧的反应式
13.答案 caco3的煅烧反应为 caco3 (s) = cao(s) + co2(g) -1 -1 -1 4 3 -1 -1 -1 3 4-1
(1)kj.mol-1反应热q= -1229.58 kj.mol-1 (2) kj.g-1反应热q= -1229.58/26 kj.g-1 c2h4+ 3o2 = 2co2 + 2h2o(l) -1
(1)kj.mol-1反应热q= -1410.94 kj.mol-1 (2) kj.g-1反应热q= -1410.94/28 kj.g-1 根据计算结果乙烯(c2h4)完全燃烧会放出热量更多 ?
15. 答 ?rhm(298.15k)
(注意数据抄写要正确,符号不要丢) 16.答:1mol反应进度qp与qv之差别qp-qv=
-1-1 ?? b -1
= -4.957 kj.mol
qp= qv-4.957 kj.mol 定压条件下放热更多些.原因是该反应是气体分子数减小的反应,在温度一定时,定压条件下环境对反应系统做功,这部分能量转化成反应系统的燃烧热.
根据反应焓变和标准生成焓的关系(或根据已知条件设计一个循环)存在以下关系 -1
(苯轭作用使其能量比三个定域c=c降低了152 kj.mol) -1 -1
第2章 化学反应的基本原理与大气污染
1. 答案 (1-)(2-)(3-)(4-)(5-)(6+)(7+)(8+) 2. 答案1b 2b 3b 4c 5bcd6a 3. (1)答案:基本不变,基本不变,增大
减小, 增大增大
(3)答案:氟里昂,nox、hcl;co2;nox和so2
4. 答案:(d)(c)(e)(a)(b)原因是气体熵液体固体 分子量大的熵大
5. (1)溶解少量食盐于水中。 答案:正
(2)活性炭表面吸附氧气。 答案:负 (1) 碳与氧气反应生成一氧化碳。答案:正
6. 答案:由定义可知fe2o3的?fgm(298.15k)??742.2kj.mol ???rgm(298.15k)??77.7kj.mol?1=3?fgm(298.15k,fe3o4)-4?fgm(298.15k,fe2o3) ??1 ? ?
?fgm(298.15k,fe3o4)= 1
[(-77.7)+4(-742.2)]=-1015.5kj.mol-1 3 ?1
7.答案:查表水的?fgm(298.15k)??237..mol
?过程的?rgm(298.15k)?(?236.7)?(?237.129)?0.429kj.mol?10
?
所以在298.15k的标准态时不能自发进行. 8. (查表时注意状态,计算时注意乘系数) ??
?rsm(298.15k)?rgm(298.15k) j.mol?1.k?1kj.mol?1
(1)3fe(s)?4h2o(l)?fe3o4(s)?4h2(g) 307.7-66.9 (2)zn(s)?2h?(aq)?zn2?(aq)?h2(g) -23.0-147.06 (3)cao(s)?h2o(l)?ca2?(aq)?2oh?(aq)-184.3 -26.9 (4)agbr(s)?ag(s)? 1
br2(l) -51.4 96.9 2
9.答案: (1) sno2 =sn +o2 (2) sno2 +c =sn+ co2(3) sno2 +2h2 =sn +2h2o (g) kj.mol?1
??rsm(298.15k)
(1)204.388 (2)207.25 (3)115.532 j.mol?1.k?1
??rhm(298.15k)
(1) 580.7 (2)187.191 (3)97.0 kj.mol?1 ?
?rhm(298.15k) tc (1)2841k 903k (3)840k(温度最低,合适) ? ?rsm(298.15k)
10.答案: c12h22o11(s)+12o2(g)=12co2(g) +11h2o(l) ?rs?m(298.15k)
??fhm(298.15k) -2225.5 0 -393.509-285.83 ?h?(298.15k)?-50.738 kj.mol?1 ? rm
(?rgm(298.15k)=-5796.127 kj.mol-1 温度对反应的标准吉布斯函数变有影响,但由于该反应的熵变相对于焓变小(绝对值),故变化不大) ?
11.答案: 查表 sm(298.15k)
197.674130.684186.2188.825 ?rs?m(298.15k)=-214.637 j.mol?1.k?1
??fhm(298.15k) -110.525 0 -74.81 -241.818?kj.mol?1
r ?
hm(298.15k)?-206.103 ?? ?
??rgm(523k)?(?93.85)?1000 lnk(523k)???21.58 r?523k8.314?523
k??e21,58?1021.58/2.303?109.37?2.35?109 ? -1
12. 答案:设平衡时有2xmol的so2转化 2so2(g)+o2(g)=2so3(g)
起始 n/mol 8 4 0n(始)=12mol 平衡时n/mol 8-2x4-x2xn(平衡)=(12-x)mol
根据pv=nrt t v一定时 n(始)p(始)12300 ??2x=6.4mol
n(平衡)p(平衡)12?x220 peq(so3)26.42202()(? )
p??
so2的转化率=6.4/8=80% k? ?eqeq
1.62200.8220p(so2)2p(o2)(?)2?(?)()?()??8.81008.8100pp
(注意在计算时可不用先计算分压,列综合算式更方便计算) 13.答案:该反应是可逆反应,需要h2的量包括反应需要的量1mol和为维持平衡需要xmol(最少量) peq(h2s)()?
peq(h2s)neq(h2s)1.0p?
==0.36 x=2.78mol k??eq?eq xpeq(h2)p(h2)n(h2) ()? p
需要h2的量=1+2.78=3.78mol(注:该反应是反应前后气体分子数不变的,在标准平衡常数表达式中系统的总压和标准压力在计算时可以在分式中消去,否则在计算时必须知道平衡时总压才能根据平衡常数计算.) 14.在不同温度时反应的标准平衡常数值如下: t/k 973 1073 1273
? k1 k?2
2.380.618 2.000.905 1.671.287 1.491.6 ?
1.47 1.81 2.48 1173 2.15 ? k1
答:反应3的标准平衡常数 k3?? (如上) k2
(因为随温度的升高平衡常数增大,故是吸热反应) ??
?rhm(298,15k)(t2?t1)k2?
15.答案:利用公式ln??求k2 rt1?t2k1 ?
k2?92.31?1000(500?298.15)?15.0316?10 8.314?298.15?5004.9?1016
16.答案:查数据计算co2(g)?h2(g)?co(g)?h2o(g) ?
sm(298.15k) ?
j.mol?1.k?1 213.74 130.684197.674
188.825?rsm(298.15k)=42.075j.mol-1.k-1 ??fhm(298.15k) kj.mol ?1 ?
?h(298.15k)?41.166kj.mol-1
-393.509 0 -110.525 -241.818 rm ???
=4.434 kj.mol-1 ?
??rgm(873k)?4.434?1000 lnk(873k)??
8.314?873=-0.61k??e?0.61?0. rt (p(co)/p?)(p(h2o)/p?)76?76
q?(p(co2)/p?)(p(h2)/p?)=127?127=0.358 (qk0 所以反应正向进行) ? ?
?rgm??rgm(873k)?rtlnq ?rtln q
因为
?rgm0 所以此条件下反应正向进行
17.答案:吕〃查德里原理判断是正确的。判断反应的方向或平衡移动的方向的依据是温度对平衡常数的影响有两个公式: ?rgm(tk)0 ??
??hm1?rsm lnk??
rtr (1)适用于某一给定反应温度对k0的影响。 ?
??rgm(tk)? lnk(tk)??
?g(tk)越正k0越小。 rt (2)适用于温度一定的前提下不同反应比较,rm ?
18.研究指出下列反应在一定温度范围内为元反应:2no(g)+cl2(g2nocl(g) (1)写出该反应的速率方程。 (因为是元反应,可根据反应式直接写出)??kc(no)c(cl2) (2)该反应的总级数是多少? ( n=2+1=3级)
(3)其他条件不变,如果将容器的体积增大到原来的2倍,反应速率如何变化? (体积增大后浓度各变为原来的1/2 2
??k(c(no))2(c(cl))?? 2 1 212 1
8 速率变为原来的1/8
(4)如果容器体积不变而将no的浓度增大到原来的3倍,反应速率又将怎样变化?(率变为原来的9倍) ??k(3c(no))2(c(cl))?9? 2 速
c094?ktln?2.09?10?5t
45c19.答案:根据一级反应的特征 t=352h=1468天=4年
k(t2)ea(t2?t1)k(1000k)167.4?1000(1000?900)
20.答案:根据阿仑尼乌斯公式 ln 代入相应数据得ln=2.237 ?? k(t1)rt1t2k(900k)8.314?900?1000 v(1000k)k(1000k) ??e2.237?9.36 v(900k)k(900k) ln
21.答案:相同用量的溶液反应,观察到出现沉淀可以认为生成沉淀量相等,所以速率的比与所需时间成反比。根据阿仑 k(300k)ea(t2?t1)ea(300?290) 代入相应数据得 ln ?
k(t1)rt1t2k(290k)8.314?290?300
1515ea(300?290)8.314?290?300ln?j.mol?1=80184j.mol-1=80.2kj.mol-1ea=ln3.03?5008.314?290?30010 ?ea1
()?lna 假定催化剂对a(指前因子)无影响时,在一定温度下,有无22.答案:根据阿仑尼乌斯公式 lnk? rt
k(2)(ea1?ea2)(75?)?1000
催化剂对速率常数的影响:ln==8.476 ? 8.314?298k(1)rt 尼乌斯公式 ln k(t2) ?
v2k(2)
23.答:(1)升高温度;减小总压或增大体积;增p(h2o)或减小p(co) 或减小p(h2) (2) 升高温度;增p(h2o)
其中升高温度使k?及k(正)、k(逆)都增大;总压力或分压的改变对k?及k(正)、k(逆)都无影响。 ??
??hm1?rsm?ea1
()?lna) ?(依据是: lnk? lnk? rtrtr 11
24.答:查标准热力学数据 n2(g)?o2(g)?no(g) 22 ? -1-1
sm(298.15k)
191.50205.138210.761 ?rs?m(298.15k)=12.442j.mol.k ?
j.mol?1.k?1 ? ?
?fhm(298.15k)0 0 90.25 ?rhm(298.15k)=90.25kj.mol-1 kj.mol?1 ?
??rgm(1573k)?70680
lnk(1573k)?== -5.4 k??e?5.4?4.5?10?3 8.314?1573r?1573 ?
【篇三:《c++大学教程第五版》课后习题答案 (作者
deitel)】
3.11
gradebook类定义:
#include string // program uses c++ standard string class using std::string; class gradebook {
public:
// constructor initializes course name and instructor name gradebook( string, string );
void setcoursename( string ); // function to set the course
name string getcoursename(); // function to retrieve the course name void setinstructorname( string ); // function to set instructor name string getinstructorname(); // function to retrieve instructor name void displaymessage(); // display welcome message and instructor name private:
string coursename; // course name for this gradebook
string instructorname; // instructor name for this gradebook }; // end class gradebook 类成员函数:
#include iostream using std::cout; using std::endl;
#include gradebook.h
// constructor initializes coursename and instructorname // with strings supplied as arguments
gradebook::gradebook( string course, string instructor ) { setcoursename( course ); // initializes coursename
setinstructorname( instructor ); // initializes instructorname } // end gradebook constructor
// function to set the course name
void gradebook::setcoursename( string name ) {
coursename = name; // store the course name } // end function setcoursename
// function to retrieve the course name string gradebook::getcoursename() {
return coursename;
} // end function getcoursename
// function to set the instructor name
void gradebook::setinstructorname( string name ) {
instructorname = name; // store the instructor name } // end function setinstructorname
// function to retrieve the instructor name string gradebook::getinstructorname() {
return instructorname;
} // end function getinstructorname
// display a welcome message and the instructors name void gradebook::displaymessage() {
// display a welcome message containing the course name cout welcome to the grade book for\\n getcoursename() ! endl;
// display the instructors name
cout this course is presented by: getinstructorname() endl; } // end function displaymessage 测试文件:
#include iostream using std::cout; using std::endl;
// include definition of class gradebook from gradebook.h
#include gradebook.h
// function main begins program execution int main() {
// create a gradebook object; pass a course name and instructor name gradebook gradebook(
cs101 introduction to c++ programming, professor smith ); // display initial value of instructorname of gradebook object cout gradebook instructor name is: gradebook.getinstructorname() \\n\\n;
// modify the instructorname using set function
gradebook.setinstructorname( assistant professor bates ); // display new value of instructorname
cout new gradebook instructor name is: gradebook.getinstructorname() \\n\\n;
// display welcome message and instructors name gradebook.displaymessage();
return 0; // indicate successful termination } // end main 3.12
类定义:
class account {
public:
account( int ); // constructor initializes balance
void credit( int ); // add an amount to the account balance void debit( int ); // subtract an amount from the account balance int getbalance(); // return the account balance private:
int balance; // data member that stores the balance }; // end class account 类成员函数:
#include iostream using std::cout; using std::endl;
#include account.h // include definition of class account // account constructor initializes data member balance account::account( int initialbalance ) {
balance = 0; // assume that the balance begins at 0
// if initialbalance is greater than 0, set this value as the // balance of the account; otherwise, balance remains 0 if ( initialbalance 0 )
balance = initialbalance;
// if initialbalance is negative, print error message if ( initialbalance 0 )
cout error: initial balance cannot be negative.\\n endl; } // end account constructor
// credit (add) an amount to the account balance void account::credit( int amount ) {
balance = balance + amount; // add amount to balance } // end function credit
// debit (subtract) an amount from the account balance void account::debit( int amount ) {
if ( amount balance ) // debit amount exceeds balance
cout debit amount exceeded account balance.\\n endl; if
( amount = balance ) // debit amount does not exceed balance balance = balance - amount; } // end function debit
// return the account balance int account::getbalance() {
return balance; // gives the value of balance to the calling function } // end function getbalance 测试函数:
#include iostream using std::cout; using std::cin; using std::endl;
// include definition of class account from account.h #include account.h
// function main begins program execution int main() {
account account1( 50 ); // create account object account account2( 25 ); // create account object // display initial balance of each object
cout account1 balance: $ account1.getbalance() endl; cout account2 balance: $ account2.getbalance() endl;
int withdrawalamount; // stores withdrawal amount read from user
cout \\nenter withdrawal amount for account1: ; // prompt cin withdrawalamount; // obtain user input
cout \\nattempting to subtract withdrawalamount from account1 balance\\n\\n;
account1.debit( withdrawalamount ); // try to subtract from account1
// display balances
cout account1 balance: $ account1.getbalance() endl; cout account2 balance: $ account2.getbalance() endl;
cout \\nenter withdrawal amount for account2: ; // prompt cin withdrawalamount; // obtain user input
cout \\nattempting to subtract withdrawalamount from account2 balance\\n\\n;
account2.debit( withdrawalamount ); // try to subtract from account2
// display balances
cout account1 balance: $ account1.getbalance() endl; cout account2 balance: $ account2.getbalance() endl; return 0; // indicate successful termination } // end main 3.13
类定义:
#include string // program uses c++ standard string class using std::string;
// invoice class definition class invoice {
public:
// constructor initializes the four data members invoice( string, string, int, int );
// set and get functions for the four data members void setpartnumber( string ); // part number string getpartnumber();
void setpartdescription( string ); // part description string getpartdescription();
void setquantity( int ); // quantity
int getquantity();
void setpriceperitem( int ); // price per item int getpriceperitem();
// calculates invoice amount by multiplying quantity x price per item int getinvoiceamount(); private:
string partnumber; // the number of the part being sold
string partdescription; // description of the part being sold int quantity; // how many of the items are being sold int priceperitem; // price per item }; // end class invoice 类成员函数:
#include iostream using std::cout; using std::endl;
// include definition of class invoice from invoice.h #include invoice.h
// invoice constructor initializes the classs four data members invoice::invoice( string number, string description, int count, int price ) {
setpartnumber( number ); // store partnumber
setpartdescription( description ); // store partdescription setquantity( count ); // validate and store quantity
setpriceperitem( price ); // validate and store priceperitem } // end invoice constructor // set part number
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- dfix.cn 版权所有 湘ICP备2024080961号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务