1、C程序设计教程面向对象分册郑秋生完整复习资料第1章 类和对象一、 选择题1.C 2.B 3.C 4.A 5.C6.A 7.C 8 C 9A 10 C二、阅读题1x=2,y=32x=2,y=3x!=y3Cstatic:va1=0cs1.vaI=1cs2.val=2cs1.val=4cs2.vaI=4四、改错题#include #include class person public:person(int n,char* nam,char s num=n;strcpy(name,nam; sex=s;coutConstructor called.endl; person( coutDestruct
2、or called.endl; void display( coutnum: endl ; coutname: endl ; coutsex: endl endl ; private:int num;char name10;char sex;int main( person s1(10010,Wang_li,f; s1.display( ; person s2(10011,Zhang_fun,m; s2.display( ; return 0;五、编程题51#include using namespace std;class CBoxpublic :CBox(double l=0,double
3、 w=0,double h=0;double area(;double volume (;private :double lengh;double width;double high;CBox:CBox(double l,double w,double hlengh=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 e
4、ndl ; 5.2#include using namespace std;class CPointpublic :CPoint(double a=0,double b=0x=a;y=b;CPoint(CPoint & px=p.x;y=p.y;void print(cout( private :double x,y;class CLinepublic:CLine(double x1=0,double y1=0,double x2=0,double y2=0:p1(x1,y1,p2(x2,y2CLine(CPoint x,CPoint y:p1(x,p2(yCLine(CLine &linp1
5、=lin.p1;p2=lin.p2;void DrawLine(coutLine form;p1.print(;coutto;p2.print(;coutendl;void Linedel(coutdelete lineendl;void move(CPoint &x,CPoint &ycoutmove lineendl;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
6、,8,12;line1.DrawLine (;line2.DrawLine (;line2.move(point3,point4;line2.DrawLine (;line2=line1;line2.DrawLine (;line1.Linedel (;5.3#include using namespace std;class CComplex public:CComplex(double, double;CComplex(CComplex &c; /复数类的拷贝构造函数声明double GetReal(;double GetImag(;void Print(;private:double r
7、eal;double imag; CComplex:CComplex (double r=0.0, double i=0.0 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 ( rea
8、l , imag endl;CComplex add(CComplex &x,CComplex &y /普通函数完成两个数的加法,对象作为函数参数,return CComplex(x.GetReal( +y.GetReal( ,x.GetImag (+y.GetImag (;void main(voidCComplex 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(;coutc=a+bendl;c=add(a,b; cout c =
9、; c.Print (;5.4#include #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 grad3; ;CStudent:CStudent(char *na,float a,float b,float cname=new charstrlen(na+1;str
10、cpy(name,na;grad0=a;grad1=b;grad2=c;CStudent:CStudent(CStudent &sname=new charstrlen(s.name+1;strcpy(name,s.name;grad0=s.grad0;grad1=s.grad 1;grad2=s.grad 2;CStudent:CStudent(delete name;void CStudent:display( int i; coutname: endl ; for(i=0;i3;i+coutgradi: i endl ; float avg(CStudent &s /普通函数,需要引用私
11、有成员,声明为学生类的友元函数return (s.grad0+s.grad1 +s.grad2/3;int main( CStudent stud1(张三,79,98,82; /定义对象stud1.display(;cout 平均成绩:avg(stud1endl;return 0;5.5#include using namespace std;class CStringpublic :CString(; /缺省构造函数,初始化为空串 CString(char ch,int nRepeat;/用一个字符重复n次,初始化字符串CString(const char*psz; /用一个字符串初始化CS
12、tring(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& strings
13、er s=new charstrlen(stringser.s+1;if(s!=0strcpy(s,stringser.s;CString:CString(char ch,int nRepeats=new charnRepeat+1;for(int i=0;inRepeat;i+si=ch;snRepeat=0;CString:CString(const char*pszs=new charstrlen(psz+1;if(s!=0strcpy(s,psz;int CString:GetLength( const int i=0;while (si!=0i+;return i;bool CStr
14、ing:IsEmpty( constif(s=NULLreturn 1;elsereturn 0;void CString:Vacate(s0=0;coutNow have vacated string.1&nindex1&nindex=GetLength(+1s nindex-1=ch; void CString:Print(cout0return 1;else if(strcmp(s,string.s0return 1;else if(strcmp(s,psz0return -1;else return 0;CString:CString(/coutendl析构: endl ; if(s!
15、=NULLdeletes;void main( char a4=y;char b4;CString c1(Hellow,c2(c1;coutStringc1 is: ; c1.Print(;coutendl;coutStringc2 is: ; c2.Print(;coutendl;CString c3(b,3;coutStringc3 is: ; c3.Print(;coutendl;cout*以下是对串的基本操作*endl;int num=c1.GetLength(;char ch;coutc1的长度是: endl ; ch=c1.GetAt(5;cout获得字符串c1中第5个字符是:ch
16、endl;cout下面是插入字符串c1中一个特殊字符dendl;c1.Setat(5,d;cout插入后字符串c1变为: ;c1.Print(;/coutendl;if(c1.IsEmpty(=1coutString is empty.endl;elsecoutString isnt empty.endl;cout下面是判断字符串c1清空endl;c1.Vacate(;cout清空后输出字符串c1: n;c1.Print(;cout字符串已被清空endl;cout请按任意键继续b;/cout*以下是对串的赋值*endl;CString c5=c2;coutString c5=c2 is: ;c
17、5.Print(;coutendl;cout*以下是对串的比较*endl;int temp=pare(c3;cout以下比较c2及c30coutStringc3endl;else if(temp0coutStringc2 endl ; elsecoutStringc9=Stringc4endl;coutendl;cout以下比较c2及任意字符串Goodboy!0coutGoodboy!endl;else if(pare(Goodboy!0coutStringc2Goodboy!endl;elsecoutStringc2 =Goodboy!endl;第二章 继承和派生一、 选择题1D 2.B 3
18、. D一、 阅读程序题四、编程题4.1 #include #include#define PAI 3.14class Circle public:Circle(r=0;Circle (double dr=d;double area(return(PAI*r*r;void display1(cout桌子的面积: endl ; private:double r;class Tablepublic:Table(heig=0;Table (double h heig=h;void display2(cout桌子的高度:heigendl;private:double heig;class Roundta
19、ble : public Circle,public Tablepublic:Roundtable(strcpy(color,白色;Roundtable(double a,double b,char* c:Circle(a,Table(bstrcpy(color,c;void display ( display1(;display2(;cout桌子的颜色: endl ; private:char color20;void main(Roundtable s(2.0,3.0,黑色;s.display(;4.2如果Age在基类中被定义为私有的,SetAge(int n不能直接给Age赋值,如果Ag
20、e是基类的公有成员变量,则可以直接赋值。class Animalpublic:Animal(;int Age;class Dog:public Animalpublic:Dog(;Void SetAge(int nAge=n;4.3#include class Rectanglepublic:Rectangle (double l,double wlength=l,width=wdouble area(return length*width;霸州市第二届中小学试题库优秀试题征集评比活动一年级(上册)语文学科试题上报单位:王庄子乡靳家堡村学校设计人姓名:刘婧题号一二三四五六七八九十十一得分得分评
21、卷人一、 按顺序填空(10分)a o u b p m n l g hyi ye yue 得分评卷人二、 填补下列不全的音节。(8分)( z jin ) (d (zh i ( l n 再 见 大 树 竹 排 两 岸(d qu ) ( l s ( din sh ( w ba打 球 绿 色 电 视 尾 巴得分评卷人三、 把下列拼读的部分补充完整。(5分)g ( ( ) guo b ( ) bal ( ) li q ( ) qu 得分评卷人四、 看拼音写词语。(16分)sh f shn t mo jn ch zi( ( ( ( xio nu fi j zhng ch din ch( ( ( ( 得分评
22、卷人五、写反义词。(12分)近 早 大黑 有 出得分评卷人六、 连线。(8分)z zh c ch子 足 真 座 纸 春 唱 草 穿得分评卷人七、照样子,填写合适的量词。(5分)一( 朵 )花 一( )小河 一( )小鸭一( )雨伞 一( )红旗 两( )兔子 得分评卷人八、写出下列字的笔顺。(6分)左: 今: 火: 得分评卷人九、按古诗填空。(10分)( )看 ( ) 有 ( ,近 听 ( ) ( )声。春 去 ( ) ( )在,( )( )( )不 惊。得分评卷人十、按课文填空,回答问题。(7分)谁的尾巴长?谁的尾巴短?谁的尾巴好像一把伞?猴子的尾巴( ),兔子的尾巴( ),松鼠的尾巴好像(
23、 )。1. 把课文内容补充完整。2. 短文告诉了我们 种动物的尾巴的特点。3. 你喜欢谁的尾巴?为什么?得分评卷人十一、写话。(13分)小朋友,你喜欢吃水果吗?你最喜欢吃什么水果?请你说出它的颜色,外形特点,为什么喜欢吃?不少于20字,不会写的字可以用拼音代替。答案一、按顺序填空(10分)a o e i u b p m f d t n l g k hyi wu yu ye yue yuan (每空1分二、填补下列不全的音节。(8分)( zi jin ) (d sh (zh pi ( ling n 再 见 大 树 竹 排 两 岸(d qu ) ( l s ( din sh ( wi ba打 球 绿 色 电 视 尾 巴长: endl每空; cout三、;class Cuboid:public Rectangle( o ) guo b ( a ),double w,double h:Rectang