已阅读5页,还剩39页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
高级程序设计语言本 学 期 实 验 指 导 辽宁科技大学实验内容实验一 类与对象基本程序设计(一)【实验类型】验证性 【实验要求】必做【实验目的】1了解C+面向对象程序设计的基本概念以及了解C+程序的基本结构。2了解类,对象的概念,掌握类及类中成员函数的定义及使用方法。3掌握对象的定义及使用方法。4了解构造函数,析构函数,拷贝构造函数的作用,特点,定义方式及使用方法。【实验内容】实验题1定义一个FDAccount类,用以描述一个定期存折(fixed deposit),实现现金支取。余额合计。信息显示等。存折基本信息包括帐号,账户名称,存款余额,存取期限(以月为单位),存款利率(以百分点为单位)等。提示:存折的基本信息定义为存折类的私有的数据成员,利用构造函数的初始化存折类对象,在利用构造函数的参数表传入实参,初始化存折对象的数据成员。实验题2设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个顶点的坐标,该类能够计算矩形的面积。提示:两个点的坐标作为矩形类的数据成员。利用坐标计算矩形长和宽,然后求面积。实验题3设计一个person类,其属性包括name和id,其中name属性为指针,分别利用person类构造函数和拷贝构造函数建立对象,打印每个person类对象的信息。要求分别编写浅拷贝构造函数和深拷贝构造函数调试程序。提示:要在构造函数中为person对象的name属性分配动态内存,在析构函数中,释放掉申请的动态内存。注意不要让同一块动态内存被释放多次。【参考程序】实验题1参考程序#include #include class FDAccountpublic:/构造函数FDAccount(char *ID,char *depositor,double amount,int period,double rate);double fetch(char *ID,char *depositor,double amount); /支取到期存款void update();/计算到期账户余额void show(); /显示账户基本信息protected:double interest_rate;private:char *accounts;char *name; double balance; /存款余额int term; /存款期限;FDAccount:FDAccount(char *ID,char *depositor,double amount,int period,double rate)name=depositor;accounts=ID;if(amount0|rate0)cout数据不正确endl; exit(1);balance=amount;term=period;interest_rate=rate;double FDAccount:fetch(char *ID,char *depositor,double amount)cout帐号 账户名称 支取金额 endl;coutaccounts name amountendl;balance=balance-amount;return balance;void FDAccount:update()balance=balance+balance*(interest_rate/100.00)*(term/12.0);void FDAccount:show()cout显示账户基本信息:endl;cout帐号 帐号名称 期限 利率 endl;coutaccounts name term interest_rateendl;cout目前账户余额为:balanceendl;void main()FDAccount depositor(0034,王涛,10078,18,1.98);depositor.show();coutendl;cout存款已到期!nendl;depositor.update();depositor.show ();coutendl;cout支取存款:endl;depositor.fetch(0034,王涛,5000);coutendl;depositor.show();实验题2参考程序#include class Rectanglepublic:Rectangle(int top ,int left,int bottom,int right);Rectangle()int GetTop() const return itsTop;int GetLeft() const return itsLeft;int GetBottom() const return itsBottom;int GetRight() const return itsRight;void SetTop(int top) itsTop=top;void SetLeft(int left) itsLeft=left;void SetBottom(int bottom) itsBottom=bottom; void SetRight(int right) itsRight=right;int GetArea() const;private:int itsTop;int itsLeft;int itsBottom;int itsRight;Rectangle:Rectangle(int top,int left,int bottom,int right)itsTop=top;itsLeft=left;itsBottom=bottom; itsRight=right;int Rectangle:GetArea()constint Width=itsRight-itsLeft;int Height=itsTop-itsBottom;return (Width*Height);int main()Rectangle MyRectangle(100,20,50,80);int Area=MyRectangle.GetArea();coutArea:Areaendl;return 0;实验题3参考程序(浅拷贝情况)#include#includeclass Person public: Person(char *pName=noname,int pid=100) coutcall constructorendl; strcpy(name,pName); id=pid; Person(Person &p) coutcall copy constructorendl; strcpy(name,); id=p.id; void printinfo() coutname,idendl;private: char name20; int id; void main() Person wang(wang); wang.printinfo(); Person li(wang); li.printinfo();实验题3参考程序(深拷贝情况)#include#includeclass Person public: Person(char *pname=noname,int pid=100) coutcall constructorendl; name=new charstrlen(pname)+1; if(name!=0) strcpy(name,pname); id=pid; Person(Person &p) coutcall copy constructorendl; name=new charstrlen()+1; if(name!=0) strcpy(name,); id=p.id;/name1=z; void printinfo() coutname,idendl;/*- void GetAddr() /取对象name成员的内存地址 coutnames address is: ; coutname)endl;-*/ Person()delete name; private: char *name; int id; void main() Person wang(wang,101); wang.printinfo(); /wang.GetAddr(); Person li(wang); li.printinfo(); / li.GetAddr();【思考题】1构造函数可以是私有成员么?2为什么同类的不同对象,调用相同的成员函数时,这些成员函数能够准确的操作相应的对象呢?实验二 类与对象基本程序设计(二)【实验类型】验证性 【实验要求】必做【实验目的】1巩固类和对象的定义及使用方法。2了解静态成员的概念,特点,定义方式,作用。3了解友元的概念,特点,定义方式,作用。4掌握组合类的概念,了解组合类对象构造和析构的过程。【实验内容】实验题1商店销售某一商品,商店每天公布统一的折扣(discount),同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者,还可以享受9.8折优待。现已知当天3个销货员的销售情况为: 销货员号(num) 销货件数(quantity) 销货单价(price) 101 5 23.5 102 12 24.56 103 100 21.5编写程序,计算出当日此商品的总销售款sum,以及每件商品的平均售价,要求用静态数据成员和静态成员函数。提示:将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,在定义静态成员函数Average(求平均价)和display(输出结果)实验题2定义单链表类,完成单链表的相关操作:。(1)显示输出一个已经生成的链表(2)对一个空表插入链表项,插入的新表被放在链表的头部,即前插入(3)对一个空表追加链表项,追加的新链表项被放在表尾部(4)两个链相连接,既将一个链表接在另一个链表的尾部(5)将一个链表的数据项逆向输出;(6)求一个链表的数据项数即长度。【参考程序】实验题1参考程序#include class Product public: Product(int n,int q,float p):num(n),quantity(q),price(p); void total(); static float average(); static void display(); private: int num; int quantity; float price; static float discount; static float sum; static int n; ;void Product:total() float rate=1.0; if(quantity10) rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; void Product:display() coutsumendl; coutaverage()endl; float Product:average() return(sum/n); float Product:discount=0.05;float Product:sum=0;int Product:n=0;int main() Product Prod3= Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5) ; for(int i=0;i3;i+) Prodi.total(); Product:display(); return 0; 实验题2参考程序#include class List;class Itempublic:friend class List;Item(int d=0) /节点类代参数的构造函数data=d;next=0;private:Item *next; /指针域int data; /数据域;class Listpublic:List()list=0; /链表类的默认构造函数List(int d)list=new Item(d); /链表类带参数的构造函数int print(); /打印链表成员函数int insert(int d=0); /向链表中插入节点int append(int d=0); /向链表中增加节点void cat(List &il); /连接两个链表void reverse(); /倒置一个链表int length(); /取链表的长度private:Item *end(); /取链表指向最后一个节点的指针Item *list;int List:print()if (list=0)coutemptyendl;return 0;cout(;int cnt=0;Item *pt=list;while(pt)if (+cnt%40=1&cnt!=1)coutendl;coutdatanext;cout)next=list;list=pt;return d;int List:append(int d)Item *pt=new Item(d);if (list=0)list=pt;else(end()-next=pt;return d;Item *List:end()Item *prv,*pt;for (prv=pt=list;pt;)prv=pt;pt=pt-next;return prv;void List:cat(List &il)Item *pt=il.list;while(pt)append(pt-data);pt=pt-next; void List:reverse()Item *pt,*prv,*tmp;prv=0;pt=list;list=end();while(pt!=list)tmp=pt-next;pt-next =prv;prv=pt;pt=tmp; list-next=prv;int List:length()int cnt=0;Item *pt=list; for(;pt;pt=pt-next,cnt+);return cnt;void main() List list1; list1.print(); for (int i=10;i18;i+) list1.insert(i); coutlist1:; list1.print(); List list2; for(i=15;i20;i+) list2.append(i); coutlist2:; list2.print(); coutlist1 length:list1.length()endl; list2.print(); list2.reverse(); coutlist2:; list2.print(); coutlist2 length:list2.length()endl;【思考题】1看看自己还能给实验题目2添加些什么样的操作?2如果用静态成员函数访问非静态数据成员,可以么?实验三 类的继承【实验类型】验证性 【实验要求】必做【实验目的】1了解类的继承的概念和按继承方式的继承分类及各种继承方式的特点。2了解并掌握派生类对象的构造和析构过程。3了解并掌握单继承,多继承的定义方式。4了解多继承情况下产生的二义性及解决办法。5了解虚基类的定义,原理和用法,了解存在虚基类的情况下,派生类对象的构造和析构过程。【实验内容】实验题1.定义一个继承与派生关系的类体系,在派生类中访问基类成员。先定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员;以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数;定义一个线段类,以两个点类对象作数据成员,定义显示、求面积及长度函数,线段类采用聚合方式,因为有两个端点,不能用派生。实验题2.由汽车类派生出轿车类和卡车类,再由轿车类和卡车类多重派生出皮卡类。所谓皮卡指的是轿车的后备箱改为卡车似的后厢,可以兼运少量货物。汽车类可以说明为虚基类,以避免在皮卡类中出现两组汽车类的数据,请与未说明为虚基类的情况对比。【参考程序】实验题1参考程序#include #include using namespace std;#define PI 3.14159class Pointfriend class Line;protected:double x, y ; public:Point()x = 0 ; y = 0 ; Point(double xv,double yv) x = xv; y = yv; double Area()return 0;void Show()coutx=x y=yPoint:operator=(cir); /在派生类中重载复制赋值操作符有固定的标准格式radius=cir.radius;return *this;double Area()return PI*radius*radius;void Show() /可以直接访问基类的数据成员coutx=x y=y radius=radiusendl;class LinePoint start,end;/对象成员public:Line():start(0,0),end(0,0) /注意对象成员初始化的方法Line(double xv1,double yv1,double xv2,double yv2) : start(xv1,yv1),end(xv2,yv2) double GetLength()return sqrt(start.x-end.x)*(start.x-end.x)+(start.y-end.y)*(start.y-end.y);double Area()return 0;void Show()coutstart point:n;start.Show();coutend point:n;end.Show();int main()Point pt(0,0);Circle cl1(100,100,10),cl2(cl1),cl3;Line ln1(0,0,100,100);cout点面积:pt.Area()endl;pt.Show();coutcl1圆面积:cl1.Area()endl;cl1.Show();coutcl2圆面积:cl2.Area()endl;cl2.Show();cl3=cl1;coutcl3圆面积:cl3.Area()endl;cl3.Show();cout线面积:ln1. Area()t线长度:ln1. GetLength()endl;ln1.Show();return 0;实验题2参考程序#include#includeusing namespace std;class automobileprotected:string model; /型号double cylinders_capability; /排量int wheels; /轮数double price; /价格public:automobile(string mod=#,double cl=0,int wh=4,double pr=0)model=mod;cylinders_capability=cl;wheels=wh;price=pr;void display()cout汽车型号:modelt排量:cylinders_capability升endl;cout轮数:wheelst价格:priceendl;class car:public virtual automobileprotected:int seats; /座位数int safeguards; /保护措施数量public:car(string mod=#,double cl=0,int wh=4,double pr=0,int st=5,int saf=0):automobile(mod,cl,wh,pr)seats=st;safeguards=saf;void display()automobile:display();cout座位数:seatst保护措施数量:safeguardsendl;class truck:public virtual automobileprotected:double carrying_capacity; /载重量public:truck(string mod=#,double cl=0,int wh=4,double pr=0,double cc=0): automobile(mod,cl,wh,pr)carrying_capacity=cc;void display()automobile:display();cout货物载重量:carrying_capacity吨endl;void display1()cout货物载重量:carrying_capacity吨endl;class car_truck:public car,public truckpublic:car_truck(string mod=#,double cl=0,int wh=4,double pr=0,int st=5,int saf=0,double cc=0): car(mod,cl,wh,pr,st,saf),truck(mod,cl,wh,pr,cc),automobile(mod,cl,wh,pr)void display()car:display();truck:display();void reset() /从类car修改类automobile中的mode,实际仅有一个automobile基类car:model=!;class car1:public automobileprotected:int seats; /座位数int safeguards; /保护措施数量public:car1(string mod=#,double cl=0,int wh=4,double pr=0,int st=5,int saf=0):automobile(mod,cl,wh,pr)seats=st;safeguards=saf;void display()automobile:display();cout座位数:seatst保护措施数量:safeguardsendl;class truck1:public automobileprotected:double carrying_capacity; /载重量public:truck1(string mod=#,double cl=0,int wh=4,double pr=0,double cc=0):automobile(mod,cl,wh,pr)carrying_capacity=cc;void display()automobile:display();cout货物载重量:carrying_capacity吨endl;void display1()cout货物载重量:carrying_capacity吨endl;class car_truck1:public car1,public truck1public:car_truck1(string mod=#,double cl=0,int wh=4,double pr=0,int st=5,int saf=0,double cc=0): car1(mod,cl,wh,pr,st,saf),truck1(mod,cl,wh,pr,cc)void display()car1:display();truck1:display();void reset()car1:model=!;/仅修改类car的基类automobile的model,类truck的基类automobile的model未改; int main()car_truck ct(#,1.6,4,8.4,5,3,0.6);ct.display();ct.reset();ct.display(); /显示出两个同样的model,实际是一个car_truck1 ct1;ct1.display();ct1.reset();ct1.display();/显示出有两个不同的modelreturn 0;【思考题】1如何实现对类的对象成员的私有数据的访问?2基类,派生类和类的对象成员的构造函数和析构函数的调用顺序是怎样的?实验四 多态性【实验类型】验证性 【实验要求】必做【实验目的】1了解多态性的相关概念。2了解运算符重载原则,掌握常用运算符重载方法。3了解虚函数的定义,原理,特点,适用方法。4了解纯虚函数,抽象类概念,定义,作用,适用方法。5了解虚析构函数。【实验内容】实验题1.为Rectangle类增加加、减复合赋值和加、减运算符重载函数。加减复合赋值语义定义为固定长方形的左上角,对右下角的坐标进行加减运算,使新矩形的长宽为原两矩形长宽之和或差。将加、减复合赋值函数定义为成员函数,将两个矩形加、减运算定义为友元函数。实验题2.应用抽象类,求圆,圆内接正方形和外切正方形的面积和周长。【参考程序】实验题1参考程序class Rectangle int left, top ;int right, bottom;public:Rectangle(int l=0, int t=0, int r=0, int b=0);Rectangle(); /析构函数,在此函数体为空void Assign(int l, int t, int r, int b);void SetLeft(int t) left = t; / 以下四个函数皆为内联成员函数void SetRight( int t ) right = t;void SetTop( int t ) top = t;void SetBottom( int t ) bottom = t;void Show();void operator +=(Rectangle&);void operator -=(Rectangle&); friend Rectangle operator+(Rectangle &, Rectangle&);friend Rectangle operator-(Rectangle &, Rectangle&);/将上述内容保存为rect.h#include #include rect.husing namespace std;Rectangle:Rectangle(int l , int t, int r, int b) left = l; top = t;right = r; bottom = b; / 构造函数,带默认参数,默认值为全0,在声明中指定void Rectangle:Assign(int l, int t, int r, int b)left = l; top = t;right = r; bottom = b;void Rectangle:Show()coutleft-top point is (left,top)n;coutright-bottom point is (right,bottom)endl;/当用.h头文件时,不用endl输出次序可能出错void Rectangle:operator+=(Rectangle& rect)int x = rect.right - rect.left;int y = rect.bottom - rect.top;right += x;bottom += y;void Rectangle:operator-=(Rectangle& rect)int x = rect.right - rect.left;int y = rect.bottom - rect.top;right -= x;bottom -= y;Rectangle operator- ( Rectangle &rect1, Rectangle& rect2)/矩形相减,从rect1中减去rect2的长度和宽度rect1 -= rect2;return rect1;Rectangle operator+ ( Rectangle &rect1, Rectangle& rect2)/矩形相加,从rect1中加上rect2的长度和宽度rect1 += rect2;return rect1;/ 将上述内容保存为rect.cpp#include using namespace std;#include rect.hint main()Rectangle rect;cout初始rect:endl;rect.Show();rect.Assign(100,200,300,400);cout赋值后rect:endl;rect.Show();Rectangle rect1(0,0,200,200);cout初始rect1:endl;rect1.Show();rect+=rect1;cout与rect1相加后的rect:endl;rect.Show();rect-=rect1;cout减去rect1后的rect:endl;rect.Show();Rectangle rect2;rect2 = rect+rect1;coutrect与rect1相加所得rect2:endl;rect2.Show();rect2 = rect-rect1;coutrect减去rect1所得rect2:endl;rect2.Show();return 0;实验题2参考程序#include #includeclass shape / 抽象类的定义public:virtual double area()=0;virtual double perimeter()=0;class circle:public shape / 圆类protected: double radius;public:circle(double r)radius=r;double area() return radius*radius*3.14;double perimeter() return 2*radius*3.14;class Outersquare:public circle /外切正方形protected: double border1;public:Outersquare(double b):circle(b)border1=2*b;double area()return border1*border1;double perimeter() return 4*border1;class Innersquare:public circle / 内接正方形protected: double border2;public:Innersquare(double b):circle(b)border2=sqrt(2*b*b);double area()return b
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026北京顺义区教委所属事业单位第二次招聘教师189人备考题库及答案详解参考
- 2026湖南岳阳市湘阴县县直事业单位“四海揽才”招聘14人备考题库及答案详解(全优)
- 2026年吉安市青原区睿才人力资源有限公司见习人员招募备考题库附答案详解ab卷
- 2026苏州电瓷厂股份有限公司招聘12人备考题库含答案详解(黄金题型)
- 2026航天科工集团数字技术有限公司部分岗位招聘11人备考题库及答案详解一套
- 2026广东江门市台山市卫生健康系统事业单位招聘43人备考题库及答案详解(夺冠系列)
- 2026滁州市轨道交通运营有限公司第一批次校园招聘21人备考题库及答案详解(夺冠系列)
- 2026新疆天宜养老有限责任公司招聘6人备考题库附答案详解(基础题)
- 光伏逆变器常见故障及标准化解决指南
- 2026 中国具身智能企业出海全景报告-资本狂飙与全球化拐点
- 物流运输风险识别与控制
- 关于杭州市“社交主题酒吧”运营模式与典型案例的调研分析
- 阿里巴巴集团内部审计制度
- 纺粘针刺非织造布制作工操作知识考核试卷含答案
- 2025年国防军事动员教育知识竞赛题库及答案(共50题)
- 泛光照明施工安全措施方案
- KPS评分表模板及使用指南
- 2025年专利代理师资格真题及答案解析
- 2025年1月浙江省高考技术试卷真题(含答案)
- 两办关于进一步加强矿山安全生产意见
- 2025年湖南邵阳市中考物理考试真题及答案
评论
0/150
提交评论