收藏 分销(赏)

C程序设计教程面向对象分册郑秋生完整答案.doc

上传人:快乐****生活 文档编号:9887734 上传时间:2025-04-12 格式:DOC 页数:45 大小:96.04KB
下载 相关 举报
C程序设计教程面向对象分册郑秋生完整答案.doc_第1页
第1页 / 共45页
C程序设计教程面向对象分册郑秋生完整答案.doc_第2页
第2页 / 共45页
点击查看更多>>
资源描述
第1章 类与对象 一、 选择题 6.A 7.C 8 C 9A 10 C 二、阅读题 1.x=2,y=3 2.x=2,y=3 x!=y 3. Cstatic::va1=0 cs1.vaI=1 cs2.val=2 cs1.val=4 cs2.vaI=4 四、改错题 #include <string.h> #include <iostream.h> class person public: person(int n,char* nam,char s num=n; strcpy(name,nam; sex=s; cout<<"Constructor called."<<endl; ~person( cout<<"Destructor called."<<endl; void display( cout<<"num: "< endl ; cout<<"name: "< endl ; cout<<"sex: "< endl << endl ; private: int num; char name[10]; char sex; int main( person s1(10010,"'Wang_li",'f'; s1.display( ; person s2(10011,"Zhang_fun",'m'; s2.display( ; return 0; 五、编程题 5.1 #include <iostream> using namespace std; class CBox public : CBox(double l=0,double w=0,double h=0; double area(; double volume (; private : double lengh; double width; double high; CBox::CBox(double l,double w,double h lengh=l; width=w; high=h; double CBox::area( return 2*(lengh*width+lengh*high+width*high; double CBox::volume ( return lengh*width*high; void main( CBox box1(4,5,6; cout< endl ; cout< endl ; #include <iostream> using namespace std; class CPoint public : CPoint(double a=0,double b=0 x=a; y=b; CPoint(CPoint & p x=p.x; y=p.y; void print( cout<<"("< private : double x,y; class CLine public: CLine(double x1=0,double y1=0,double x2=0,double y2=0:p1(x1,y1,p2(x2,y2 CLine(CPoint x,CPoint y:p1(x,p2(y CLine(CLine &lin p1=lin.p1; p2=lin.p2; void DrawLine( cout<<"Line form"; p1.print(; cout<<"to"; p2.print(; cout<<endl; void Linedel( cout<<"delete line"<<endl; void move(CPoint &x,CPoint &y cout<<"move line"<<endl; p1=x; p2=y; private : CPoint p1,p2; void main( CPoint point1(1,5,point2(5,8,point3(20,30,point4(40,50; CLine line1(point1,point2; CLine line2(2,3,8,12; line1.DrawLine (; line2.DrawLine (; line2.move(point3,point4; line2.DrawLine (; line2=line1; line2.DrawLine (; line1.Linedel (; #include <iostream> using namespace std; class CComplex public: CComplex(double, double; CComplex(CComplex &c; //复数类的拷贝构造函数声明 double GetReal(; double GetImag(; void Print(; private: double real; double imag; real = r; imag = i; cout<<"调用两个参数的构造函数"<<endl; CComplex::CComplex (CComplex &c //复数类的拷贝构造函数定义 real = c.real; imag = c.imag; cout<<"调用拷贝构造函数"<<endl; double CComplex::GetReal({return real;} double CComplex::GetImag({return imag;} void CComplex::Print( // 显示复数值 cout << "(" << real << "," << imag << "" << endl; CComplex add(CComplex &x,CComplex &y //普通函数完成两个数的加法,对象作为函数参数, return CComplex(x.GetReal( +y.GetReal( ,x.GetImag (+y.GetImag (; void main(void CComplex a(3.0,4.0, b(5.6,7.9; CComplex c(a; //调用复数类的拷贝构造函数 cout << "a = "; a.Print(; cout << "b = "; b.Print(; cout << "c = "; c.Print(; cout<<"c=a+b"<<endl; c=add(a,b; cout << "c = "; c.Print (; #include <iostream> #include using namespace std; class CStudent //类声明 public: CStudent(char *,float,float,float; CStudent(CStudent &s; ~CStudent(; void display(; friend float avg(CStudent &s; private: char *name; float grad[3]; CStudent::CStudent(char *na,float a,float b,float c name=new char[strlen(na+1]; strcpy(name,na; grad[0]=a; grad[1]=b; grad[2]=c; CStudent::CStudent(CStudent &s name=new char[strlen(s.name+1]; strcpy(name,s.name; grad[0]=s.grad[0]; grad[1]=s.grad [1]; grad[2]=s.grad [2]; CStudent::~CStudent( delete []name; void CStudent::display( int i; cout<<"name:"< endl ; for(i=0;i<3;i++ cout<<"grad["<<i<<"]:"< i ]<< endl ; float avg(CStudent &s //普通函数,需要引用私有成员,声明为学生类的友元函数 return (s.grad[0]+s.grad[1] +s.grad[2]/3; int main( CStudent stud1("张三",79,98,82; //定义对象 stud1.display(; cout <<"平均成绩:"<<avg(stud1<<endl; return 0; #include <iostream> using namespace std; class CString public : CString(; //缺省构造函数,初始化为空串 CString(char ch,int nRepeat;//用一个字符重复n次,初始化字符串 CString(const char*psz; //用一个字符串初始化 CString(CString &stringser; //拷贝构造函数 ~CString(; int GetLength( const; bool IsEmpty( const; char GetAt(int nindex const; void Setat(int nindex,char ch; void Print(; int compare(CString& string; int compare(const char* pszconst; void Vacate(; private : char *s; CString::CString( s=NULL; CString::CString(CString& stringser s=new char[strlen(stringser.s+1]; if(s!=0 strcpy(s,stringser.s; CString::CString(char ch,int nRepeat s=new char[nRepeat+1]; for(int i=0;i<nRepeat;i++ s[i]=ch; s[nRepeat]='\0'; CString::CString(const char*psz s=new char[strlen(psz+1]; if(s!=0 strcpy(s,psz; int CString::GetLength( const { int i=0; while (s[i]!='\0' i++; return i; bool CString::IsEmpty( const if(s==NULL return 1; else return 0; void CString::Vacate( s[0]='\0'; cout<<"Now have vacated string."<<endl; char CString::GetAt(int nindex const if(nindex>1&&nindex<=GetLength(+1 return s[ nindex-1]; else return '0'; void CString::Setat(int nindex,char ch if(nindex>1&&nindex<=GetLength(+1 s[ nindex-1]=ch; void CString::Print( cout< endl ; int CString::compare(CString& string if(strcmp(s,string.s>0 return 1; else if(strcmp(s,string.s<0 return -1; else return 0; int CString::compare(const char* pszconst if(strcmp(s,psz>0 return 1; else if(strcmp(s,psz<0 return -1; else return 0; CString::~CString( //cout<<endl<<"析构:"< endl ; if(s!=NULL delete[]s; void main( char a[4]="y";char b[4]; CString c1("Hellow",c2(c1; cout<<"Stringc1 is:"<<" "; c1.Print(; cout<<endl; cout<<"Stringc2 is:"<<" "; c2.Print(; cout<<endl; CString c3('b',3; cout<<"Stringc3 is:"<<" "; c3.Print(; cout<<endl; cout<<"*******************以下是对串的根本操作****************"<<endl; int num=c1.GetLength(; char ch; cout<<"c1的长度是:"<<" "< endl ; ch=c1.GetAt(5; cout<<"获得字符串c1中第"<<5<<"个字符是:"<<ch<<endl; cout<<"下面是插入字符串c1中一个特殊字符'd'"<<endl; c1.Setat(5,'d'; cout<<"插入后字符串c1变为:"<<" "; c1.Print(; cout<<endl; if(c1.IsEmpty(==1 cout<<"String is empty."<<endl; else cout<<"String isn't empty."<<endl; cout<<"下面是判断字符串c1清空"<<endl; c1.Vacate(; cout<<"清空后输出字符串c1:"<<" \n"; c1.Print(; cout<<"字符串已被清空"<<endl; cout<<"请按任意键继续"<<endl; cin>>b; cout<<"****************以下是对串的赋值******************"<<endl; CString c5=c2; cout<<"String c5=c2 is:"<<" ";c5.Print(; cout<<endl; cout<<"****************以下是对串的比拟******************"<<endl; int temp=c2 pare(c3; cout<<"以下比拟c2及c3"<<endl; if(temp>0 cout<<"Stringc2>Stringc3"<<endl; else if(temp<0 cout<<"Stringc2 endl ; else cout<<"Stringc9==Stringc4"<<endl; cout<<endl; cout<<"以下比拟c2及任意字符串Goodboy!"<<endl; if(c2 pare("Goodboy!">0 cout<<"Stringc2>'Goodboy!'"<<endl; else if(c2 pare("Goodboy!"<0 cout<<"Stringc2<'Goodboy!"<<endl; else cout<<"Stringc2 =='Goodboy!'"<<endl; 第二章 继承与派生 一、 选择题 1.D 2.B 3. D 一、 阅读程序题 四、编程题 4.1 #include <iostream.h> #include<string.h> class Circle public: Circle({r=0;} Circle (double d{r=d;} double area({return(PAI*r*r;} void display1({cout<<"桌子的面积:"< endl ;} private: double r; class Table public: Table({heig=0;} Table (double h {heig=h;} void display2({cout<<"桌子的高度:"<<heig<<endl;} private: double heig; class Roundtable : public Circle,public Table public: Roundtable({strcpy(color,"白色";} Roundtable(double a,double b,char* c:Circle(a,Table(b{strcpy(color,c;} void display ( { display1(; display2(; cout<<"桌子的颜色:"< endl ; } private: char color[20]; void main( Roundtable s(2.0,3.0,"黑色"; s.display(; 如果Age在基类中被定义为私有的,SetAge(int n不能直接给Age赋值,如果Age是基类的公有成员变量,那么可以直接赋值。 class Animal public: Animal({}; int Age; class Dog:public Animal public: Dog({}; Void SetAge(int n Age=n; #include <iostream.h> class Rectangle public: Rectangle (double l,double w{length=l,width=w} double area({return length*width;} 霸州市第二届中小学‘试题库’优秀试题征集评比活动一年级〔上册〕语文学科试题 上报单位:王庄子乡靳家堡村学校 设计人姓名:刘婧 题号 一 二 三 四 五 六 七 八 九 十 十一 得分 得分 评卷人 一、 按顺序填空〔10分〕 a o u b p m n l g h yi ye yue 得分 评卷人 二、 填补以下不全的音节。〔8分〕 〔 z jiàn 〕 (dà ù (zhú ái ( l àn 再 见 大 树 竹 排 两 岸 〔d qíu 〕 ( lǜ s ( diàn sh ( w ba 打 球 绿 色 电 视 尾 巴 得分 评卷人 三、 把以下拼读的局部补充完整。〔5分〕 g — ( —〔 〕— guo b — 〔 〕— ba l — 〔 〕— liú q — 〔 〕— qu 得分 评卷人 四、 看拼音写词语。〔16分〕 shā fā shēn tǐ máo jīn chǐ zi xiǎo níu fēi jī zhǎng chū diàn chē 得分 评卷人 五、写反义词。〔12分〕 近—— 早—— 大—— 黑—— 有—— 出—— 得分 评卷人 六、 连线。〔8分〕 z zh c ch 子 足 真 座 纸 春 唱 草 穿 得分 评卷人 七、照样子,填写适宜的量词。〔5分〕 一〔 朵 〕花 一〔 〕小河 一〔 〕小鸭 一〔 〕雨伞 一〔 〕红旗 两〔 〕兔子 得分 评卷人 八、写出以下字的笔顺。〔6分〕 左: 今: 火: 得分 评卷人 九、按古诗填空。〔10分〕 〔 〕看 〔 〕 有 ( , 近 听 〔 〕 〔 〕声。 春 去 〔 〕 〔 〕在, 〔 〕〔 〕〔 〕不 惊。 得分 评卷人 十、按课文填空,答复以下问题。〔7分〕 谁的尾巴长? 谁的尾巴短? 谁的尾巴好似一把伞? 猴子的尾巴〔 〕, 兔子的尾巴〔 〕, 松鼠的尾巴好似〔 〕。 1. 把课文内容补充完整。 2. 短文告诉了我们 种动物的尾巴的特点。 3. 你喜欢谁的尾巴?为什么? 得分 评卷人 十一、写话。〔13分〕 小朋友,你喜欢吃水果吗?你最喜欢吃什么水果?请你说出它的颜色,外形特点,为什么喜欢吃?不少于20字,不会写的字可以用拼音代替。 答案 一、按顺序填空〔10分〕 a o e i u ü b p m f d t n l g k h yi wu yu ye yue yuan (每空1分 二、填补以下不全的音节。〔8分〕 〔 zài jiàn 〕 (dà shù (zhú pái ( liǎng àn 再 见 大 树 竹 排 两 岸 〔dǎ qíu 〕 ( lǜ sè ( diàn shì ( wěi ba 打 球 绿 色 电 视 尾 巴<<"长:"< endl每空; cout<<" 三、;} class Cuboid:public Rectangle〔 o 〕— guo b — 〔 a 〕,double w,double h:Rectangle(L,w{high=h,volume=L*w*high }; double vol({return area(*high;} void show (; private: double high; 看拼音写词语。〔 16 分〕〔每字 分〕 display1(; cout<<"高:"< endl ; cout<<"体积:"<<vol(<<endl; void main( Cuboid cub (10,20,30; cub.show(; #include <iostream.h> class Vehicle public: Vehicle(:maxspeed(0,weight(0{} Vehicle( 出—— void run ( { cout<<" 可以奔跑"<<endl; } void stop ( { cout<<" 可以停顿奔跑"<<endl;} private: double maxspeed; double weight; class Bicycle:virtual public Vehicle public: Bicycle (double m,double w,double h:Vehicle(m,w,height(h{} private: double height; class Motorcar : virtual public Vehicle public: Motorcar (double m,double w,double s:Vehicle(m,w,setnum(s{} private: double setnum; class Motorcycle:public Bicycle,public Motorcar public: Motorcycle(double m,double w,double h,double s:Bicycle(m,w,h,Motorcar(m,w,s, Vehicle(m,w{} void main( Motorcycle s1(300,10,6,30; s1.run(; s1.stop(; 如果不把Vehicle设置为虚基类 近 听 第3水 〕 〔无 3.1 简答题 春 去 .〕::〔还 〕在, 3.1.2 运算符重载后,优先级与结合性如何?〔 人 〕〔 来 〕〔 鸟 在每一种编译系统中,运算符实际上都对应一个函数,只是这种运算对用户具有透明性,使用者并不知道函数的存在。运算符重载实际上是运算符函数的重载,所以运算符的重载实际上是函数的重载。 编译程序对运算符重载的选择,遵循着函数重载的选择原那么。当遇到不很明显的运算符时,编译程序将去寻找参数相匹配的运算符函数。 十、按课文填空,答复以下问题。〔7 〔1〕 不可臆造新的运算符 〔2〕 坚持四个不能改变。 兔子的尾巴〔 短 〕, 松鼠的尾巴好似〔 一把伞 运算符的返回类型 〔3〕C++规定,运算符中,参数类型都是内部类型时,不允许重载。 “::、3种 〔3.1.5 运算符重载必须遵循哪些原那么? 3. (答案略〔2 〔2〕十一、写话。〔13分〕 说出它的颜色,外形特点,为什么喜欢吃,语句通顺,不少于20 3.2.1 C++中多态性包括两种多态性: 〔1〕 与 〔2〕 。前者是通过 〔3〕 实现的,而后者是通过 〔4〕 与 〔5〕 来实现的。 答案:〔1〕编译时的 〔2〕运行时的 〔3〕函数与运算符的重载 〔4〕类继承关系 〔5〕虚函数 3.2.2 纯虚函数定义时在函数参数表后加 〔1〕 ,它说明程序员对函数 〔2〕 ,其本质是将指向函数体的指针定为 〔3〕 。 答案:〔1〕=0 〔2〕不定义 〔3〕NULL 3.2.3在基类中将一个成员函数说明成虚函数后,在其派生类中只要 〔1〕 、 〔2〕 与 〔3〕 完全一样就认为是虚函数,而不必再加关键字 〔4〕 。如有任何不同,那么认为是 〔5〕 而不是虚函数。除了非成员函数不能作为虚函数外, 〔6〕 、 〔7〕 与 〔8〕 也不能作为虚函数。 答案:〔1〕同虚函数名 〔2〕同参数表 〔3〕同返回类型。如基类中返回基类指针,而派生类中返回派生类指针是允许的 〔4〕virtual 〔5〕重载 〔6〕静态成员函数 〔7〕内联函数 〔8〕构造函数 第三大题答案: 3.1答案: #include<iostream.h> #include<string.h> class CStudent char name[20]; char nativeplace[20]; char code[30]; int age; float score; public : CStudent(char *, char*,char*,int ,float; CStudent(CStudent &s; void display(; float operator+(CStudent s1; CStudent::CStudent(char *name, char*native,char*code,int age,float score strcpy(this->name,name; strcpy(this->nativeplace,native; strcpy(this->code,code; this->age=age; this->score=score; CStudent::CStudent(CStudent &s strcpy(this->name,s.name; strcpy(this->nativeplace,s.nativeplace; strcpy(this->code,s.code; this->age=s.age; this->score=s.score; void CStudent::display( cout< nativeplace <<" "< endl ; float CStudent::operator +(CStudent s1 return this->score+s1.score; void main( CStudent w("whl","zhengzhou","",30,90; w.display(; CStudent c("ctm","zhengzhou","",30,90; c.display(; cout<<w+c<<endl; 3.2答案: #include<iostream.h> #include<string.h> #include<math.h> class CTriangle float a,b,c; public: CTriangle(float a,float b,float c this->a=a; this->b=b; this->c=c; float GetArea( float t=(a+b+c/2; return sqrt(t*(t-a*(t-b*(t-c; float operator +(CTriangle t return this->GetArea(+t.GetArea(; void main( CTriangle tr1(3,4,5,tr2(6,8,10; cout< endl ; 3.3答案: #include<iostream.h> class BaseClass public: virtual void f1( {cout<<"BaseClass::f1("<<endl;} void f2( {cout<<"BaseClass::f2("<<endl;} class DerivedClass: public BaseClass public: void f1( {cout<<"DerivedClass::f1("<<endl;} void f2( {cout<<"DerivedClass::f2("<<endl;} void main( DerivedClass d1; BaseClass* Bptr; DerivedClass* Dptr; Bptr=&d1; Bptr->f1(; Bptr->f2(; Dptr=&d1; Dptr->f1(; Dptr->f2(; 习题四 一、选择题 1.D 2.A 3.B 4.C 5. C 6.C 7. A 二、简答题 1.什么叫做流?流的提取与插入是指什么?I/O流在C++中起着怎样的作用? 答:流是一种抽象,它负责在数据的生产者〔程序/文件〕与数据的消费者〔文件/程序〕之间建立联系,并管理数据的流动。 一般意义下的读操作在流数据抽象中被称为〔从流中〕提取,写操作被称为〔向流中〕插入。 完成数据从动态〔内存〕到静态〔外存〕的转换,或着是从静态〔外存〕到动态〔内存〕的转换。 2.什么是字节流、字符流与二进制流? 答:根据对字节内容的解释方式,字节流分为字符流〔也称文本流〕与二进制流。 字符流将字节流的每个字节按ASCⅡ字符解释,它在数据传输时需作转换,效率较低。例如源程序文件与文本文件都是字符流。由于ASCⅡ字符是标准的,所以字符流可以直接编辑,显示或打印,字符流产生的文件通行于各类计算机。 二进制流将字节流的每个字节以二进制方式解释,它在数据传输时不作任何转换,故效率高。但各类计算机对数据的二进制存放格式各有差异,且无法人工阅读,故二进制流产生的文件可移植性较差。 3.cerr与clog作用是什么?有何区别? 答:对于输入提示信息或输出结果而言, cerr与clog的用法一样,但作用不同。cerr的作用是向标准错误设备(standard error device输出有关出错信息。cerr流中的信息只能在显示器输出。当调试程序时,往往不希望程序运行时的出错信息被送到其他文件,而要求在显示器上及时输出,这时应该用cerr,cerr流中的信息是用户根据需要指定的。clog流对象也是标准错误流,它是console log的缩写。它的作用与cerr一样,都是在终端显示器上显示出错信息。不同的是cerr不经过缓冲区,直接向显示器上输出有关信息,而clog中的信息存放在缓冲区中,缓冲区满后或遇endl时向显示器输出。 4.用什么方法来控制输入输出流中出现的错误? 答:为提高程序的可靠性,应在程序中检测I/O流的操作是否正常。当检测到流操作出现错误时,可以通过异常处理来解决问题。 5.比拟读写文本文件及二进制文件的异同。 答:从文件编码的方式来看,文件可分为ASCII码文件与二进制码文件两种。   ASCII文件也称为文本文件,这种文件在磁盘中存放时每个字符对应一个字节,用于存放对应的ASCII码。例如,数5678的存储形式为: ASC码:  00110101 00110110 00110111 00111000 十进制码: 5     6    7    8 共占用4个字节。ASCII码文件可在屏幕上按字符显示, 例如源程序文件就是ASCII文件,用DOS命令TYPE可显示文件的内容。 由于是按字符显示,因此能读懂文件内容。   二进制文件是按二进制的编码方式来存放文件的。 例如, 数5678的存储形式为: 00010110 00101110只占二个字节。二进制文件虽然也可在屏幕上显示, 但其内容无法读懂。C++系统在处理这些文件时,并不区分类型,都看成是字符流,按字节进展处理。 输入输出字符流的开场与完毕只由程序控制而不受物理符号(如回车符的控制。 因此也把这种文件称作“流式文件〞。 6.随机读写是什么意思,常用于哪种类型的文件? 答:在C++中可以由程序移动文件指针,从而实现文件的随机访问,即可读写流中任意一段内容。一般文本文件很难准确定位,所以随机访问多用于二进制文件。 7.文件流与字符串流有什么区别? 答:文件在C++看来是字符流或二进制流,文件流是以外存文件为输入输出对象的数据,所以文件流是及设备相关的。可以把流的概念应用到字符串〔String〕上。这样字符串就可以看作字符串流。字符串流不是以外存文件为输入输出的对象,而以内存中用户定义的字符数组为输入输出的对象。字符串流是及内存相关,所以也称内存流。可以用输入输出操作来完成字符串流的操作。 三、编程题 1. x=+468******y=-3.43***** 2. 生成一个名称为data.txt文件,内容:This is test data 3.源程序。 #include <iostream> #include <iomanip> using namespace std; int main( for(int n=8;n>0;n-- cout<<setw(20-n<<"*"<<setfill('*'<<setw(2*n-1<<" "<<setfill(' '<<setw(20-n<<endl; return 0; 4.源程序。 #include<fstream.h> void main(void char f2[256]; cout<<"请输入目标文件名?"; cin>>f2; ofstream out(f2; if(!out{ cout<<"\n不能翻开目标文件:"< int i=0; while(i<21 out<<i; i=i+1; out.close(; cout<<"\n操作完毕!\
展开阅读全文

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


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

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

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

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

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

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

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

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

客服