




已阅读5页,还剩27页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一题 #include class Stack /定义堆栈类 struct Node int content; Node *next; *top;public: Stack() top = NULL; / 构造函数的定义 bool push(int i); / 压栈成员函数的声明 bool pop(int& i); / 弹栈成员函数的声明;bool Stack:push(int i) / 压栈成员函数的定义 Node *p=new Node; if (p = NULL) cout content = i; p-next = top; top = p; return true;bool Stack:pop(int& i) / 弹栈成员函数的定义if (top = NULL) cout next; i = p-content; delete p; return true;void main()Stack st1,st2; / 定义对象st1和st2 int x;for(int i=1;i=5;i+)st1.push(i); / 压栈成员函数的调用st2.push(i); / 压栈成员函数的调用 coutstack1:endl;for(i=1;i=3;i+)st1.pop(x); / 弹栈成员函数的调用coutxendl;st1.push(20);for(i=1;i=4;i+)if(st1.pop(x)coutxendl;elsebreak;coutstack2:endl;while(st2.pop(x)coutxendl;第二题#include / 使用名空间std,则必须去掉.h扩展名using namespace std; void main() char name10; int age; coutname; coutage; coutname is nameendl; coutage is ageendl;第三题#includeusing namespace std;void main() struct studentint no;float math;int n;cinn;student wang; / C+中变量的定义语句可以出现在程序的任意位置;可以使用结构体名定义变量wang.no=n;cinwang.math;coutwang.no wang.mathendl;第四题#include using namespace std;void main()int *p;p=new int; / 分配内存空间*p=5;cout*p;delete p; / 释放内存空间第五题#include using namespace std;void main() int n; / 定义数组元素的个数 int *p; coutn; if(p=new intn)=0) cout cant allocate more memory,terminating. endl;exit(1); / 分配内存空间 for( int i=0;in;i+) pi= i *2; coutNow output the array : endl; for( i=0;in;i+) coutpi ; coutendl; delete p; / 释放内存空间第六题#include void main()int num=50;int &ref=num;ref+=10;coutnum=numendl;cout ref=refendl;num+=40;cout num=numendl;coutref=refendl;第七题#include void main()int num;int & ref=num;num=5; coutnum=numendl;coutref=refendl;cout&num=&numendl;cout &ref=&refendl;第八题#include using namespace std;void swap(int &x,int &y);void main() int x=5, y=6; cout before swap, x: x ,y: y endl; swap(x,y); coutafter swap,x:x ,y:y;void swap(int &rx,int &ry) / 引用作为形参 int temp=rx; rx=ry; ry=temp;第九题#include using namespace std;int array64=60,80,90,75, 75,85,65,77, 80,88,90,98, 89,100,78,81, 62,68,69,75, 85,85,77,91;int& level(int grade, int size, int& tA, int& tB);void main() int typeA=0,typeB=0; int student=6; int gradesize=4; for(int i=0; istudent; i+) / 对所有的学生数据进行处理 level(arrayi, gradesize, typeA, typeB)+; / 函数调用作为左值 cout number of type A is typeA endl; cout number of type B is typeB endl;int& level(int grade, int size,int& tA, int& tB) int sum=0; for(int i=0; i=80) return tA; / A类学生 else return tB; / B类学生第十题#include using namespace std;float& fn2(float r) float temp; temp=r*r*3.14; return temp; / 返回了局部变量void main() fn2(5.0)=12.4; / 返回的是局部作用域内的变量,函数调用作为左值使用。此种情况应尽量避免。第十题#include #include / 使用字符串string类型的程序应包含头文件using namespace std;void main()string s,t;cout请输入一个字符串:s; / 由键盘输入一行文本,并把它赋给sring类型的变量s,注意: / 使用此方式输入的字符串中不能包含空白字符,具体请参见7.1.2小节t=I like programming!;cout字符串的输出:endlsendltendl;couts.append( OK!)endl; / append为string类的成员函数第十一题#include using namespace std;inline double circumference(double radius);/ 内联函数的声明,如果此处省略inline关键字,即使在函数定义时加上inline关键字,/ 编译程序也不认为那是内联函数,对待该函数如普通函数那样,/ 产生该函数的调用代码并进行连接。void main()double r=3.0,s;s=circumference(r);coutthe circumference is sendl;inline double circumference(double radius) / 内联函数的定义,此处也可以省略inline关键字 return 2*3.1415926*radius;第十二题#include int add(int x,int y)int sum;sum=x+y;return sum;int add(int x,int y,int z)int sum;sum=x+y+z;return sum;void main( )int a,b;a=add(5,10);b=add(5,10,20);couta=aendl;coutb=bendl;第十三题#includetemplateT min(T a ,int n) int i; T minv=a0; for(i=1;iai) minv=ai; return minv;void main() int a =1,3,0,2,7,6,4,5,2; double b =1.2,-3.4,6.8,9,8; couta数组的最小值为:min(a,9)endl; coutb数组的最小值为:min(b,4)endl;第十四题#include#define PI 3.1415926535templatedouble Circle_Square(T x) return x*x*PI;double Circle_Square(long x) return x*x*PI;void main() int r1=1; double r2=2.0; long r3=3; coutThe first circle square is Circle_Square(r1)endl The second circle square is Circle_Square(r2)endl The third circle square is Circle_Square(r3)endl;第十五题/ tdate.h 这个头文件只存放有关Tdate类的定义说明#ifndef Tdate_H / 用来避免重复定义#define Tdate_H / 不是类的一部分class Tdatepublic:void set(int,int,int); / 成员函数原型int isLeapYear();void print();private:int month;int day;int year; / Tdate类定义的结束#endif / Tdate_H/ tdate.cpp 类Tdate的实现部分#include / 因为tdate.cpp文件要访问运算符 和stream类/ 对象cout,而这二者都是定义在iostream类中的,所以包含iostream.h头文件。#includetdate.h / 包含用户自定义的头文件,该文件中提供了Tdate类的定义void Tdate:set(int m,int d,int y)month = m; day = d; year = y;int Tdate: isLeapYear()return (year % 4 = 0 & year % 100 != 0)|(year%400=0);void Tdate:print()cout month / day / year endl;第十六题/ ch3_2.cpp#include#includetdate.hvoid someFunc(Tdate& refs)refs.print(); / 通过对象的引用调用成员函数if(refs.isLeapYear() / 通过对象的引用调用成员函数cout errorn;elsecout print(); / 通过指向对象的指针调用成员函数if(*pTdate).isLeapYear() / 通过指向对象的指针调用成员函数cout errorn;elsecout rightn;someFunc(s); / 对象的地址传给引用第十七题#includeconst int SIZE = 10; / 存储的最多字符数 class Cstackprivate: char stkSIZE; int position;public: void init() position = 0; char push (char ch); char pop();char Cstack:push(char ch) if(position = SIZE) coutn栈已满 n; return 0; stkposition+ = ch; return ch;char Cstack:pop() if (position = 0) coutn栈已空endl; return 0; return stk-position;void main() Cstack s; s.init(); char ch; cout 请输入字符:ch; while(ch != #& s.push(ch) cinch; cout n现在输出栈内数据n; while(ch = s.pop() cout ch;第十八题 #include class Tdatepublic: void set(int m=5,int d=16,int y=1991) / 置日期值 month=m; day=d; year=y; void print() / 输出日期值 coutmonth/day/yearendl; private: int month; int day; int year;void main()Tdate a,b,c;a.set(4,12,1996);b.set(3);c.set(8,10);a.print();b.print();c.print();第十九题#includeclass cubepublic:int volume(int ht,int wd)return ht*wd;int volume(int ht,int wd,int dp)height=ht;width=wd;depth=dp;return height*width*depth;private:int height,width,depth;void main()cube cube1;cout cube1.volume(10,20) endl; / 调用带2个参数的成员函数cout cube1.volume(10,20,30) endl; / 调用带3个参数的成员函数二十题#includeclass queue int q100; int sloc,rloc;public: queue( ); void qput(int i ); int qget( );queue:queue( ) sloc=rloc=0; cout queue initializedn;void queue:qput(int i) if(sloc = 100) cout queue is fulln; return; sloc+; qsloc=i ;int queue:qget( ) if(rloc=sloc) cout queue is emptyn; return 0; rloc+; return qrloc;void main( ) queue a,b; a.qput(10); b.qput(20); a.qput(20); b.qput(19); couta.qget( ) ; couta.qget( ) n ; coutb.qget( ) ; coutb.qget( ) n ;二十一题#include class test private: int num; float f1; public: test(); test(int n, float f); / 参数化的构造函数 int getint()return num; float getfloat()return f1;test:test() coutInitializing defaultendl; num = 0; f1 = 0.0;test:test(int n, float f) coutInitializing n, fendl; num = n; f1 = f;void main() test x; / 调用无参的构造函数 test y(10, 21.5); / 调用带两个参数的构造函数 test *px=new test; / 调用无参的构造函数 test *py=new test(10,21.5); / 调用带两个参数的构造函数二十二题#include class Tdatepublic: Tdate(int m=5,int d=16,int y=1990) month=m; day=d; year=y; cout month / day / year endl; private: int month; int day; int year;void main() Tdate aday; Tdate bday(2); Tdate cday(3,12); Tdate dday(1,2,1998);二十三题#include#include using namespace std;class Studentpublic: Student(char* pName) coutcall one parameter constructorendl;strncpy(name,pName,sizeof(name); namesizeof(name)-1= 0; coutthe name is nameendl; Student()coutcall no parameter constructorendl; / 注意,上面的无参构造函数不能省略,因为在main()中创建无参对象noName时会调用此函/ 数,如果省略掉此条语句则编译时会显示以下的错误信息:/ error C2512: Student : no appropriate default constructor availableprotected: char name20;void main() Student noName; Student ss(Jenny); 二十四题/ test.hclass test private: int num; float f1; public: test(); test(int n, float f); / 参数化的构造函数 int getint()return num; float getfloat()return f1;/ test.cpp#include test.h#includeusing namespace std;test:test() coutInitializing defaultendl; num = 0; f1 = 0.0;test:test(int n, float f) coutInitializing n, fendl; num = n; f1 = f;/ ex3_10.cpp#include test.h#includeusing namespace std;void main()coutthe main function:endl;test array5;coutthe second element of array is array1.getint() array1.getfloat()endl;二十五题#includeusing namespace std;class test private: int num; float f1; public: test(int n); test(int n, float f); ;inline test:test(int n) coutInitializing nendl; num = n;test:test(int n, float f) coutInitializing n, fendl; num = n; f1 = f;void main()/ test array03; 若加上此语句,则编译时会出现如下的错误信息:/ error C2512: test : no appropriate default constructor availabletest array13 = 1,2,3;test array2 = test(2,3.5),test(4);test array3 = test(5.5,6.5),test(7,8.5); / array3有2个元素test array4 = test(5.5, 6.5), 7.5, 8.5; / array4有3个元素二十六题#include#includeusing namespace std;class Person public: Person(char *na) / 构造函数 coutcall constructorendl; name=new charstrlen(na)+1; / 使用new进行动态内存分配 if(name!=0) strcpy(name,na); Person(Person&p) / 深拷贝构造函数 coutcall copy constructorendl; name=new charstrlen()+1; / 复制资源 if(name!=0) strcpy(name,); / 复制对象空间 void printname() coutnameendl; Person()delete name; / 析构函数的定义,参见3.3.2小节private: char *name; / 类定义的结束 void main() Person wang(wang); Person li(wang); wang.printname(); li.printname();二十八题class SillyClass public: SillyClass(int&i):ten(10),refI(i) protected: const int ten; / 常量数据成员 int&refI; / 引用数据成员 ;void main() int i ; SillyClass sc(i);二十九题#includeclass RMBpublic: RMB(double value = 0.0); / 构造函数用作类型转换 operator double() return yuan + jf / 100.0; / 类类型转换函数 void display() cout (yuan + jf / 100.0) endl; protected: unsigned int yuan; unsigned int jf;RMB:RMB(double value) yuan = value; jf = ( value - yuan ) * 100 + 0.5;void main() RMB d1(2.0), d2(1.5), d3; d3 = RMB(double)d1 + (double)d2); / 显式转换 d3 = d1 + d2; / 隐式转换,系统首先会调用类类型转换函数把对象d1和d2隐式转换为double数据类型,/ 然后进行相加,加完的结果再调用构造函数隐式转换为RMB类类型,赋给d3。 d3.display();三十题/【例3.15】析构函数和构造函数的调用顺序#include #include class Studentpublic: Student(char* pName=no name,int ssId=0) strncpy(name,pName,40); name39= 0; id = ssId; cout Constructing new student pName endl; Student(Student& s) / 拷贝构造函数 cout Constructing copy of endl; strcpy(name, copy of ); strcat(name,); id=s.id; Student() cout Destructing name endl; protected: char name40; int id;void fn(Student s) cout In function fn()n; / fn函数调用结束时,析构对象svoid main() Student randy(Randy,1234); / 调用构造函数,创建对象randy Student wang(wang,5678); / 调用构造函数,创建对象wang cout Calling fn()n; fn(randy); / 调用fn函数,参数传递时调用拷贝构造函数 cout Returned from fn()n;/ 主函数调用结束时,先析构对象wang,再析构对象randy三十一题#include #include class StudentIDpublic: StudentID(int id=0) / 带缺省参数的构造函数 value=id; cout Assigning student id value endl; StudentID() cout Destructing id value endl; private: int value;class Studentpublic: Student(char* pName=no name,int ssID=0):id(ssID) cout Constructing student pName endl; strncpy(name,pName,sizeof(name); namesizeof(name)-1=n; Student() coutDeconstructing student nameendl;protected: char name20; StudentID id; / 对象成员;void main() Student s(wang,9901); Student t(li);三十二题#includeclass Student / 学生类的定义 public: Student() coutconstructing student.n; semesHours=100; gpa=3.5; Student() coutdestructing student.n; protected: int semesHours; float gpa;class Teacher / 教师类的定义 public:Teacher() coutconstructing teacher.n; Teacher() coutdestructing teacher.n;class Tutorpair / 帮教类的定义 public: Tutorpair() coutconstructing tutorpair.n; nomeeting=0; Tutorpair() coutdestructing tutorpair.n; protected: Student student; Teacher teacher; int nomeeting; / 会晤时间;void main() Tutorpair tp; coutback main.n;三十三题#includeclass A static int i; / 定义静态数据成员 public: A()i+; int list()return i;int A:i=0; / 请注意静态数据成员的初始化格式,无static关键字void main() A a1,a2,a3; couta1.list(),a2.list(),a3.list(); / 显示均为3(因为创建三个对象,三次使得静态数据成员加1)三十四题#include #include class Studentpublic: Student(char* pName =no name) cout create one studentn; strncpy(name,pName,40); name39=0; noOfStudents+; / 静态成员:每创建一个对象,学生人数增1 cout noOfStudents endl; Student()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 六一活动吃喝玩乐活动方案
- 六一活动捉小鸡活动方案
- 六一活动美容活动方案
- 六一烹饪活动方案
- 六一舞蹈趣味活动方案
- 六一趣味捞鱼活动方案
- 六一饮品联名活动方案
- 医保知识考试试题及答案2025
- 药日考试试题及答案
- 药房招聘考试试题及答案
- 《小米智能家居》课件
- 俄语阅读知到智慧树章节测试课后答案2024年秋哈尔滨师范大学
- 2025年广西宏桂资产经营集团招聘笔试参考题库含答案解析
- DG-TJ 08-2343-2020 大型物流建筑消防设计标准
- 燃气公司生产安全事故隐患排查治理体系手册
- 操作系统(鲁东大学)知到智慧树章节测试课后答案2024年秋鲁东大学
- 2024年安徽省合肥市公开招聘警务辅助人员(辅警)笔试必刷测试卷(2)含答案
- 5G无线技术及设备运行维护知到智慧树章节测试课后答案2024年秋青岛工程职业学院
- 机关事业单位财务管理制度(六篇)
- 管理心理学-终结性考核-国开(SC)-参考资料
- 2025年“两新”领域超长期特别国债项目申报策略
评论
0/150
提交评论