c程序设计教程--面向对象分册(郑秋生)完整答案

上传人:自*** 文档编号:80090305 上传时间:2019-02-18 格式:DOCX 页数:23 大小:39.49KB
返回 下载 相关 举报
c程序设计教程--面向对象分册(郑秋生)完整答案_第1页
第1页 / 共23页
c程序设计教程--面向对象分册(郑秋生)完整答案_第2页
第2页 / 共23页
c程序设计教程--面向对象分册(郑秋生)完整答案_第3页
第3页 / 共23页
c程序设计教程--面向对象分册(郑秋生)完整答案_第4页
第4页 / 共23页
c程序设计教程--面向对象分册(郑秋生)完整答案_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《c程序设计教程--面向对象分册(郑秋生)完整答案》由会员分享,可在线阅读,更多相关《c程序设计教程--面向对象分册(郑秋生)完整答案(23页珍藏版)》请在金锄头文库上搜索。

1、第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( ) coutDestructor called.endl;

2、 void display( ) coutnum: numendl; coutname: nameendl; coutsex: sexendlendl; 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,doub

3、le 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);coutbox

4、1.area()endl;coutbox1.volume()endl;5.2#include using namespace std;class CPointpublic :CPoint(double a=0,double b=0)x=a;y=b;CPoint(CPoint & p)x=p.x;y=p.y;void print()cout(x,y);private :double x,y;class CLinepublic:CLine(double x1=0,double y1=0,double x2=0,double y2=0):p1(x1,y1),p2(x2,y2)CLine(CPoint

5、 x,CPoint y):p1(x),p2(y)CLine(CLine &lin)p1=lin.p1;p2=lin.p2;void DrawLine()coutLine form;p1.print();coutto;p2.print();coutendl; void Linedel() coutdelete lineendl; void move(CPoint &x,CPoint &y) coutmove lineendl; p1=x; p2=y; private :CPoint p1,p2;void main()CPoint point1(1,5),point2(5,8),point3(20

6、,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 ();5.3#include using namespace std;class CComplex public:CComplex(double, double);CComplex(CComplex &c); /复数

7、类的拷贝构造函数声明double GetReal();double GetImag();void Print();private:double real;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 rea

8、l;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); /调用复数类的拷贝构造函数co

9、ut a = ;a.Print();cout b = ;b.Print();cout c = ;c.Print();coutc=a+bendl;c=add(a,b); cout c = ; 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

10、 *name;float grad3; ; CStudent:CStudent(char *na,float a,float b,float c) name=new charstrlen(na)+1; strcpy(name,na); grad0=a; grad1=b; grad2=c;CStudent:CStudent(CStudent &s) name=new charstrlen(s.name)+1; strcpy(name,s.name); grad0=s.grad0; grad1=s.grad 1; grad2=s.grad 2;CStudent:CStudent()delete n

11、ame;void CStudent:display( ) int i; coutname:nameendl;for(i=0;i3;i+)coutgradi:gradiendl; float avg(CStudent &s) /普通函数,需要引用私有成员,声明为学生类的友元函数return (s.grad0+s.grad1 +s.grad2)/3;int main( )CStudent stud1(张三,79,98,82);/定义对象stud1.display();cout 平均成绩:avg(stud1)endl;return 0;5.5#include using namespace std;class CStringpublic :CString(); /缺省构造函数,初始化为空串 CString(char ch,int nRepeat);/用一个字符重复n次,初始化字符串CString(const char*psz); /用一个字符串初始化 CString(CString &stringser)

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号