实验11_参考答案_第1页
实验11_参考答案_第2页
实验11_参考答案_第3页
实验11_参考答案_第4页
实验11_参考答案_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

1、实验11类与对象2(2)请将自己报告部分,文字和程序代码,加粗完成下面实验内容和实验要求。 自测练习 1编程练习:简易计算器编写一个SimpleCalculator类,包含一些public方法来对两个double型变量进行加减乘除运算。实例输出: 将下面程序代码补充完整,并运行(注意:共有3个文件:SimpleCalculator.h, SimpleCalculator.cpp, CalcDriver.cpp,此3个文件放到一个工程中进行调试)/ SimpleCalculator.hclass SimpleCalculator public:/*补充 add方法的原型声明 */double s

2、ubtract ( double, double ) const;double multiply( double, double ) const;/* 补充divide方法的原型声明 */;/ SimpleCalculator.cpp#include “SimpleCalculator.h”/ 补充add方法的定义double SimpleCalculator:subtract( double a, double b ) constreturn a b;double SimpleCalculator:multiply( double a, double b ) constreturn a *

3、b;/补充 divide方法的定义/ CalcDriver.cpp#include < iostream>#include “SimpleCalculator.h”using namespace std;int main()double a=10.0;double b=20.0;/ 声明SimpleCalculator类的对象sccout << “The value of a is: “<<a<<endl;cout <<”The value of b is: “ << b<< endl;/*写一条语句,声明变量

4、addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量 */cout << “Adding a and b yields “<< addition << endl;double substraction = sc.substract ( a, b );cout << “Subtracting b from a “<< subtraction << endl;double multiplication = sc.multiply ( a, b );cout <&l

5、t; “Multiplying a and b yields “<< multiplication << endl;/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量 */cout << “Dividing a by b yields “<< division<< endl;return 0;补充后代码:/ SimpleCalculator.hclass SimpleCalculator public:/*补充 add方法的原型声明 */double

6、add( double, double ) const;double substract( double, double ) const;double multiply( double, double ) const;double divide( double, double ) const;/* 补充divide方法的原型声明 */;/ SimpleCalculator.cpp#include "SimpleCalculator.h"double SimpleCalculator:add(double a,double b)constreturn a + b;double

7、 SimpleCalculator:substract( double a, double b ) constreturn a - b;double SimpleCalculator:multiply( double a, double b ) constreturn a * b;double SimpleCalculator:divide(double a,double b)constreturn a / b;/ CalcDriver.cpp#include < iostream>#include "SimpleCalculator.h"using names

8、pace std;int main()double a=10.0;double b=20.0;SimpleCalculator sc;/ 声明SimpleCalculator类的对象sccout << "The value of a is: "<<a<<endl;cout <<"The value of b is: " << b<< endl;/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量 */d

9、ouble addition = sc.add ( a, b );cout << "Adding a and b yields "<< addition << endl;double substraction = sc.substract ( a, b );cout << "Subtracting b from a "<< substraction << endl;double multiplication = sc.multiply ( a, b );cout << &

10、quot;Multiplying a and b yields "<< multiplication << endl;/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量 */double division = sc.divide (a,b);cout << "Dividing a by b yields "<< division<< endl;return 0;运行结果:修改上面创建的类,在SimpleCalculator

11、类中创建一个名为answer的private型数据成员,执行某项运算后,将运算结果赋给answer。增加一个名为getAnswer的函数,其功能为检索对象的最后一次算术运算结果。同时,再给SimpleCalculator类增加一个构造函数,将answer的值初始化为0.修改后代码:/ SimpleCalculator.hclass SimpleCalculator public:/*补充 add方法的原型声明 */SimpleCalculator();double add( double, double ) ;double substract( double, double ) ;double

12、 multiply( double, double ) ;double divide( double, double ) ;double getAnswer()const;/* 补充divide方法的原型声明 */private:double answer;/ SimpleCalculator.cpp#include "SimpleCalculator.h"SimpleCalculator:SimpleCalculator()answer=0;double SimpleCalculator:add(double a,double b)answer = a + b;retur

13、n answer;double SimpleCalculator:substract( double a, double b ) answer = a - b;return answer;double SimpleCalculator:multiply( double a, double b ) answer = a * b;return answer;double SimpleCalculator:divide(double a,double b)answer = a / b;return answer;double SimpleCalculator:getAnswer() constret

14、urn answer;/ CalcDriver.cpp#include < iostream>#include "SimpleCalculator.h"using namespace std;int main()double a=10.0;double b=20.0;SimpleCalculator sc;/ 声明SimpleCalculator类的对象sccout << "The value of a is: "<<a<<endl;cout <<"The value of b is:

15、 " << b<< endl;/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量 */sc.add ( a, b );cout << "Adding a and b yields "<<sc.getAnswer()<< endl;sc.substract ( a, b );cout << "Subtracting b from a "<<sc.getAnswer()<<

16、; endl;sc.multiply ( a, b );cout << "Multiplying a and b yields "<<sc.getAnswer()<< endl;/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量 */sc.divide (a,b);cout << "Dividing a by b yields "<<sc.getAnswer()<< endl;return 0;运行结

17、果修改程序,给SimpleCalculator类增加input函数,使用户可以自己输入两个double型变量。函数将输入的值存储在类新增private成员数据a和b中。再给SimpleCalculator类创建两个构造函数,一个没有参数,将a和b初始化为0;另一个将输入的两个double型变量作为参数,并将a和b初始化为输入的值。最后,创建一个函数printValues,将a和b的值显示出来。修改后代码:/ SimpleCalculator.hclass SimpleCalculator public:/*补充 add方法的原型声明 */SimpleCalculator();double ad

18、d( double, double ) ;double substract( double, double ) ;double multiply( double, double ) ;double divide( double, double ) ;double getAnswer()const;void input(double ,double );void printValues();/* 补充divide方法的原型声明 */private:double answer;double a;double b;/ SimpleCalculator.cpp#include < iostrea

19、m>#include "SimpleCalculator.h"SimpleCalculator:SimpleCalculator()answer=0;a=0;b=0;double SimpleCalculator:add(double a,double b)answer = a + b;return answer;double SimpleCalculator:substract( double a, double b ) answer = a - b;return answer;double SimpleCalculator:multiply( double a,

20、double b ) answer = a * b;return answer;double SimpleCalculator:divide(double a,double b)answer = a / b;return answer;double SimpleCalculator:getAnswer() constreturn answer;void SimpleCalculator:input(double pA,double pB)a=pA;b=pB;void SimpleCalculator:printValues()std:cout<<"a: "<

21、;<a<<"tb: "<<b<<std:endl;/ CalcDriver.cpp#include < iostream>#include "SimpleCalculator.h"using namespace std;int main()double a=10.0;double b=20.0;SimpleCalculator sc;/ 声明SimpleCalculator类的对象sccout << "The value of a is: "<<a<&

22、lt;endl;cout <<"The value of b is: " << b<< endl;/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量 */sc.add ( a, b );cout << "Adding a and b yields "<<sc.getAnswer()<< endl;sc.substract ( a, b );cout << "Subtracting

23、 b from a "<<sc.getAnswer()<< endl;sc.multiply ( a, b );cout << "Multiplying a and b yields "<<sc.getAnswer()<< endl;/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量 */sc.divide (a,b);cout << "Dividing a by b yields "<

24、;<sc.getAnswer()<< endl;sc.printValues();sc.input(20.0,10.0);sc.printValues();return 0;运行结果2【程序输出练习】有如下的Time类定义class Time public:Time( );/ constructorvoid setTime ( int, int, int );/ set hour minute secondvoidPrintMilitary ( );/ print military time formatvoid PrintStandard ( );/ print stand

25、ard time formatprivate:int hour;/ 0-23int minute;/ 0-59int second;/ 0-59;Time:Time() hour=minute=second=0;void Time:setTime( int h, int m , int s)hour = (h>=0 && h<24 )?h:0;minute=(m>=0 && m<60)?m:0; second=(s>=0 && s<60)?s:0;void Time:printMilitary()cout &l

26、t;<( hour<10?”0”: “”) <<hour <<”:”<<(minute<10 ? “0” : “”)<<minute;void Time:printStandard()cout << ( ( hour=0 | hour =12) ? 12 : hour % 12 )<<”:” << ( minute<10 ? “0” : “”) << minute<<”:” << ( second<10 ? “0” : “”) <<

27、second<< ( hour < 12 ? “AM” : “PM” ); 假设采用中的Time类定义,以下语句的输出结果是什么?(请阅读分析,不要运行代码) Time t (3, 4, 5);编译出错,没有上述构造函数,应该是Time t;t.setTime(3,4,5)。并且后三问最好按照(3,4,5)进行t.printStandard();cout<<endl;输出结果:3:04:05AMt.printMilitary();cout<<endl;输出结果:03:04t.setTime(99,3,4);t.printMilitary();cout&

28、lt;<endl;输出结果:00:03写出下面Time类的两个构造函数的名字和功能,正确的Time类定义是否可以同时包含以下的两个构造函数?如果不可以,请说明原因。Time( int h=0, int m = 0, int s = 0);Time ( );答:不可以。当创建无参数对象的时候,比如Time t;,Time( int h=0, int m = 0, int s = 0);和Time ( );会被编译器当成同样的函数,不知道要调用哪个函数。3.【程序调试练习】下列程序运行不正确。请改正其中的语法错误使得程序可以编译成功。程序编译后,请比较输出结果与实例输出,并对可能存在的逻辑错

29、误进行修改。实例输出显示了程序如果正确应该输出的结果。实例输出:错误的代码:(注意:共有3个文件:card.h, card.cpp, debugging.cpp,此3个文件放到一个工程中进行调试)第一个文件card.h#ifndef CARD_H#define CARD_Hclass Cardpublic:void Card();void Card( int s, int v);void Card();void setSuit( int s);void setValue( int v);int getSuit() const;int getValue() const;void print()

30、const;private:int suit=4;int value=1;bool validCard() const;#endif第二个文件card.cpp#include <iostream>using namespace std;void Card:Card() suit=4; value=1;Card:Card(int s, int v) suit=s; value=v;Card:Card() cout<< "The destructor has been invokedn"void Card:setSuit(int s) suit=s;vo

31、id Card:setValue( int v) value=v; void print()if (!validCard() ) cout << "This card is not validn"return;cout<<"This is the: "switch(value)case 1: cout <<"Ace "break;case 11:cout<<"Jack "break;case 12:cout<<"Queen "break

32、;case 13:cout << "King "breawk;default:cout<<value<<" "switch(suit) case 1:cout <<"of clubsn"break;case 2:cout<<"of diamondsn"break;case 3:cout<<"of heartsn"break;case 4:cout<<"of spadesn"break;defa

33、ult:cout<<"ninvalid suitn"int Card:getSuit() return suit;int Card:getValue() return value;bool validCard()return value>=1 && value <=13 && suit >=1 && suit <=4;void Card:setValue(int v) value=v;void print()if (!validCard()cout<<"This car

34、d is not validn"return;cout <<"This is the: "switch(value)case 1: cout <<"Ace "break;case 11:cout<<"Jack "break;case 12:cout<<"Queen "break;case 13:cout << "King "break;default:cout<<value<<" "

35、switch(suit) case 1:cout <<"of clubsn"break;case 2:cout<<"of diamondsn"break;case 3:cout<<"of heartsn"break;case 4:cout<<"of spadesn"break;default:cout<<"ninvalid suitn"int Card:getSuit() return suit;int Card:getValue()

36、return value;bool validCard()return value>=1 && value <=13 && suit >=1 && suit <=4;第三个文件debugging.cpp#include <iostream>using namespace std;int main()Card c1, c2(3, 4), c3(1, 14);Card *p1=&c2;c1.print();c2.print();c3.print();p1->print();cout<<en

37、dl;c1.setSuit(p1->getSuit();c3.value=12;p1->value=5;c1.print();c2.print();c3.print();*p1.print();cout<<endl;return 0;修改后代码/card.h#ifndef CARD_H#define CARD_Hclass Cardpublic:Card();Card( int s, int v);Card();void setSuit( int s);void setValue( int v);int getSuit() const;int getValue() co

38、nst;void print() const;private:int suit;int value;bool validCard() const;#endif/card.cpp#include <iostream>#include "card.h"using namespace std;Card:Card() suit=4; value=1;Card:Card(int s, int v) suit=s; value=v;Card:Card() cout<< "The destructor has been invokedn"voi

39、d Card:setSuit(int s) suit=s;void Card:setValue( int v) value=v; void Card:print() constif (!validCard() ) cout << "This card is not validn"return;cout<<"This is the: "switch(value)case 1: cout <<"Ace "break;case 11:cout<<"Jack "break;c

40、ase 12:cout<<"Queen "break;case 13:cout << "King "break;default:cout<<value<<" "switch(suit) case 1:cout <<"of clubsn"break;case 2:cout<<"of diamondsn"break;case 3:cout<<"of heartsn"break;case 4:cou

41、t<<"of spadesn"break;default:cout<<"ninvalid suitn"bool Card:validCard() constreturn value>=1 && value <=13 && suit >=1 && suit <=4;int Card:getSuit()const return suit;int Card:getValue()const return value;/debugging.cpp#include <

42、;iostream>#include "card.h"using namespace std;int main()Card c1, c2(3, 4), c3(1, 14);Card *p1=&c2;c1.print();c2.print();c3.print();p1->print();cout<<endl;c1.setSuit(p1->getSuit();c3.setValue(12);p1->setValue(5);c1.print();c2.print();c3.print();p1->print();cout<

43、<endl;return 0; 运行结果4编程题 题目:创建一个SavingAccount类,依据下列要求完成该类的定义。 使用一个static数据成员annualInterestRate保存每个存款者的年利率。 类的每个对象都包含一个private数据成员savingsBalance,用以指示存款者目前的存款金额。 该类提供成员函数calculateMonthlyInterest,它将余额乘以annualInterestRate再除以12来计算月利息,这个利息应该加到savingsBalance中。 该类还提供一个static成员函数modifyInterestRate,它将stati

44、c的annualInterestRate设置为一个新值。 编写一段程序代码测试SavingAccount类。实例化SavingAccount类的两个对象saver1和saver2,余额分别是2000.00元和3000.00元。将annualInterestRate设置为3%,然后计算月利率并打印每个存款者的新余额。接着再将annualInterestRate设置为4%,计算下一个月的利息并打印每个存款者的新余额。要求:根据题目要求,写出类和应用程序的设计思路和程序代码。 思路: 类设计: 应用程序设计:(主函数中,定义对象及调用对象的成员函数)程序代码:/SavingAccount.hclass SavingAccountpublic:SavingAccount(double);SavingAccount();void calculateMonthlyInterest();void printBalance(); static void modifyInterestRate(double );p

温馨提示

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

评论

0/150

提交评论