实验二类与对象_第1页
实验二类与对象_第2页
实验二类与对象_第3页
实验二类与对象_第4页
实验二类与对象_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验二 类与对象一、实验目的1、学习类与对象的定义,掌握类与对象的使用方法。2、学习数据成员与成员函数的访问方式,理解构造函数和析构函数的定义 与执行过程,学会构造函数的重载方法。3、掌握数组与指针的定义与使用方法, 理解数组与指针的存储分配与表示4、掌握用指针和引用向函数传递参数。5、掌握静态数据成员和静态成员函数的使用。6、理解友元与友元函数的作用与使用方法。、实验内容1、下面是一个计算器类的定义,请完成该类成员函数的实现class Counterpublic:Counter(int number);void increment(); / 给原值加 1void decrement(); /

2、给原值减 1int getValue(); /取得计数器值int print(); /显示计数private:int value;#includeclass countpublic:counter(int number);void increment();void decrement();int getvalue(int);int print();private:int value;void count:increment()int a=value+1;void count:decrement()int b=value-1;int count:getvalue(int s)value=s;re

3、turn 0;int count:print()coutvalue+1=value+1endl; coutvalue-1=value-1endl; return 0;void main()count s;s.getvalue(5);s.print();/2、根据注释语句的提示,实现类 Date的成员函数。#in cludeclass Datepublic:void prin tDate();显示日期void setDay(i nt d);/ 设置日的值void setMo nth(i nt m);/设置月的值void setYear(i nt y);/设置年的值private:int day,

4、 mon th,year;void Date:pri ntDate()coutvvyearvv年month月day日void Date:setDay(int d)day=d;void Date:setMonth(int m)month=m;void Date:setYear(int y)year=y;int main()Date testDay; testDay.setDay(5); testDay.setMonth(10); testDay.setYear(2014); testDay.printDate(); return 0;x C:XDucu*eBts and SexiincsXABi

5、nistratoi.ese空014年10月占日匸”去玄amy key td contd3、建立类cylinder , cylinder的构造函数被传递了两个 double值,分别 表示圆柱体的半径和高度。用类cyli nder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol(),用来显示每个cyli nder 对象的体积。#in cludeclass cyli nderprivate:double r;double h;double v;public:cyli nder();double vol();cyli nder(double,double)

6、;cyli nder:cyli nder(double m,double n ):r(m),h( n)cyli nder:cyli nder()coutC on structor callede ndl;double cyli nder:vol()double v;v=3.14*r*r*h;return v;double mai n()cyli nder a(1.1,2.2);cout体积=a.vol()endl; return 0;4、构建一个类book,其中含有两个私有数据成员qu和price ,建立一个有 5个元素的数组对象,将qu初始化为15,将price初始化为qu的10倍。显示 每

7、个对象的qu*price值。#in cludeclass bookprivate:int qu;int price;int s;public:book(i nt p,i nt q):qu(p),price(q)void prin t()coutqu*pricee ndl;;int mai n()book a(1,10);a. pri nt();book b(2,20);b. pri nt();book c(3,30);c. pri nt();book d(4,40);d. pri nt();book e(5,50);e. pri nt(); return 0;5、修改上题,通过对象指针访问对象

8、数组,使程序以相反的顺序显示对象 数组的qu*price值。#in cludeclass bookprivate:int qu;int price;int s;public:book(int p)qu=p;price=qu*10;int print() return(qu*price);int main()book a5=1,2,3,4,5;book *p;p=&a4;for(int i=4;i=0;i-)coutprint()endl; p-;return 0;6 构建一个类Stock,含字符数组stockcode及整型数据成员quan、双 精度型数据成员price。构造函数含3个参数:字符

9、数组na及q、p。当定义 Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2个和第3个参数分别赋给quan和price。未设置第2个和第3个参数时,quan 的值为1000, price的值为8.98。成员函数print()使用this指针,显示对象 内容。#in clude#in cludeclass Stockchar stockcode10;int qua n;double price;public:Stock(char na10,int q=1000,double p=8.98);void prin t();Stock:Stock(char na10,i

10、nt q,double p) strcpy(stockcode,na);quan=q;price=p;void Stock:print()coutstockcode,quan,priceendlvoid main()Stock m(sdgjgj,798,9.89);m.print();Stock n(sljf); cout 默认: n.print();x C: Ducu*enis and SexiinSABinist ratoi. LEK0OA273L042DetutVc. ese.JIAe gi e d七卧 is黒 Banyto99outxnue7、参考课本例子,建立一个源程序文件,在此文件

11、中建立一个新的类,将 新建的类命名为Rect。class Rectpublic:int Areant();double Area_double();Rect(double length ,double width);Rect(int length ,int width);virtual Rect();private :int nLen gth;int nWidth;double dLe ngth;double dWidth;;【要求】(1) 向 Rect 类中添加数据成员及成员函数, 并完善成员函数的功能。 如设计 一个 Area_int() 函数,计算边长为整型的长方形的面积; 设计一个 A

12、rea_double() 函数,计算边长为 double 型的长方形的面积。(2) 重载构造函数。一种构造函数用整型变量记录长方形的长和宽,另一种 构造函数用 double 型记录。(3) 体现对象的构造和析构过程。例如,在构造函数中用cout”I am theconstructor! ” endl ; 在 析 构 函 数 中 输 出 cout ”Iam thedestructo r ” endl 。(4) 在 main() 函数中定义两个 Rect 类的对象,一个对象用实例实现 ( 就像定 义普通的变量一样),另一个对象用指针实现(利用关键字new,给指针分配内存 空间) 。并用不同的参数,

13、以调用不同的构造函数体现构造函数的重载。#includeclass Rectpublic:int Area_int();double Area_double();Rect(double length,double width);Rect(int length,int width);virtual Rect();private:int nLength;int nWidth;double dLength;double dWidth;int Rect:Area_int()int s; s=nLength*nWidth;coutint 的长方形的面积 :sendl; return 0;double R

14、ect:Area_double()double k;k=dLength*dWidth;coutdouble 型的长方形的面积 : kendl;return 0;Rect:Rect(int length,int width)nLength=length;nWidth=width;coutI am the constructor!endl; Rect:Rect()coutI am the destructorendl;Rect:Rect(double length,double width) dLength=length;dWidth=width;coutI am the constructor!

15、Area_i nt();delete p;Rect *q=new Rect(3.2,3.4);q-Area_double();delete q;EE*C;LiciiBeat s and SettingsAdainistor. 1EIOFO-12T3104DebufXteseI am the cansI am the cans true 101*s七的丧方评的面积皿皿aJb型向長芳形的面积=7-82I am fzlkc cartstiructor?init的长方形的面积江倉I cMli the dlisti-UCLoir*I duii Lht: eunstructorfdejufjg型曲长方形的

16、面积:10-88I an the destructoi*1 an the destruct01*I an the de st met di*Presskey to continue8、声明一个Student,在该类中包括一个数据成员 score (分数)、两个静 态数据成员total_score (总分)和count(学生人数);还包括一个成员函数 account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员 函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。在 main()函数中,输入某班学生的成绩,并调用上述函数求 出全班学

17、生的成绩之和和平均分。#includeclass studentdouble score;static double tatal_score;static int count;static double ave;public:void account(double);static double sum();static double average();void print();void student:account(double m) score=m; tatal_score=tatal_score+m;+count;double student:sum()return(tatal_sco

18、re);double student:average()ave=tatal_score/count;return ave;void student:print()cout 人数为: countendl;aveendl;cout 总成绩为 : tatal_score 平均成绩为: int student:count=0;double student:tatal_score=0.0;double student:ave=0.0;void main() student a;a.account(97);a.sum();a.average(); student b;b.account(87);b.sum

19、();b.average();b.print();9、设计一个用来表示直角坐标系的Locatio n类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象 限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:A(x1,y1),B(x2,y2)Dista nce=d其中:x1、y1、x2、y2为指定的坐标值,d为两个坐标点之间的距离。#in clude#in cludeclass locati ondouble x;double y;public:locati on (double,double);void print(

20、location m);friend void print(location &,location &);location:location(double m,double n) x=m;y=n;void location:print(location m)double dx=x-m.x;double dy=y-m.y;double d=sqrt(dx*dx+dy*dy);coutAB 的 distance :dendl; void print(location &a,location &b)double dx=a.x-b.x;double dy=a.y-b.y;double d=sqrt(d

21、x*dx+dy*dy);coutAB 的 distance :dendl; void main()location A(-3.0,-2.0);location B(-4.3,4.3);A.print(B);print(A,B);10、使用C+啲string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前的 5 个字符串是: Germany Japan、American、British 、France按逆转后的顺序输出字符串为:France、British 、American、Japan、Germany#in clude#in cludeusing n amespace std;void mai n()stri ng s5=Germa ny, Jap a n,America n,Bri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论