《C++大学教程第五版》课后习题答案--(作者-DEITEL)_第1页
《C++大学教程第五版》课后习题答案--(作者-DEITEL)_第2页
《C++大学教程第五版》课后习题答案--(作者-DEITEL)_第3页
《C++大学教程第五版》课后习题答案--(作者-DEITEL)_第4页
《C++大学教程第五版》课后习题答案--(作者-DEITEL)_第5页
已阅读5页,还剩67页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

6、 the 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 instruc

9、torName 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 successf

10、ul termination / 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;

11、/ data member 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

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

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

14、 the calling 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 ); /

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

16、ract withdrawalAmount 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

17、to subtract 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 mai

18、n推荐精选3.13类定义:#include / 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 num

19、ber 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 i

20、tem 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 using std:cout;using std

21、:endl;/ include 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

22、partDescription setQuantity( count ); / validate and store quantity setPricePerItem( price ); / validate and store pricePerItem / end Invoice constructor/ set part number推荐精选void Invoice:setPartNumber( string number ) partNumber = number; / no validation needed / end function setPartNumber/ get part

23、 numberstring Invoice: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:getPartDescript

24、ion() return partDescription; / 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

25、 to 0 cout 0 ) / if 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 it

26、emint Invoice:getPricePerItem() 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;usi

27、ng std:endl;/ include definition 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.getPartNumbe

28、r() endl; cout Part description: 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.set

29、PartDescription( Saw ); invoice.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

30、: invoice.getPartDescription() 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

31、classusing std:string;/ Employee 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

32、 last name void setMonthlySalary( 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;#incl

33、ude Employee.h / Employee class 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 sala

34、ry / end Employee constructor/ 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 n

35、ame )推荐精选 lastName = name; / no validation 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

36、 monthlySalary = salary; / set monthlySalary 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

37、using std:cout;using std:endl;#include 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 Emp

38、loyees yearly salaries: endl; / retrieve 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

39、 monthlySalary2 = employee2.getMonthlySalary(); 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 ye

40、arly salary again cout nEmployees yearly 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.get

41、MonthlySalary(); cout employee2.getFirstName() employee2.getLastName() : $ monthlySalary2 * 12 endl; return 0; / indicate successful termination / end main3.15类定义:class Date public: Date( int, int, int ); / constructor initializes data members void setMonth( int ); / set month int getMonth(); / retu

42、rn month void setDay( int ); / set day int getDay(); / return day void setYear( int ); / set year int getYear(); / return year void displayDate(); / displays date in mm/dd/yyyy formatprivate: int month; / the month of the date int day; / the day of the date int year; / the year of the date; / end cl

43、ass Date类成员函数:#include using std:cout;using std:endl;#include Date.h / include definition of class Date from Date.h/ Date constructor that initializes the three data members;/ assume values provided are correct (really should validate)Date:Date( int m, int d, int y ) setMonth( m ); setDay( d ); setY

44、ear( y ); / end Date constructor / set month推荐精选void Date:setMonth( int m ) month = m; if ( month 12 ) month = 1; / end function setMonth/ return monthint Date:getMonth() return month; / end function getMonth/ set dayvoid Date:setDay( int d ) day = d; / end function setDay/ return dayint Date:getDay

45、() return day; / end function getDay/ set yearvoid Date:setYear( int y ) year = y; / end function setYear/ return yearint Date:getYear() return year; / end function getYear/ print Date in the format mm/dd/yyyyvoid Date:displayDate() cout month / day / year endl; / end function displayDate测试函数:#inclu

46、de using std:cout;using std:endl;#include Date.h / include definition of class Date from Date.h/ function main begins program execution推荐精选int main() Date date( 5, 6, 1981 ); / create a Date object for May 6, 1981 / display the values of the three Date data members cout Month: date.getMonth() endl;

47、cout Day: date.getDay() endl; cout Year: date.getYear() endl; cout nOriginal date: endl; date.displayDate(); / output the Date as 5/6/1981 / modify the Date date.setMonth( 13 ); / invalid month date.setDay( 1 ); date.setYear( 2005 ); cout nNew date: endl; date.displayDate(); / output the modified da

48、te (1/1/2005) return 0; / indicate successful termination / end main9.05类定义:#ifndef COMPLEX_H#define COMPLEX_Hclass Complex public: Complex( double = 0.0, double = 0.0 ); / default constructor Complex add( const Complex & ); / function add Complex subtract( const Complex & ); / function subtract void printComplex(); / print complex number format void setComplexNumber( double, double ); / set complex number private: double realPart; double imaginaryPart; / end class C

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论