收藏 分销(赏)

2023年C++面向对象程序设计习题解答与上机指导(第二版)源程序.doc

上传人:二*** 文档编号:4688168 上传时间:2024-10-09 格式:DOC 页数:120 大小:256.54KB 下载积分:5 金币
下载 相关 举报
2023年C++面向对象程序设计习题解答与上机指导(第二版)源程序.doc_第1页
第1页 / 共120页
本文档共120页,全文阅读请下载到手机保存,查看更方便
资源描述
C++面向对象程序设计习题解答与上机指导(第2版) 习题参考答案源代码 使用源程序的几点注意事项 (1) 由于源程序在复制、编辑、解压缩等过程中也许引起部分符号(重要是标点符号,如分号、冒号、逗号、引号)的字体、半全角等发生变化,在编译时也许被检出语法错误,只要使用“替换”功能,纠正后即能顺利运营。 (2) 有的C++系统(如Visual C++6.0)没有完全实现C++标准,它所提供的不带后缀的.h的头文献不支持友元运算符重载函数,在Visual C++6.0中编译会犯错,这时可采用带后缀的.h头文献。将程序中的 #include<iostream> using namespace std; 修改成 #include<iostream.h> 即可顺利运营。 第2章 C++基础 【2.2】下面是一个C程序,改写它,使它采用C++风格的I/O语句。 #include<stdio.h> int main() { int a,b,d,min; printf("Enter two numbers:"); scanf("%d%d",&a,&b); min=a>b? b:a; for (d=2; d<min; d++) if (((a%d)==0)&&((b%d)==0)) break; if (d==min) { printf("No common denominators\n"); return 0; } printf("The lowest common denominator is %d\n",d); return 0; } 【解】 #include<iostream> using namespace std; int main() { int a,b,d,min; cout<<"Enter two numbers:"; cin>>a; cin>>b; min=a>b? b:a; for (d=2; d<min; d++) if (((a%d)==0)&&((b%d)==0)) break; if (d==min) { cout<<"No common denominators\n"; return 0; } cout<<"The lowest common denominator is "<<endl<<d; return 0; } 【2.24】写出下列程序的运营结果。 #include<iostream> using namespace std; int i=15; int main() { int i; i=100; ::i=i+1; cout<<::i<<endl; return 0; } 运营结果:101 Please any key to continue。 【2.25】写出下列程序的运营结果。 #include<iostream> using namespace std; void f(int &m,int n) { int temp; temp=m; m=n; n=temp; } int main() { int a=5,b=10; f(a,b); cout<<a<<" "<<b<<endl; return 0; } 结果:10 10 Please any key to continue。 【2.26】分析下面程序的输出结果。 #include<iostream> using namespace std; int &f(int &i) { i+=10; return i; } int main() { int k=0; int &m=f(k); cout<<k<<endl; m=20; cout<<k<<endl; return 0; } 运营结果:10 20 Please any key to continue。 【2.27】 编写一个C++风格的程序,用动态分派空间的方法计算Fibonacci数列的前20项并存储到动态分派的空间中。 【解】实现本题功能的程序如下: #include<iostream> using namespace std; int main() { int *p=new int[20]; //动态分派20个整型内存空间 *p=1; *(p+1)=1; //对前面2个内存空间赋值1 cout<<*p<<"\t"<<*(p+1)<<"\t"; p=p+2; //p指向第3个内存空间 for (int i=3;i<=20;i++) { *p=*(p-1)+*(p-2); cout<<*p<<"\t"; if (i%5==0) cout<<endl; p++; //p指向下一个内存空间; } return 0; } 【2.28】 编写一个C++风格的程序,建立一个被称为sroot的函数,返回其参数的二次方根。重载函数sroot三次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt)。 【解】实现本题功能的程序如下: #include<iostream> #include<cmath> using namespace std; double sroot(int i) { return sqrt(i); } double sroot(long l) { return sqrt(l); } double sroot(double d) { return sqrt(d); } int main() { int i=12; long l=1234; double d=12.34; cout<<"i的二次方根是:"<<sroot(i)<<endl; cout<<"l的二次方根是:"<<sroot(l)<<endl; cout<<"d的二次方根是:"<<sroot(d)<<endl; return 0; } 【2.29】 编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、5分的硬币,有多少种换法? 【解】实现本题功能的程序如下: #include<iostream> using namespace std; int main() { int i,j,sum=0;; for(i=0;i<=20;i++) for (j=0;j<=50;j++) if (100-5*i-2*j>=0) { sum++; cout<<100-5*i-2*j<<"\t"<<j<<"\t"<<i<<endl; } cout<<"sum is "<<sum<<endl; return 0; } 【2.30】编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序输出。规定使用变量的引用。 【解】实现本题功能的程序如下: #include<iostream> using namespace std; int main() { void change(int &,int &); int a,b; cin>>a>>b; if(a>b)change(a,b); cout<<a<<" "<<b<<endl; return 0; } void change(int &a1,int &b1) { int temp; temp=a1; a1=b1; b1=temp; } 【2.31】编写C++风格的程序,用二分法求解f(x)=0的根。 【解】实现本题功能的程序如下: #include<iostream> #include <cmath> using namespace std; inline float f(float x) { return 2*x*x*x-4*x*x+3*x-6; } int main() { float left,right,middle,ym,yl,yr; cout<<"please two number:"<<endl; //接受输入,拟定第一组数据区域 cin>>left>>right; yl=f(left); yr=f(right); do { middle=(right+left)/2; ym=f(middle); if (yr*ym>0) { right=middle; yr=ym; } else { left=middle; yl=ym; } } while (fabs(ym)>=1e-6); cout<<"\nRoot is :"<<middle; return 0; } 第3章 类和对象(一) 【3.18】写出下面程序的运营结果。 #include<iostream> using namespace std; class test { public: test() ; ~test(){ }; private: int i; }; test::test() { i = 25; for (int ctr=0; ctr<10; ctr++) { cout<<"Counting at "<<ctr<<"\n"; } } test anObject; int main() { return 0; } 【3.19】写出下面程序的运营结果。 #include<iostream> using namespace std; class Test{ private: int val; public: Test() { cout<<"default."<<endl; } Test(int n) { val=n; cout<<"Con."<<endl; } Test(const Test& t) { val=t.val; cout<<"Copy con."<<endl; } }; int main() { Test t1(6); Test t2=t1; Test t3; t3=t1; return 0; } 【3.20】指出下列程序中的错误,并说明为什么。 #include<iostream> using namespace std; class Student{ public: void printStu(); private: char name[10]; int age; float aver; }; int main() { Student p1,p2,p3; p1.age =30; … return 0; } 【3.21】指出下列程序中的错误,并说明为什么。 #include<iostream> using namespace std; class Student{ int sno; int age; void printStu(); void setSno(int d); }; void printStu() { cout<<"\nSno is "<<sno<<", "; cout<<"age is "<<age<<"."<<endl; } void setSno(int s) { sno=s; } void setAge(int a) { age=a; } int main() { Student lin; lin.setSno(20231); lin.setAge(20); lin.printStu(); } 【3.22】指出下列程序中的错误,并说明为什么。 #include<iostream> using namespace std; class Point{ public: int x,y; private: Point() { x=1; y=2; } }; int main() { Point cpoint; cpoint.x=2; return 0; } 【3.23】下面是一个计算器类的定义,请完毕该类成员函数的实现。 class counter{ public: counter(int number); void increment(); //给原值加1 void decrement(); //给原值减1 int getvalue(); //取得计数器值 int print(); //显示计数 private: int value; }; 【解】 class counter{ public: counter(int number); void increment(); //给原值加1 void decrement(); //给原值减1 int getvalue(); //取得计数器值 int print(); //显示计数 private: int value; }; counter::counter(int number) { value=number; } void counter::increment() { value++; } void counter::decrement() { value--; } int counter::getvalue() { return value; } int counter::print() { cout<<"value is "<<value<<endl; return 0; } 【3.24】根据注释语句的提醒,实现类Date的成员函数。 #include<iostream> using namespace std; class Date { public: void printDate(); //显示日期 void setDay(int d); //设立日的值 void setMonth(int m); //设立月的值 void setYear(int y); //设立年的值 private: int day,month,year; }; int main() { Date testDay; testDay.setDay(5); testDay.setMonth(10); testDay.setYear(2023); testDay.printDate(); return 0; } 【解】 void Date::printDate() { cout<<"\nDate is "<<day<<"."; cout<<month<<"."<<year<<endl; } void Date::setDay(int d) { day=d; } void Date::setMonth(int m) { month=m; } void Date::setYear(int y) { year=y; } 【3.25】建立类cylinder,cylinder的构造函数被传递了两个double值,分别表达圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。 【解】实现本题功能的程序如下: #include<iostream> using namespace std; class cylinder{ public: cylinder(double a,double b); void vol(); private: double r,h; double volume; }; cylinder::cylinder(double a,double b) { r=a; h=b; volume=3.141592*r*r*h; } void cylinder::vol() { cout<<"volume is:"<<volume<<"\n"; } int main() { cylinder x(2.2,8.09); x.vol(); return 0; } 【3.26】构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设立第2和第3个参数时,quan的值为1000,price的值为8.98。成员函数 print没有形参,需使用this指针,显示对象数据成员的内容。假设类Stoc第1个对象的三个参数分别为:"600001", 3000和 5.67 ,第2个对象的第1个数据成员的值是"600001",第2和3数据成员的值取默认值。规定编写程序分别显示这两个对象数据成员的值。 【解】 实现本题功能的程序如下: #include<iostream> using namespace std; const int SIZE=80; class Stock{ public: Stock() { strcpy(stockcode," "); } Stock(char code[], int q=1000, double p=8.98) { strcpy(stockcode, code); quan=q; price= p; } void print(void) { cout<<this->stockcode; cout<<" "<<this->quan<<" "<<this->price<<endl; } private: char stockcode[SIZE]; int quan; double price; }; int main() { Stock st1("600001",3000,5.67); st1.print(); Stock st2("600002"); st2.print(); return 0; } 第4章 类和对象(二) 【4.12】以下程序的运营结果是( )。 #include<iostream> using namespace std; class B { public: B(){} B(int i,int j) { x=i; y=j; } void printb() { cout<<x<<","<<y<<endl; } private: int x,y; }; class A{ public: A() { } A(int I,int j); void printa(); private: B c; }; A::A(int i,int j):c(i,j) { } void A::printa() { c.printb(); } int main() { A a(7,8); a.printa(); return 0; } A) 8,9 B)7,8 C) 5,6 D)9,10 【4.13】以下程序的运营结果是( )。 #include<iostream> using namespace std; class A{ public: void set(int i,int j) { x=i; y=j; } int get_y() { return y; } private: int x,y; }; class box{ public: void set(int l,int w,int s,int p) { length=l; width=w; label.set(s,p); } int get_area() { return length*width; } private: int length,width; A label; }; int main() { box b; b.set(4,6,1,20); cout<<b.get_area()<<endl; return 0; } A) 24 B) 4 C) 20 D) 6 【4.14】以下程序的运营结果是( )。 #include<iostream> using namespace std; class Sample{ public: Sample( int i,int j) { x=i; y=j; } void disp() { cout<<"disp1"<<endl; } void disp() const { cout<<"disp2"<<endl; } private: int x,y; }; int main() { const Sample a(1,2); a.disp(); return 0; } A) disp1 B) disp2 C) disp1 disp2 D) 程序编译犯错 【4.15】以下程序的运营结果是( )。 #include<iostream> using namespace std; class R{ public: R(int r1,int r2) { R1=r1; R2=r2; } void print(); void print() const; private: int R1,R2; }; void R::print() { cout<<R1<<","<<R2<<endl; } void R::print() const { cout<<R1<<","<<R2<<endl; } int main() { R a(6,8); const R b(56,88); b.print(); return 0; } A) 6,8 B) 56,88 C) 0,0 D) 8,6 【4.16】指出下面程序中的错误,并说明因素。 #include<iostream> using namespace std; class Student{ public: Student() { ++x; cout<<"\nplease input student No."; cin>>Sno; } static int get_x() { return x; } int get_Sno() { return Sno; } private: static int x; int Sno; }; int Student::x=0; int main() { cout <<Student::get_x()<<" Student exist\n"; Student stu1; Student *pstu=new Student; cout <<Student::get_x()<<" Student exist,y="<<get_Sno()<<"\n"; cout <<Student::get_x()<<" Student exist,y="<<get_Sno()<<"\n"; return 0; } 【4.17】指出下面程序中的错误,并说明因素。 #include<iostream> using namespace std; class CTest{ public: const int y2; CTest (int i1,int i2):y1(i1),y2(i2) { y1=10; x=y1; } int readme() const; //... private: int x; const int y1; }; int CTest::readme() const { int i; i=x; x++; return x; } int main() { CTest c(2,8); int i=c.y2; c.y2=i; i=c.y1; return 0; } 【解】 #include<iostream> using namespace std; class CTest{ public: const int y2; CTest (int i1,int i2):y1(i1),y2(i2) { y1=10; // 错误, y1是用const定义的,不能修改 x=y1; } int readme() const; //... private: int x; const int y1; }; int CTest::readme() const { int i; i=x; x++; // 错误,函数定义用了const,表达该函数不能修改对象 return x; } int main() { CTest c(2,8); int i=c.y2; c.y2=i; // 错误, y2是常量,不能修改 i=c.y1; // 错误,y1私有变量,不能直接存取 return 0; } 【4.18】指出下面程序中的错误,并说明因素。 #include<iostream> using namespace std; class CTest{ public: CTest () { x=20; } void use_friend(); private: int x; friend void friend_f(CTest fri); }; void friend_f(CTest fri) { fri.x=55; } void CTest::use_friend() { CTest fri; this->friend_f(fri); ::friend_f(fri); } int main() { CTest fri,fri1; fri.friend_f(fri); friend_f(fri1); return 0; } 【解】 #include<iostream> using namespace std; class CTest{ public: CTest() { x=20; } void use_friend(); private: int x; friend void friend_f(CTest fri); }; void friend_f(CTest fri) { fri.x=55; } void CTest::use_friend() { CTest fri; this->friend_f(fri); // 错误, 友元函数不是成员函数, // 所以不能用this->调用友元函数 ::friend_f(fri); } int main() { CTest fri,fri1; fri.friend_f(fri); // 错误友元函数不是成员函数, // 所以不能用“对象.函数名”调用友元函数 friend_f(fri1); return 0; } 【4.19】指出下面程序中的错误,并说明因素。 #include<iostream> using namespace std; class CTest{ public: CTest () { x=20; } void use_this(); private: int x; }; void CTest::use_this() { CTest y,*pointer; this=&y; *this.x=10; pointer=this; pointer=&y; } int main() { CTest y; this->x=235; return 0; } 【解】 #include<iostream> using namespace std; class CTest{ public: CTest () { x=20; } void use_this(); private: int x; }; void CTest::use_this() { CTest y,*pointer; this=&y; // 错误,不能对this直接赋值。 *this.x=10; // 错误, 按优先级原句的含义是*(this.x)=10,显然不对 // 对的的写法是(*this).x=10;或 this->x=10; pointer=this; pointer=&y; } int main() { CTest y; this->x=235; // 错误,this的引用不能在外部函数中,只能在内部函数中。 Return 0; } 【4.20】写出下面程序的运营结果。 #include<iostream> using namespace std; class toy { public: toy(int q, int p) { quan = q; price = p; } int get_quan() { return quan; } int get_price() { return price; } private: int quan, price; }; int main() { toy op[3][2]={ toy(10,20),toy(30,48), toy(50,68),toy(70,80), toy(90,16),toy(11,120), }; for (int i=0;i<3;i++) { cout<<op[i][0].get_quan()<<","; cout<<op[i][0].get_price()<<"\n"; cout<<op[i][1].get_quan()<<","; cout<<op[i][1].get_price()<<"\n"; } cout<<endl; return 0; } 【4.21】写出下面程序的运营结果。 #include<iostream> using namespace std; class example { public: example(int n) { i=n; cout<<"Constructing\n "; } ~example() { cout <<"Destructing\n"; } int get_i() { return i; } private: int i; }; int sqr_it(example o) { return o.get_i()* o.get_i(); } int main() { example x(10); cout<<x.get_i()<<endl;
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服