




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
高级语言C+程序设计作业面向对象方法与C+作业一、填空题1若char *string=”test”;则如果要输出指针值,正确的语句是 。2在重载“ ”运算符时,必须使用 函数重载。3当用public继承时,基类的public成员成为派生类的 成员,基类的protected成员成为派生类的 成员。4可以赋给指针的唯一整数是 。5在重载“=”运算符时,必须使用 函数重载。6以下程序: int c=10;cout.flags(ios:hex|ios:showbase);cout XXXX;XXXX必须是一个变量名,而不能是一个任意表达式。( ) 2以下声明将不会导致编译错误。( )enum GradeType A, B, C, D, E, F; 3在C+里,派生类的构造函数先于其基类的构造函数执行。( )4C+中的所有函数都是传值调用。( )5在类time中声明如下的函数原型:void time( ); ( )6以下程序:class CA public: virtual void dis( )=0; ;class CB:public CA public:void set(int m) k=m;protected:int k; ;void main( )CB b; 是正确的。( )7函数模板能够定义一个在不同数据类型基础上完成同一任务的函数。( ) 8以下语句:char *string=”test”;delete string; 是正确的 。( )9一个有指针数据成员的类必须要包含的成员函数有:初始化构造函数,析构函数,拷贝构造函数和赋值运算符函数 。( )10抽象基类中的所有虚函数都要声明为纯虚函数。 ( )11以下语句: char *string=new char10;strcpy(string,”test”);/delete string; 是正确的 。 ( )12枚举类型变量的值可以直接进行输入、输出。 ( )三、读程题1下面程序在执行过程中,若顺序输入十个整数: 1 2 3 4 5 6 7 8 9 10,其结果是什么。#include class stack;class node int data; node *prev;public: node(int d,node *n) data=d; prev=n; friend class stack; ;class stack node *top;public: stack( ) top=0; void push(int i) node *n=new node(i,top); top=n; int pop( ) node *t=top; if(top)top=top-prev; int c=t-data;delete t; return c; return 0; ;void main( )int c; stack s;for(int i=0;ic; s.push(c); for(i=0;i10;i+) cout s.pop( ) ;coutendl; 2#include #include class Increment public: Increment(int=0,int=1); void addIncrement( ) count+=increment; void print( ) const;private: int count; const int increment; ;Increment:Increment(int c,int i):increment(i) count=c; void Increment:print( ) const coutcount=setw(8)countendl; coutincrement=incrementendl; void main( )Increment object(65,7);coutbefore:endl;object.print( );for(int j=0;j3;j+)object.addIncrement( );coutafter:j+1endl;object.print( ); 3#include class point static int count ; float xcoord , ycoord ;public: point(float x=0 ,float y=0 ) xcoord=x ;ycoord=y ;count+ ; static int getcount( ) return count ;point( ) count- ; ;int point:count=0;void main( ) coutpoint:getcount( ) ; point *p ,a(32.98,-4.71) ,b ,c; coutpoint:getcount( ) ; coutb.getcount( ) ;p=new point100;coutpoint:getcount( ) ; delete p; coutpoint:getcount( )endl; 4#include class Teststatic int count;public:Test( ) +count; Test( ) -count; static int getCount( ) return count; ;int Test:count=0;void main( )coutTest:getCount( );Test t,tab5,*p;couttTest:getCount( );p=new Test10;couttTest:getCount( );delete p;couttTest:getCount( )endl; 5#include #include template class vectorT *v; int sz;public:vector(int s=100)v=new Tsz=s; vector(const vector&vv)sz=vv.sz; v=new Tsz;vector&operator=(const vector&vv)if(&vv!=this) delete v; sz=vv.sz; v=new Tsz; for(int i=0;isz;i+)vi=vv.vi; return *this;vector( ) delete v;T &operator (int i)if(i=sz) cerrErrorn;exit(1);return vi; ;void main( ) vector v(10),u(v);for(int i=0;i10;i+) vi=3*i+1;u=v;for( i=0;i=10;i+)coutui )和插入符( )以支持I/O操作。在main函数中定义学生对象数组,对于学生对象数组从磁盘文件“student.txt” 进行输入,最后以学号n为参数在数组中查找学号为n的学生,并显示该生的全部信息。 #include #include #include class student_2_;char name20,sex;int age;float score;public: student( ) student(int nu,char * na,char se,int ag, _3_ sc) num=nu; strcpy(name,na); sex=se; age=ag; score=sc;int getn( )return num;friend istream&operator(istream& s,student& st) st.sexst.agest.score; return _4_; friend ostream&operator( _5_ s,const student& st) sst.num _6_ st.sex st.age st.scores_9_;int n,flag=1;coutEnter n:;_10_;for(int j=0;ji & flag;j+) if(sj.getn( )=n) flag=0;cout找到了:sj;if(flag=1) cout没找到。n; 2请完成以下复数类的设计,复数类对象的创建及使用。#include #include #include class complexdouble rpart,ipart;double abs( )const;double norm( )const;public: complex(double r=0,double i=0)_2_; ipart=i; complex operator-( ); friend complex operator+(const complex&,const complex&); friend complex operator-(const complex&,const complex&); friend complex operator*(const complex&,const complex&); friend complex operator/(const complex&,const complex&); friend _3_ operator(const complex&,const complex&); friend int operator=(const complex&,const complex&); friend int operator (const complex&,const complex&); friend int operator(istream& si,complex& c) sic.rpartc.ipart;return si;friend ostream& operator( ostream&so,const complex&c) so(c.rpart,c.ipart)n;_4_; ;double complex:abs( )const return sqrt(rpart*rpart +_5_);double complex:norm( )const return (rpart*rpart+ipart*ipart);complexcomplex: operator-( ) return complex(-rpart,-ipart);complex operator+(const complex&c1,const complex&c2) return complex(c1.rpart+c2.rpart),c1.ipart+c2.ipart);complex operator-(const complex&c1,const complex&c2) return complex(_6_ ,c1.ipart-c2.ipart);complex operator*(const complex&c1,const complex&c2) return complex(c1.rpart*c2.rpart-c1.ipart*c2.ipart,c1.rpart*c2.ipart+c1.ipart*c2.rpart); complex operator/(const complex&c1,const complex&c2) complex res; double d=c2.norm( ); if(d!=0)res.rpart=(c1.rpart*c2.rpart+c1.ipart*c2.ipart)/d; res.ipart=(c1.ipart*c2.rpart-c1.rpart*c2.ipart)/d; else cerr(const complex&c1,const complex&c2) return c1.abs( )c2.abs( );int operator=(const complex&c1,const complex&c2) return _8_; int operator(const complex&c1,const complex&c2) return c1.abs( )c2.abs( ); int operator=(const complex&c1,const complex&c2) return c1.abs( )c3; coutc3;c3=-c1*2+c2/c3*c1-6;coutc2) coutc1;else coutc1c2c3;coutc1c2c3; 3有一家医院的门诊记录如下所示,已知该记录存放在一个”patient.dat”文件中,请编写一个程序,帮助医生计算一下每一位病人平均血压。病人的编号 病人血压的测量次数 病人血压各次测量的结果1001 5 100 120 90 110 1001002 2 100 1201003 3 90 120 1301004 4 80 70 90 1001005 3 120 135 110 4请完成以下集合类的设计,集合类对象的创建及使用。#include const int maxcard=20;class set int elemsmaxcard,card;public: set( ) card=_1_;friendbool operator&(int,set);friendbool operator=(set,set);friend_2_ operator!=(set,set);friendset operator+(set,set);friendset operator+(set,int);friendset operator-(set,int);_3_set operator*(set,set);friendbool operator(set,set);friendbool operator=(set,set);friendostream& operator(ostream&out,const set&s) outendl; for(int i=0;is.card;i+) outs.elemsi ; return _4_; ;bool operator&(int e ,set s) for(int i=0;is.card;i+)if(s.elemsi=e)return true;return _5_;bool operator=(set s1,set s2)if(s1.card!=s2.card) return false;for(int i=0;is1.card;_6_)if(!(s1.elemsi&s2) return false;return true;bool operator!=(set s1,set s2) return !(s1=s2)?true:false;set operator+(set s,int e) set res=s; if(s.cardmaxcard) if(!(e&s)res.elemsres.card+=e; return res;set operator+(set s1,set s2) set res=s1; for(int i=0;is2.card;i+) res=res+s2.elemsi; return res;set operator-(set s,int e) set res=s; if(!(e&s)return res; for(int i=0;is.card;i+)if(s.elemsi=e)for(;is.card-1;i+) res.elemsi=res.elemsi+1; _7_; return res;set operator*(set s1,set s2) _8_;for(int i=0;is1.card;i+)for(int j=0;js2.card;j+) if(s1.elemsi=s2.elemsj) res.elemsres.card+=_9_; break; return res;bool operators2.card)return false;for(int i=0;is1.card;i+)if(!(s1.elemsi&s2)return false;return true;bool operator(set s1,set s2)return s1.cards2.card&s1=s2?true:false;void main( ) set _10_;for(int i=0;i100;i+) s1=s1+i;couts1; if(s1!=s2) coutnTruen; else coutnFalsen; s2=s1; couts2; if(s1=s2) coutnTruen; else coutnFalsen; for(i=0;i20;i+) s1=s1-i;couts1; s3=s1*s2; couts3;if(s1s2) coutnTruen; else coutnFalsen; if(s1+s2=s3)coutnTruen; else coutnFalsen; 5完成以下程序。#include #include double function(double x)return 4/(1+x*x);class inte_algo protected:double a,b,n,h,sum;public:inte_algo(double left,double right,double steps)a=_1_,b=right,n=steps,h=(b-a)/n,sum=0;virtual void integrate( )= _2_; ;class rectangle: _3_public: rectangle(double left,double ri
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年艺术表演场馆服务合作协议书
- 2025年皮肤科医院合作协议书
- 2025年汽车内外饰件合作协议书
- 2025年张家口危险品驾驶员考试题
- 2025年淮安2024驾校考试危险品考试题
- 2025年纺织仪器相关专用测试仪器合作协议书
- 企业出资特别声明(6篇)
- 技术服务支持合作协议要求
- 施工阶段管控试题及答案
- 土地流转型农业种植开发合同
- 造谣调解协议书范本
- 眩晕护理课件
- 《集成电路基础及其应用》课件
- 2025年保密观知识竞赛题库完整答案带答案详解
- 2020 年全国硕士研究生入学统一考试英语 ( 一) 试题
- 云南建筑文化课件
- DB64 2115-2024 精神障碍患者康复服务指南
- 2025森林消防考试试题及答案
- 2025年下半年山东淄博师范高等专科学校高层次人才招聘16人(第二批)易考易错模拟试题(共500题)试卷后附参考答案
- 2025届江苏省南通市三模(苏北八市)高三第三次调研测试 历史试题(含答案)
- 2024年安徽省粮油经贸有限公司招聘考试真题
评论
0/150
提交评论