已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 第第1章章 1.2 【解答】 重点:标准输入输出库函数标准输入输出流对象 #include int main() char name20; coutname; cout using namespace std; /Overload max( ) three ways 重载函数名 max 三次 int max(int a,int b); long max(long a,long b); double max(double a,double b); int main() int a1=3,b1=10; long a2=123456,b2=567893; double a3=2*106,b3=-12.34; coutb?c=a:c=b; return(c); long max(long a,long b) long c; ab?c=a:c=b; return(c); double max(double a,double b) double c; ab?c=a:c=b; return(c); 4 1.9 要点:申请动态数组 【解答】 / A simple example of new and delete. #include #include using namespace std; const int N=10; int main( ) char *p,q; int i=0; p=new charN; /allocate memory for a array 为数组分配动态内存空间 if(p=NULL) coutq; while(q!=#) pi+=q; cinq; cout void f(int a ,int n, int for(int i=1;iai) min=ai; void main( ) int a10=2,5,3,9,0,8,1,7,6,4; int max,min; f(a,10,max,min); cout #include class my_string private: char *contents; int length; public: my_string(char *s); void show(); int getlength(); void add(char *s); ; my_string:my_string(char *s) contents=new charstrlen(s)+1; strcpy(contents,s); length=strlen(s); 8 void my_string:show() cout class Rectangle private: int length; int width; public: Rectangle(int l,int w); 9 int area(); ; Rectangle:Rectangle(int l,int w) length=l; width=w; int Rectangle:area() return (length*width); void main() Rectangle Rect(10,15); cout)运算符。 【解答】 #include #include class Student 11 private: int number; char name15; float score; public: Student(int number1,char *name1,float score1); /构造函数的声明 void modify(float score1) score=score1; void print( ); ; /构造函数的定义 Student:Student (int number1,char *name1,float score1) number=number1; strcpy(name,name1); score=score1; void Student:print( ) coutprint(); 12 return 0; 4.3 阅读下列程序,指出其中的错误。并分析出错的原因。 #include using namespace std; class myclass int a, b; public: void set(int i, int j) a = i; b = j; void show() cout class Circle public: Circle(float r) radius=r; +count; 14 Circle() -count; static int num() return count; private: float radius; static int count; ; int Circle:count=0; void main() Circle c1(10),c2(20); cout using namespace std; class Square int side; public: Square(int x) side = x; coutdisplay(); delete ps; /自动调用析构函数,然后释放堆内存 return 0; 5.3 假设有以下的类定义, class Square int side; public: Square(int x=10) side = x; cout using namespace std; class X public: X() p = new int2; cout #include #include using namespace std; class strtype char *p; public: strtype(char *s); strtype() delete p; char *get() return p; ; strtype:strtype(char *s) int l; l = strlen(s)+1; p = new char l; if(!p) cout #include #include using namespace std; class strtype char *p; public: strtype(char *s); strtype(const strtype strtype() delete p; char *get() return p; ; strtype:strtype(char *s) int l; l = strlen(s)+1; p = new char l; if(!p) cout class A protected: 22 int m,n; public: void set(int a, int b) m=a; n=b; void show( ) cout 23 class planet protected: double distance; / miles from the sun int revolve; / in days public: planet(double d,int r) distance=d; revolve=r; ; class earth: public planet double circumference; / circumference of orbit public: earth(double d,int r):planet(d,r) circumference=2*r*3.1416; void show() cout运算符。 ? 在重载增值和减值运算符时, 用 friend operator 函数需要使用引用参 数。 C+的大部分运算符即可以说明为成员函数运算符,也可以说明成友元函数 运算符。究竟选择哪一种运算符函数好一些,没有定论,这主要取决于实际 情况和程序员的习惯。 第第7章章 7.3 完成下列的 String 类,并在主函数 main( )中测试它。 class String public: String(const char *str = NULL); / constructor String(const String / copy constructor String(void); / destructor String String / 重载=运算符 int operator=(String / 重载=运算符 int operator=(char *str); private: char *m_data; / used for storing the string int length; ; 【解答】 25 #include #include class String public: String(char *str); / constructor String(const String / copy constructor String() / destructor String String / 重载=运算符 int operator=(String / 重载=运算符 int operator=(char *str); private: char *m_data; / used for storing the string int length; ; String:String(char *str) length=strlen(str); m_data=new charlength+1; strcpy(m_data,str); String:String(const String strcpy(m_data,other.m_data); length=other.length; String String int String:operator=(String else return 0; int String:operator=(char *str) if(strcmp(m_data,str)=0) return 1; else return 0; void main() String s1(“Hello“),s2(“hello“); String s3(s1); if(s1=s2) cout class three_d int x,y,z; public: three_d(int i, int j ,int k) x = i ,y =j ,z = k; three_d( ) x = 0 ,y =0 ,z = 0; void get(int three_d three_d operator + (three_d); three_d 30 three_d void print() cout“x:“x“y:“y“z“zendl; ; three_d y=t.y; z=t.z; return *this; three_d three_d:opera
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小麦病害:防治技术新进展
- 面包租赁合同
- 大学毕业生三方协议3篇
- 2024年事业单位招聘考试辽宁省鞍山市职业能力倾向测验题库含答案解析
- 2023年化妆品销售半期工作总结
- 2023年化学教师学年工作总结
- 叔丁基苯酚行业相关投资计划提议
- 余热锅炉相关行业投资规划报告范本
- 如何进行促销的策划与实施
- 幼儿园课件之大班语言《三个和尚》
- 专业解读-高血压病中医高血压指南
- 期货基础知识考试试题及答案解析(一)
- 员工绩效考核管理
- 礼宾枪端枪收枪教学教案
- 国内防火门窗行业市场发展现状及竞争格局与投资发展研究报告
- 宗教与法律知识讲座
- 人工智能家居
- 美国文学选读第3版翻译-陶洁
- 婚介登记表(实用).xls
- Micromine数字矿山软件系统的使用规范和流程.doc
- 中国数字地震观测网络技术规程.doc
评论
0/150
提交评论