




已阅读5页,还剩44页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验一 C+类的定义及成员引用一、实验目的:1. 掌握类和对象的概念及特性;2. 掌握C+类和对象的定义;3. 掌握对象成员的基本引用方法;4. 掌握简单的面向对象程序的编写;二、实验任务题目1 图形类设计设计三个图形类:Circle(圆)、Rectangle(矩形)、Triangle(三角形);1、Cirlce 类基本信息:圆心坐标、半径;Rectangle 类基本信息:长、宽;Triangle 类基本信息:三个顶点坐标;其中:成员变量为 private 属性,成员函数为public 属性;2、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show(),修改基本信息的函数Set(形参)。以Circle 类为例:通过GetArea()计算圆的面积,Show()函数中显示圆心坐标、直径、周长、面积等基本信息;Set(int x,int y, int r)函数可以修改圆心坐标和半径。3、当输入数据为不合理数据时(例如:输入的三角形的顶点是否能组成一个三角形),提示用户输入错误;测试数据1、Circle 类测试数据:圆心:(20,30) 半径:5(-20,40) 30(40,-10) -52、Rectangle 类测试数据:顶点坐标:20,5040,5-9,1043,-83、Triangle 类测试数据:顶点:(20,30)、(40,50)、(40,20)(10,10)、(50,10)、(35,10)源程序:#include#include#includeusing namespace std;/圆的面积 class Circleprivate:int x;int y;int r;public:Circle ()int x=0;int y=0;int r=0;float Circle_GetArea();void Circle_show(); void Circle_set(int a,int b,int c)x=a;y=b;r=c;float Circle:Circle_GetArea()float s;s=3.14*r*r;return s;void Circle:Circle_show() float s;if(r0)cout(x,y)setw(6)rsetw(6)2*rsetw(6)2*3.14*rsetw(6)sendl;elsecout输入的数据错误!=b) a=b; if(a=c)a=c;if(b=c)b=c; if(x1-x4)*(x1-x2)+(y1-y4)*(y1-y2)=0&(x1-x2=x4-x3)&(y1-y2=y4-y3) length=a; width=b; ;float Rectangle: Rectangle_GetArea() float z;z=length*width;return z;void Rectangle:Rectangle_show() float z;if(z0)cout矩形的长:lengthsetw(2) 宽: widthsetw(2) 面积: zendl; elsecout输入数据错误!l3&l1+l3l2&l2+l3l1) d=(l1+l2+l3)/2; s=sqrt(d*(d-l1)*(d-l2)*(d-l3); else s=0;void Triangle_show();void Triangle_set(int x1,int y1,int x2,int y2,int x3,int y3) m1=x1;n1=y1;m2=x2;n2=y2;m3=x3;n3=y3; float a,b,c;a=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);b=sqrt(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); c=sqrt(x3-x2)*(x3-x2)+(y3-y2)*(y3-y2); l1=a; l2=b; l3=c; ;void Triangle:Triangle_show()float s;int c;c=!s;switch(c)case 0: cout三角形三点坐标;setw(2)(m1,n1)setw(2)(m2,n2)setw(2)(m3,n3)endl; cout三角形边长:setw(2)l1 l2 l3endl; cout三角形的面积:setw(2)sendl; break;case 1: cout输入数据错误!x1y1r1; cinx2y2r2;cinx3y3r3;cina1b1;cina2b2;cina3b3;cina4b4;cink1l1k2l2k3l3;cink4l4k5l5k6l6; c1.Circle_set( x1, y1,r1); c2.Circle_set(x2, y2, r2); c3.Circle_set( x3, y3, r3);r.Rectangle_set(a1,b1,a2,b2,a3,b3,a4,b4);t1.Triangle_set(k1,l1,k2,l2,k3,l3);t2.Triangle_set(k4,l4,k5,l5,k6,l6);cout-圆-endl;cout圆心坐标setw(6)直径setw(6)半径setw(6)周长setw(6)面积endl;c1.Circle_GetArea();c1.Circle_show();c2.Circle_GetArea();c2.Circle_show();c3.Circle_GetArea();c3.Circle_show();cout-矩形-endl;r.Rectangle_GetArea();r.Rectangle_show();cout-三角形- endl;t1.Triangle_GetArea();t1.Triangle_show();t2.Triangle_GetArea();t2.Triangle_show();return 0;运行结果: 题目2 学生类设计设计一个 Studnet(学生)类1、基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业;其中:基本信息为 private 属性,成员函数为public 属性;2、Student 类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数3、基本功能:1)可以从键盘输入学生的基本信息;2)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:班级、专业等;3)Show()函数显示学生信息;4) 当输入数据为不合理数据时,提示用户输入错误;测试数据1、学号: 姓名:zhangesan 出生日期:85 年11 月年级:05 级班级:jy01 院系:computer 专业:application2、学号: 姓名:lihuan 出生日期:84 年11 月3、学号: 姓名:wang 出生日期:85 年7 月年级:05 级班级:jy01 院系:computer 专业:software4、学号: 姓名:zhan 出生日期:85 年11 月年级:05 级班级:jy025、学号: 姓名:song 出生日期:85 年11 月思考与扩展采用什么方式可以让学号自动按输入顺序生成?源程序代码:#include#include#includeusing namespace std;class Studentprivate:int number;string name;string year;string grade;string clas;string department;string profession;public:Student( )grade=05级; Student(string cla); /Student:Student(int num=):number(num)void setInto( int b, string b1,string b2,string b3,string b4,string b5,string b6) number=b;name=b1;year=b2;grade=b3;clas=b4;department=b5;profession=b6;void show();Student:Student(string cla)clas=cla; void Student:show() cout学号:numbersetw(6)姓名:namesetw(10)出生日期:yearsetw(6)年级:gradesetw(6)班级:classetw(6)院系:departmentsetw(6)专业:professionendlendl; int main() Student stud5; int i,j,num;for(i=0,num=;ia1a2a3a4a5a6;studi.setInto(num,a1, a2, a3,a4, a5,a6);studi.show(); return 0;实验二 C+中的构造函数与析构函数一、实验目的:5. 掌握C+中使用构造函数创建对象;6. 掌握C+中使用析构函数释放对象;7. 掌握构造函数的重载特性;8. 掌握类相关的指针及数组的使用方法;9. 掌握const成员及static成员的特性;10. 掌握友元的特性。二、实验任务题目 人员管理设计某小型公司的 employee(人员)类(1)类1)employee 类:基本信息:编号、姓名、性别、出生日期、职位等;出生日期使用自定义的 Date(日期)类;其中:基本信息为 private 属性,成员函数为public 属性;多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数;可以从外部访问类成员的友员函数;2)Date 类:成员变量:年、月、日成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day)GetYear()、GetMonth()、GetDay()(2)基本功能:1)职工信息的录入;2)职工信息的显示;3)用对象数组保存已输入的职工对象;4)可以修改人员的基本信息,如:姓名、职位等;5)可以通过编号或姓名进行人员查询;(3)实验提示1)注意带参数的构造函数和带默认参数的构造函数的声明与定义;2)定义employee 类的成员变量时,应注意变量类型的声明;3)在查询时,通过申明成employee 类的友元函数来访问类的成员变量;实验提示1、注意带参数的构造函数和带默认参数的构造函数的声明与定义;2、定义employee 类的成员变量时,应注意变量类型的声明;3、在查询时,通过申明成employee 类的友元函数来访问类的成员变量;测试数据1、编号:10001 姓名:jack 出生日期:80 年11 月1 日职位:普通2、编号:10002 姓名:andy 出生日期:75 年1 月9 日职位:经理3、编号:10003 姓名:alex 出生日期:81 年4 月3 日职位:秘书4、编号:10005 姓名:lili 出生日期:82 年10 月1 日职位:技师源代码: #include #include #define N 100using namespace std;class Dateprivate: int year; int month; int day;public: void SetYear(int x) year=x; void SetMonth(int x) month=x; void SetDay(int x) day=x; int GetYear() return year; int GetMonth() return month; int GetDay() return day; Date():year(2000),month(1),day(1) Date(int x, int y,int z) year=x; month=y; day=z; ;class employeeprivate: int no; string name;string sex; Date birth;string pos;public: employee():no(0),birth(2000,1,1) name=Andy; sex=male; pos=intern; employee(int n, string str,string y,Date day,string p) no = n; name=str; sex=y; birth=day; pos=p; friend string Getname(employee x) string name1; name1 =; return name1; friend int Getno(employee x) return x.no; friend string Getsex(employee x) string sex1; sex1=x.sex ; return sex1; friend Date Getbirth(employee x) return x.birth; friend string Getpos(employee x) string pos1; pos1=x.pos; return pos1; friend void display() coutntt=endl; couttt1:录入 2:显示 3:修改 4:查找 0:退出endl; couttt=endl; coutchoose; while(choose!=0) switch(choose) case 1: if(num=100) coutfull!n; continue; coutn; coutymd; coutstr; couts; coutp; personsnum+=new employee(n,str,s,Date(y,m,d),p); break; case 2: cout编号t姓名t性别t生日tt职务endl; for(i=0;inum;i+) coutGetno(*personsi)tGetname(*personsi)tGetsex(*personsi)tGetbirth(*personsi).GetYear() 年Getbirth(*personsi).GetMonth()月Getbirth(*personsi).GetDay()日tGetpos(*personsi)endl; break; case 3: coutn; for(i=0;i=num-1;i+) if(n=Getno(*personsi) coutntt=endl; couttt1:名字修改 2:职位修改endl; couttt=endl; cout选择您要进行的修改方式:choose1; switch(choose1) case 1: coutstr;n=Getno(*personsi); s=Getsex(*personsi); y=Getbirth(*personsi).GetYear(); m=Getbirth(*personsi).GetMonth(); d=Getbirth(*personsi).GetDay(); p=Getpos(*personsi); delete personsi; personsi= new employee(n,str,s,Date(y,m,d),p); break; case 2: coutp;n=Getno(*personsi); str=Getname(*personsi); s=Getsex(*personsi); y=Getbirth(*personsi).GetYear(); m=Getbirth(*personsi).GetMonth(); d=Getbirth(*personsi).GetDay();delete personsi; personsi=new employee(n,str,s,Date(y,m,d),p); break; break; case 4: coutntt=endl; couttt1:按编号 2:按姓名 endl; couttt=endl; coutn; switch(n) case 1: coutn; for(i=0;i=num;i+) if(n=Getno(*personsi) cout编号t姓名t性别t生日tt职务endl; coutGetno(*personsi)tGetname(*personsi)tGetsex(*personsi)tGetbirth(*personsi).GetYear() 年Getbirth(*personsi).GetMonth()月Getbirth(*personsi).GetDay()日tGetpos(*personsi)endl; break; if(i=num) cout该员工不存在 endl; break; case 2: coutstr; for(i=0;i=num;i+) if(str=Getname(*personsi) cout编号t姓名t性别t生日tt职务endl; coutGetno(*personsi)tGetname(*personsi)tGetsex(*personsi)tGetbirth(*personsi).GetYear() 年Getbirth(*personsi).GetMonth()月Getbirth(*personsi).GetDay()日tGetpos(*personsi)endl; break; if(i=num) cout该员工不存在 choose; return 0;实验三 C+中的运算符重载一、实验目的:1. 掌握C+中运算符重载的机制和运算符重载的方式。2. 熟悉几种常见的运算符的重载方式。3. 掌握类型转换的方式。二、实验任务【题目】定义一个复数类,用real描述其实部,imag描述其虚部。使用友元函数,重载复数的“加、减、乘、除”四则运算,实现复数的四则运算。【提示】运算符重载为友元函数的声明如下:friend complex operator +(const complex &c1, const complex &c2);friend complex operator -(const complex &c1, const complex &c2);friend complex operator *(const complex &c1, const complex &c2);friend complex operator /(const complex &c1, const complex &c2); 源代码:#includeusing namespace std;class Complex private: double real; double imag; public: void set() ; friend Complex operator +(const Complex &c1, const Complex &c2); friend Complex operator -(const Complex &c1, const Complex &c2); friend Complex operator *(const Complex &c1, const Complex &c2); friend Complex operator /(const Complex &c1, const Complex &c2); friend ostream&operator (ostream&,Complex&); friend void display() cout =endl; cout +:加法 -: 减法 *: 乘法 /:除法 0:退出endl; cout =endl; coutrealimag; ostream&operator (ostream &output,Complex &c1) output(=0) output+; outputc1.imagi)choose; while(choose!=0) if(choose!=+&choose!=-&choose!=*&choose!=/) cout请再重新输入正确的运算符! endlendlendl; switch(choose) case +: cout输入第一复数: ; c1.set(); cout输入第二复数: ; c2.set(); c3=c1+c2; cout输出结果: ; coutc1+c2=c3endl; break; case - : cout输入第一复数: ; c1.set(); cout输入第二复数: ; c2.set(); c3=c1-c2; cout输出结果: ; coutc1-c2=c3endl; break; case * : cout输入第一复数: ; c1.set(); cout输入第二复数: ; c2.set(); cout输出结果: ; c3=c1*c2; coutc1*c2=c3endl; break; case / : cout输入第一复数: ; c1.set(); cout输入第二复数: ; c2.set(); c3=c1/c2; cout输出结果: ; coutc1/c2=c3choose; cout谢谢!endl;return 0;实验四 C+中的继承与派生一、实验目的:4. 理解C+中继承与派生的概念;5. 掌握C+中各种不同继承方式的访问属性差别;6. 掌握单继承与多继承的实现方法;7. 掌握派生类构造函数与析构函数的实现及调用顺序;8. 掌握虚基类的使用方法。二、实验任务【题目】小型公司人员管理某小型公司有四类人员:经理、技术人员、销售经理、销售员。设计一个基类employee,派生出manager(经理)、technician(技术人员)、salesmanager(销售经理)、saleman(销售员)。销售经理既是经理又是销售员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。1、类定义1)employee 类:基本信息:编号、姓名、性别、出生日期、职位、薪水等;出生日期使用自定义的 Date(日期)类;2)Date 类:成员变量:年、月、日3)派生类technician:新增属性:工作时间派生类 saleman: 新增属性:销售额、所属部门2、实现人员信息的录入与显示;3、计算并显示个人月薪:月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;推销员的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5 。【提示】1、 在基类中,除了定义构造函数和析构函数,还应统一定义对各类人员信息应有的操作,规范类族中各派生类的基本行为,但是各类人员的月薪计算方法不同,不能在基类employee 中统一确定计算方法。各类人员信息的显示内容不同,同样不能在基类employee中统一确定显示方法。在基类中实现上述功能的函数体应为空,在派生类中根据同名覆盖原则定义各自的同名函数实现具体功能。源代码:#include#includeusing namespace std;class Date private: int year; int month; int day; public: void SetYear(int x) year=x; void SetMonth(int x) month=x; void SetDay(int x) day=x; int GetYear() return year; int GetMonth() return month; int GetDay() return day;class employee protected: int num; string nam; string sex; Date birth; string position ; float salary; public: void display() cout编号:num姓名:nam性别:sex出生日期:birth. GetYear()-birth.GetMonth()-birth.GetDay()职位:position;class manager:virtual public employee public: void set_manager(int n,string na,string se,int y,int m,int d,string pos) num=n; nam=na; sex=se; birth.SetYear(y); birth.SetMonth(m); birth.SetDay(d); position=pos; void display() employee:display(); void total_sal
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年对口机械基础考试试题及答案
- 2025年华阳实验小学题库及答案
- 高一年级组长发言稿
- 保险分享演讲稿
- 临夏坡屋面防水施工方案
- 主变套管更换施工方案
- 科学七下第一章测试卷及答案
- 乐理考级一级模拟试题及答案
- 幼儿护理知识培训心得课件
- 青马工程笔试题目及答案
- 2025年《互联网销售》课程标准
- 4《公民的基本权利和义务》第一课时 公开课一等奖创新教案
- 家博会合同协议书
- 2025年中国高速双针链缝机市场调查研究报告
- 《植物组织培养》课件 项目3 无菌操作技术
- 2025届广东省广州市高三4月二模生物试题(原卷版+解析版)
- 装修装饰工程技术施工方案
- 《白银投资深度解析》课件
- 澳大利亚旅游
- 2024-2025学年浙江省宁波市鄞州区九年级(上)期末数学试卷(含答案)
- 发电机的工作原理
评论
0/150
提交评论