文库网
ImageVerifierCode 换一换
首页 文库网 > 资源分类 > DOC文档下载
分享到微信 分享到微博 分享到QQ空间

C大学教程第五版课后习题答案(作者DEITEL).doc

  • 资源ID:21763642       资源大小:248.50KB        全文页数:52页
  • 资源格式: DOC        下载积分:20文币
微信登录下载
快捷下载 游客一键下载
账号登录下载
三方登录下载: QQ登录 微博登录
二维码
扫码关注公众号登录
下载资源需要20文币
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 
账号:
密码:
验证码:   换一换
  忘记密码?
    
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

C大学教程第五版课后习题答案(作者DEITEL).doc

1、C+ 大学基础教程 课后答案(DEITEL)版3.11GradeBook类定义:#include / program uses C+ standard string classusing std:string;class GradeBookpublic: / constructor initializes course name and instructor name GradeBook( string, string ); void setCourseName( string ); / function to set the course name string getCourseName(

2、); / 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 nameprivate: string courseName; / course name for this Grad

3、eBook string instructorName; / instructor name for this GradeBook; / end class GradeBook类成员函数:#include using std:cout;using std:endl;#include GradeBook.h/ constructor initializes courseName and instructorName / with strings supplied as argumentsGradeBook:GradeBook( string course, string instructor )

4、 setCourseName( course ); / initializes courseName setInstructorName( instructor ); / initialiZes instructorName / end GradeBook constructor/ function to set the course namevoid GradeBook:setCourseName( string name ) courseName = name; / store the course name / end function setCourseName/ function t

5、o retrieve the course namestring GradeBook:getCourseName() return courseName; / end function getCourseName/ function to set the instructor namevoid GradeBook:setInstructorName( string name ) instructorName = name; / store the instructor name / end function setInstructorName/ function to retrieve the

6、 instructor namestring GradeBook:getInstructorName() return instructorName; / end function getInstructorName/ display a welcome message and the instructors namevoid GradeBook:displayMessage() / display a welcome message containing the course name cout Welcome to the grade book forn getCourseName() !

7、 endl; / display the instructors name cout This course is presented by: getInstructorName() endl; / end function displayMessage测试文件:#include using std:cout; using std:endl;/ include definition of class GradeBook from GradeBook.h#include GradeBook.h/ function main begins program executionint main() /

8、 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() nn; / modify the instructorN

9、ame using set function gradeBook.setInstructorName( Assistant Professor Bates ); / display new value of instructorName cout new gradeBook instructor name is: gradeBook.getInstructorName() nn; / display welcome message and instructors name gradeBook.displayMessage(); return 0; / indicate successful t

10、ermination / end main3.12类定义:class Accountpublic: 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 balanceprivate: int balance; / data m

11、ember that stores the balance; / end class Account类成员函数:#include using std:cout;using std:endl;#include Account.h / include definition of class Account/ Account constructor initializes data member balanceAccount:Account( int initialBalance ) balance = 0; / assume that the balance begins at 0 / if in

12、itialBalance 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 balance ) / deb

13、it 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 balanceint Account:getBalance() return balance; / gives the value of balance to the calling

14、 function / end function getBalance测试函数:#include using std:cout;using std:cin;using std:endl;/ include definition of class Account from Account.h#include Account.h/ function main begins program executionint main() Account account1( 50 ); / create Account object Account account2( 25 ); / create Accou

15、nt 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 withdrawalAmount; / obtain user input cout nattempting to subtract withdra

16、walAmount from account1 balancenn; account1.debit( withdrawalAmount ); / try to subtract from account1 / display balances cout account1 balance: $ account1.getBalance() endl; cout account2 balance: $ account2.getBalance() endl; cout withdrawalAmount; / obtain user input cout nattempting to subtract

17、withdrawalAmount from account2 balancenn; 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 main3.13类定义:#in

18、clude / program uses C+ standard string classusing std:string;/ Invoice class definitionclass Invoicepublic: / 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 getPa

19、rtNumber(); 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 getInvoi

20、ceAmount(); 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 using std:cout;using std:endl;/ include

21、definition of class Invoice from Invoice.h#include Invoice.h/ Invoice constructor initializes the classs four data membersInvoice:Invoice( string number, string description, int count, int price ) setPartNumber( number ); / store partNumber setPartDescription( description ); / store partDescription

22、setQuantity( count ); / validate and store quantity setPricePerItem( price ); / validate and store pricePerItem / end Invoice constructor/ set part numbervoid Invoice:setPartNumber( string number ) partNumber = number; / no validation needed / end function setPartNumber/ get part numberstring Invoic

23、e:getPartNumber() return partNumber; / end function getPartNumber/ set part descriptionvoid Invoice:setPartDescription( string description ) partDescription = description; / no validation needed / end function setPartDescription/ get part descriptionstring Invoice:getPartDescription() return partDes

24、cription; / end function getPartDescription/ set quantity; if not positive, set to 0void Invoice:setQuantity( int count ) if ( count 0 ) / if quantity is positive quantity = count; / set quantity to count if ( count = 0 ) / if quantity is not positive quantity = 0; / set quantity to 0 cout 0 ) / if

25、price is positive pricePerItem = price; / set pricePerItem to price if ( price = 0 ) / if price is not positive pricePerItem = 0; / set pricePerItem to 0 cout npricePerItem cannot be negative. pricePerItem set to 0.n; / end if / end function setPricePerItem/ get price per itemint Invoice:getPricePer

26、Item() return pricePerItem; / end function getPricePerItem/ calulates invoice amount by multiplying quantity x price per itemint Invoice:getInvoiceAmount() return getQuantity() * getPricePerItem(); / end function getInvoiceAmount测试函数:#include using std:cout;using std:cin;using std:endl;/ include def

27、inition of class Invoice from Invoice.h#include Invoice.h/ function main begins program executionint main() / create an Invoice object Invoice invoice( 12345, Hammer, 100, 5 ); / display the invoice data members and calculate the amount cout Part number: invoice.getPartNumber() endl; cout Part descr

28、iption: invoice.getPartDescription() endl; cout Quantity: invoice.getQuantity() endl; cout Price per item: $ invoice.getPricePerItem() endl; cout Invoice amount: $ invoice.getInvoiceAmount() endl; / modify the invoice data members invoice.setPartNumber( 123456 ); invoice.setPartDescription( Saw ); i

29、nvoice.setQuantity( -5 ); /negative quantity,so quantity set to 0 invoice.setPricePerItem( 10 ); cout nInvoice data members modified.nn; / display the modified invoice data members and calculate new amount cout Part number: invoice.getPartNumber() endl; cout Part description: invoice.getPartDescript

30、ion() endl; cout Quantity: invoice.getQuantity() endl; cout Price per item: $ invoice.getPricePerItem() endl; cout Invoice amount: $ invoice.getInvoiceAmount() endl; return 0; / indicate successful termination / end main3.14类定义:#include / program uses C+ standard string classusing std:string;/ Emplo

31、yee class definitionclass Employee public: Employee( string, string, int ); / constructor sets data members void setFirstName( string ); / set first name string getFirstName(); / return first name void setLastName( string ); / set last name string getLastName(); / return last name void setMonthlySal

32、ary( int ); / set weekly salary int getMonthlySalary(); / return weekly salaryprivate: string firstName; / Employees first name string lastName; / Employees last name int monthlySalary; / Employees salary per month; / end class Employee类成员函数:#include using std:cout;#include Employee.h / Employee cla

33、ss definition/ Employee constructor initializes the three data membersEmployee:Employee( string first, string last, int salary ) setFirstName( first ); / store first name setLastName( last ); / store last name setMonthlySalary( salary ); / validate and store monthly salary / end Employee constructor

34、/ set first namevoid Employee:setFirstName( string name ) firstName = name; / no validation needed / end function setFirstName/ return first namestring Employee:getFirstName() return firstName; / end function getFirstName/ set last namevoid Employee:setLastName( string name ) lastName = name; / no v

35、alidation needed / end function setLastName/ return last namestring Employee:getLastName() return lastName; / end function getLastName/ set monthly salary; if not positive, set to 0.0void Employee:setMonthlySalary( int salary ) if ( salary 0 ) / if salary is positive monthlySalary = salary; / set mo

36、nthlySalary to salary if ( salary = 0 ) / if salary is not positive monthlySalary = 0; / set monthlySalary to 0.0 / end function setMonthlySalary/ return monthly salaryint Employee:getMonthlySalary() return monthlySalary; / end function getMonthlySalary测试函数:#include using std:cout;using std:endl;#in

37、clude Employee.h / include definition of class Employee/ function main begins program executionint main() / create two Employee objects Employee employee1( Lisa, Roberts, 4500 ); Employee employee2( Mark, Stein, 4000 ); / display each Employees yearly salary cout Employees yearly salaries: endl; / r

38、etrieve and display employee1s monthly salary multiplied by 12 int monthlySalary1 = employee1.getMonthlySalary(); cout employee1.getFirstName() employee1.getLastName() : $ monthlySalary1 * 12 endl; / retrieve and display employee2s monthly salary multiplied by 12 int monthlySalary2 = employee2.getMo

39、nthlySalary(); cout employee2.getFirstName() employee2.getLastName() : $ monthlySalary2 * 12 endl; / give each Employee a 10% raise employee1.setMonthlySalary( monthlySalary1 * 1.1 ); employee2.setMonthlySalary( monthlySalary2 * 1.1 ); / display each Employees yearly salary again cout nEmployees yea

40、rly salaries after 10% raise: endl; / retrieve and display employee1s monthly salary multiplied by 12 monthlySalary1 = employee1.getMonthlySalary(); cout employee1.getFirstName() employee1.getLastName() : $ monthlySalary1 * 12 endl; monthlySalary2 = employee2.getMonthlySalary(); cout employee2.getFirstName() employee2.getLastName() : $ monthlySalary2 * 12 endl; return 0; / indicate successful termination / en


注意事项

本文(C大学教程第五版课后习题答案(作者DEITEL).doc)为本站会员(初中学霸)主动上传,文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知文库网(点击联系客服),我们立即给予删除!




关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

文库网用户QQ群:731843829  微博官方号:文库网官方   知乎号:文库网

Copyright© 2025 文库网 wenkunet.com 网站版权所有世界地图

经营许可证编号:粤ICP备2021046453号   营业执照商标

1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png