c++期末考试复习重点例题总结

上传人:n**** 文档编号:111935313 上传时间:2019-11-04 格式:PDF 页数:19 大小:327.70KB
返回 下载 相关 举报
c++期末考试复习重点例题总结_第1页
第1页 / 共19页
c++期末考试复习重点例题总结_第2页
第2页 / 共19页
c++期末考试复习重点例题总结_第3页
第3页 / 共19页
c++期末考试复习重点例题总结_第4页
第4页 / 共19页
c++期末考试复习重点例题总结_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《c++期末考试复习重点例题总结》由会员分享,可在线阅读,更多相关《c++期末考试复习重点例题总结(19页珍藏版)》请在金锄头文库上搜索。

1、封装/多态定义一个学生类,学生包含的特征信息有学号、姓名、性别和年龄等;功能 (函数)包括设置修改姓名、修改年龄和显示学生个人信息等;构造函数至少能够实现默认 构造、带参数构造和拷贝构造功能;析构函数输出“对象被析构”字样。 定义主函数,应用不同构造函数(包括拷贝构造函数)分别创建多个学生对象,针对每个对 象修改姓名或年龄,输出修改前后学生基本信息。 #include #include using namespace std; class student string name; string sex; int age; public: student();/默认构造函数初始化 student

2、(string na,string se,int ag); string getsex();/私有成员公用,若不想这样用,可以使用友元,即在访问到私有成员的 函数,在其声明处、函数类型前加上 friend int getage();/私有成员公用 student();/析构函数 ; class teacher/管理学生类 private: student stu5;/定义 5 个学生对象数组 public: teacher(); int getMaleAge(); int getFemaleAge(); ; #include #include #include“head.h“ using na

3、mespace std; student:student() this-name=“zhangsan“; this-sex=“male“; this-age=18; student:student(string na,string se,int ag) name=na; sex=se; age=ag; /*-与上面函数等价- student:student(string name,string sex,int age) this-name=name; this-sex=sex; this-age=age; */ /*student:student(string na,string se,int

4、 ag): name(na),sex(se),age(ag) */ string student:getsex() return sex; int student:getage() return age; teacher:teacher() stu0=student(“zhangsan“,“male“,22); stu1=student(“lisi“,“female“,16); stu2=student(“wangwu“,“male“,15); stu3=student(“maliu“,“female“,36); stu4=student(“guiqi“,“male“,52); int tea

5、cher:getMaleAge() int sum=0; int r=0; for(int i=0; iAge=Age; string Person:GetName() return Name; int Person:GetAge() return Age; Person:Person() delete Name; /为何运行还是会出现内存错误 coutmn; try /trycatch 用于捕捉异常 q=a(m,n); coutn; for(i=0;iscore; total_Score+=score; count+; void score:show() coutrun();/不能用 a1.

6、 a1-stop(); a2-run(); a2-stop(); a3-run(); a3-stop(); a4-run(); a4-stop(); return 0; -以下看看就好以下看看就好- 编写一个链表类的模板, 即只能在链表头部进行删除操作, 在链表尾部进行插入操作。 其链表元素类型未知,定义相应的成员函数,包括构造函数、析构函数、插入元素,删除元 素,判断链表是否为空以及求队列中元素数目的函数。 #include using namespace std; template/类模板声明 class node/结构体。节点应为 node,不一定非用 node public: T va

7、l;/数据域 node *next;/指针域。定义:指向下一个箭头 ; template/类模板声明 class list/结构体处理 node *head;/头指针声明 node *tail;/尾指针声明 int size;/记录当前链表内的节点数 public: list() head=new node;/创建头结点由 head 指向(空的头结点) tail=head;/把创建的 head 结点由 tail 指向 size=0;/初始化节点数为 0 head-val=0; void insert (T val)/-添加-/ node *nodes=new node;/创建节点 nodes-

8、val=val; /把 main 传来的值 val,赋给新创建的结点 tail-next=nodes; /把创建的结点由 tail 的 next 指向 tail=nodes; /指针 tail 的指向,向后移一个结点 tail-next=NULL; /最后一个结点的 next 指向空 size+; /长度+1 void delet()/-删除-/ node *p=head;/这里备份 head 的当前指向节点 head=p-next;/把 head 的指向改变为下一节点 delete p;/把 head 的原指向给删除掉 /*-有目的性删除 int 型中数据域- node *p; node *

9、s; int delV alue;/要删除的数 p=head; s=p-next; coutnext; if(s!=NULL) /已经找到,则删除 S 结点 p-next=s-next; delete s; else /没有找到 coutnext;p;p=p-next)/从 head 下一个节点开始,到 NULL coutnext; /指向被释放结点的下一个结点,使单链表不断开 delete temp;/删除当前节点,如此一直下去,尾节点的 next 肯定是指向 NULL 的 ; void main() list intlist; intlist.insert(1); intlist.inse

10、rt(2); intlist.insert(3); list floatlist; floatlist.insert(1.1); floatlist.insert(2.2); floatlist.insert(3.3); list charlist; charlist.insert(“ABC“); charlist.insert(“abc“); intlist.print(); floatlist.print(); charlist.print(); coutreal; coutimag; Complex Complex:operator +(Complex return *this; 等 价

11、 counter t;t.v=v+;return t; Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; Complex Complex:operator -(Complex c.real=real-c2.real; c.imag=imag-c2.imag; return c ; /将得到的内容(包含在对象 c 中)返回给类 Complex Complex Complex:operator *(Complex c.real=real*c2.real-imag*c2.imag; c.imag=real*c2.imag+i

12、mag*c2.real; return c; Complex Complex:operator /(Complex c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c; void Complex:display() cout“(“real“,“imag“i)“endl; Complex:Complex() cout“函数被析构“endl; #inclu

13、de using namespace std; #include “head.h“ int main() Complex c1,c2,c3; c1.set(); cout“c1=“; c1.display(); c2.set(); cout“c2=“; c2.display(); c3=c1+c2; cout“c1+c2=“; c3.display(); c3=c1-c2; cout“c1-c2=“; c3.display(); c3=c1*c2; cout“c1*c2=“; c3.display(); c3=c1/c2; cout“c1/c2=“; c3.display(); return 0;

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

最新文档


当前位置:首页 > 大杂烩/其它

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