




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上C+ 大學(xué)基礎(chǔ)教程 課后答案(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 g
2、etCourseName(); / 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
3、for this GradeBook string instructorName; / instructor name for this GradeBook; / end class GradeBook類成員函數(shù):#include using std:cout;using std:endl;#include GradeBook.h/ constructor initializes courseName and instructorName / with strings supplied as argumentsGradeBook:GradeBook( string course, string
4、 instructor ) 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 setCourseNam
5、e/ function to 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
6、 retrieve 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 getC
7、ourseName() ! 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 executio
8、nint 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() nn; / modify th
9、e instructorName 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
10、 successful 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 bala
11、nce; / data member that stores the balance; / end class Account類成員函數(shù):#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
12、 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 ba
13、lance ) / 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 t
14、o the calling function / end function getBalance測試函數(shù):#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 sub
16、tract 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 ma
18、in3.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 number
19、 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
20、 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類成員函數(shù):#include using std:cout;using std:en
21、dl;/ 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 par
22、tDescription 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 number
23、string 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:getPartDescription() r
24、eturn 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 to 0 c
25、out 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 itemint Invoic
26、e:getPricePerItem() return pricePerItem; / end function getPricePerItem/ calulates invoice amount by multiplying quantity x price per itemint Invoice:getInvoiceAmount() return getQuantity() * getPricePerItem(); / end function getInvoiceAmount測試函數(shù):#include using std:cout;using std:cin;using std:endl;
27、/ 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.getPartNumber() endl; co
28、ut 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( ); invoice.setPartDescription( Sa
29、w ); 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: invoice.getPartDe
30、scription() 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;/
31、 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 last name void setMont
32、hlySalary( 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類成員函數(shù):#include using std:cout;#include Employee.h / Employ
33、ee 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 salary / end Employee const
34、ructor/ 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;
35、/ 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 monthlySalary = salary; /
36、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測試函數(shù):#include using std:cout;using std:en
37、dl;#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 Employees yearly salaries: end
38、l; / 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 monthlySalary2 = employee2
39、.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 yearly salary again cout nEmploye
40、es 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.getMonthlySalary(); cout employee2
41、.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(); / return month void setDay( int ); /
42、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 class Date類成員函數(shù):#include using st
43、d: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 ); setYear( y ); / end Date constructo
44、r / set monthvoid 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() return day; / end function getDa
45、y/ 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測試函數(shù):#include using std:cout;using std:endl;#i
46、nclude Date.h / include definition of class Date from Date.h/ function main begins program executionint 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; cout Day: date.getDay() endl; cout Year
47、: 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 date (1/1/2005) return 0; / indicate succ
48、essful 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 Complex #endif類成員函數(shù):#include using std:cout; #include Complex.hComplex:Compl
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年春季初三中考沖刺高級研修第4講 (共28張)
- 物聯(lián)網(wǎng)推動能源管理的創(chuàng)新
- 預(yù)防醫(yī)學(xué)之食物中毒及其防治
- 防傳染病安全教育中班
- 預(yù)防冒充領(lǐng)導(dǎo)詐騙
- 養(yǎng)生知識課堂
- 鄉(xiāng)村公路建設(shè)項(xiàng)目可行性實(shí)施方案
- 一體化城市指揮管理平臺建設(shè)方案
- 畢節(jié)幼兒師范高等??茖W(xué)?!锻馐露Y儀》2023-2024學(xué)年第二學(xué)期期末試卷
- 濟(jì)南大學(xué)《病毒學(xué)實(shí)驗(yàn)技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- JCT640-2010 頂進(jìn)施工法用鋼筋混凝土排水管
- GD-C1-316工程項(xiàng)目管理人員變更通知書
- 利巴韋林顆粒劑車間設(shè)計
- 新疆的名勝古跡
- 第11課 動物的花衣裳(說課稿)2022-2023學(xué)年美術(shù)三年級下冊 人教版
- 現(xiàn)代漢語專題學(xué)習(xí)通超星課后章節(jié)答案期末考試題庫2023年
- 成語故事鵬程萬里
- 《思想道德與法治》2021版第四章
- 石灰石破碎崗位風(fēng)險告知卡
- 催收服務(wù)公司招聘管理制度
- 以Fe3+催化過氧化氫分解機(jī)理為例的項(xiàng)目式學(xué)習(xí)
評論
0/150
提交評論