




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文格式为Word版,下载可任意编辑——面向对象程序设计基础r实验指导和实验报告年《面向对象程序设计基础R》试验指导和试验报告(年)
————————————————————————————————:
————————————————————————————————日期:
试验报告?实践报告□
课程名称:面向对象程序设计基础R
试验、实践名称:面向对象程序设计基础R
试验、实践地点:逸夫楼201
专业班级:软件1601学号:
学生姓名:
指导教师:宋春花
2017年4月17
日
试验名称
试验一熟悉VisualStudio开发环境
试验目的和要求
(1)熟悉基本的输入输出方法;
(2)把握函数的定义,调用和声明方法,函数的参数传递机制,函数嵌套调用和递归调用,内联函数,带默认形参的函数,重载函数;
(3)理解命名空间的概念,把握命名空间的使用;
(4)熟悉const关键字的使用;
(5)把握内存的动态分派的概念和使用;
(5)把握类的定义和对象的定义和使用;
试验内容
1.编写重载函数area()和perimeter(),分别计算圆、长方形、正方形的面积和周长,并在主函数中测试之。
2.完善程序,并上机运行:(此程序见原模板)
3.定义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3,用默认构造函数将c1初始化为c1=20+40i,将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。用公有成员函数Display()显示复数c1、c2与c3的内容。
4.定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最终用delete运算符回收为矩形动态分派的存储空间。
主要仪器设备
台式或笔记本电脑:1台/人
试验记录(写出试验内容中1、2、3、4的程序代码和运行结果)(可分栏或加页)
1.#includestdafx.h"
#includeiostream
usingnamespacestd;
constdoublepi=3.141592;
doublearea(doubler);
doublearea(doublea,doubleb);
doubleperimer(doubler);
doubleperimer(doublea,doubleb);
doublearea(doubler)
{
?doubles;
?s=pi*r*r;
?returns;
}
doublearea(doublea,doubleb)
{
?doubles;
s=a*b;
returns;
}
doubleperimer(doubler)
{
?doublep;
?p=2*pi*r;
returnp;
}
doubleperimer(doublea,doubleb)
{
doublep;
p=2*(a=b);
returnp;
}
int_tmain(intargc,_TCHAR*argv[])
{
doubler;
?doublem;
doublen;
cout<请输入圆的半径:<endl;
?cinr;
?cout<圆的面积为:area(r)<圆的周长为:<<perimer(r)<endl;
?cout<"请输入长方形的长和宽:<endl;
cinm>n;
?cout长方形面积为:<area(m,n)"""长方形的周长为:<<perimer(m,n)endl;
?cout请输入正方形的边长:endl;
cinm;
cout<"正方形的面积为:<area(m,m)<正方形的周长为:"perimer(m,m)endl;
?getchar();getchar();
return0;
}
//试验1-2.cpp:定义操纵台应用程序的入口点。
//
#includestdafx.h
#include"iostream
usingnamespacestd;
constdoublepi=3.14;
classCircle{
public:
doublearea(){returnx*x*pi;}
Circle(doublea):x(a){}
private:doublex;
};
classRectangle{
public:
Rectangle(doublex,doubley):len(x),wid(y){}//写出矩形类的构造函数
?doublearea(){returnlen*wid;}//写出计算矩形面积函数
private:
doublelen,wid;
};
classSquare{
public:
?Square(doublex):len(x){}//写出正方形类的构造函数
?doublearea(){returnlen*len;}//写出计算正方形面积函数
private:
?doublelen;
};
intmain()
{
?coutInputshape<endl;
?coutifcircle,inputc,ifrectangleinputr;ifsquareinputsendl;
?charshape;
?cinshape;
?switch(shape)
{
casec:{doubler;
?coutinputradiusendl;
?cinr;
?Circler1(r);
?coutcirclearea=r1.area()<endl;
system(pause);
?break;
}
?caser:{doublelen,wid;
cout"inputlengthandwidthendl;
?cin>lenwid;
Rectanglejx(len,wid);//构造矩形对象,宽为wid,高为len
?cout"rectanglearea=jx.area()endl;//输出矩形面积
system(pause);
break;
}
?cases:{doublelen;
?cout"inputlength"endl;
cinlen;
?Squarefx(len);//构造正方形对象,边长为len
cout<squarearea=<fx.area()endl;//输出正方形面积
?system(pause);
break;
}
?default:cout<inputerror!"<endl;
break;
?}
?return0;
}
3.#includestdafx.h
#includeiostream
usingnamespacestd;
classComplex
{
public:
Complex(doublea,doubleb):real(a),image(b){};
?Complex(Complexc);
?voidDispaly();
private:
doublereal;
?doubleimage;
};
Complex::Complex(Complexc)
{
real=c.real;
?image=c.image;
}
voidComplex::Dispaly()
{
cout"(<<real+<image<i<)<endl;
}
intmain()
{
?Complexc1(20,40);
Complexc2(0,0);
Complexc3(c1);
c1.Dispaly();
c2.Dispaly();
c3.Dispaly();
?system("pause);
}
4.#includestdafx.h"
#includeiostream
#includemath.h
usingnamespacestd;
classRectangle{
protected:
doubleLeft,Top,Right,Bottom;
public:
Rectangle(doublea,doubleb,doublec,doubled):Left(a),Top(b),Right(c),Bottom(d){};
voidShow(Rectangled);
?doubleDiagonal(Rectanglec);
};
doubleRectangle::Diagonal(Rectanglec){
?doublex=c.Right-c.Left;
?doubley=c.Top-c.Bottom;
?doubled=sqrt((x*x+y*y));
?returnd;
}
voidRectangle::Show(Rectangle&d){
cout<<左上角"("Left,"Top)endl;
cout<右下角("<Right<,<Bottom)<endl;
cout斜角线长度¨¨<d.Diagonal(d)endl;
}
intmain(){
Rectangle*r1=newRectangle(10,10,20,20);
r1-Show(*r1);
deleter1;
system(pause);
return0;
}
遇到的问题和解决方法
心得体会
?
试验名称
试验二类与对象的特性
试验目的和要求
(1)把握类的定义和对象的定义和使用;
(2)把握静态数据成员和静态成员函数的定义和使用方法;
(3)理解类的作用域、对象的作用域及生存周期;
(4)把握函数调用中参数的传递;
(5)把握常量类型;
(6)把握友元函数和友元类的定义及使用。
试验内容
编写一个学生类。(1)输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。
主要仪器设备
台式或笔记本电脑:1台/人
试验记录(写出试验内容中程序代码和运行结果)(可分栏或加页)
#includestdafx.h
#includeiostream
usingnamespacestd;
//定义学生类
classStudent{
intno;
//学号
charname[10];//姓名
?doublescore;//成绩
?staticinttotalNumber;//学生人数
?staticdoubletotalScore;//总成绩
staticdoublelowestScore;//最低成绩
staticdoublehighestScore;//最高成绩
public:
Student(intno_,char*name_,doublescore_);//构造函数
?staticvoidOutput();//输出相关信息
voidStudentInformation();//输出学生基本信息
};
intStudent::totalNumber=0;//静态数据初始化
doubleStudent::highestScore=0.;
doubleStudent::lowestScore=100.;
intmain()
{Studentstu1(1001,"张小三¨y,97.5);
?stu1.StudentInformation();
?Studentstu2(1625,李老四,83.);
stu2.StudentInformation();
Studentstu3(1628,王老五",93.);
?stu3.StudentInformation();
Studentstu4(1352,郭小六¨′,62.5);
stu4.StudentInformation();
?Studentstu5(1999,王小明,77.);
?stu5.StudentInformation();
?Student::Output();
?system(pause);
return0;
}
Student::Student(intno_,char*name_,doublescore_)
{no=no_;
?strcpy(name,name_);
?score=score_;
totalNumber++;
?totalScore+=score;
?if(score>highestScore)
?highestScore=score;
?if(scorelowestScore)
?lowestScore=score;
}
voidStudent::StudentInformation()
{cout<学号"<no姓名"<name成绩:scoreendl;
}
voidStudent::Output()
{?cout<<学总数<totalNumber"总
成绩totalScore
endl;
cout平均成绩<totalScore/totalNumber<"最高成绩<highestScore
"<最低成绩<lowestScore"
endl;
}
遇到的问题和解决方法
心得体会
试验名称
试验三继承与派生
试验目的和要求
(1)把握类的继承和派生概念;
(2)把握派生类的定义与使用;
(3)把握派生类的构造函数与析构函数的应用及调用顺序;
(4)理解赋值兼容原则的应用。
试验内容
考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。
建立3个类,分别为点类、圆类、圆柱类,点类派生得到圆类,圆类派生得到圆柱类。
主要仪器设备
台式或笔记本电脑:1台/人
试验记录(写出试验内容中的程序代码和运行结果)(可分栏或加页)
#include"stdafx.h
#include<iostream>
#include<iomanip
usingnamespacestd;
classPoint{
friendostream&operator(ostream&,constPoint&);
public:
?Point(int=0,int=0);?//带默认参数的构造函数
voidsetPoint(int,int);?//对点坐标数据赋值
intgetX()const{returnx;}
?intgetY()const{returny;}
protected:
intx,y;
?//Point类的数据成员
};
classCircle:publicPoint{
friendostream&operator(ostream,constCircle);?//友元函数
public:
?Circle(doubler=0.0,intx=0,inty=0);//构造函数
?voidsetRadius(double);//置半径
?doublegetRadius()const;//返回半径
?doublearea()const;//返回面积
protected:doubleradius;?//数据成员,半径
};
classCylinder:publicCircle{
friendostreamoperator<<(ostream,constCylinder);
//友元函数
public:
Cylinder(doubleh=0.0,doubler=0.0,intx=0,inty=0);//构造函数
?voidsetHeight(double);//置高度值
?doublegetHeight()const;//返回高度值
?doublearea()const;?//返回面积
?doublevolume()const;?//返回体积
protected:
?doubleheight;//数据成员,高度
};
Point::Point(inta,intb)//构造函数,调用成员函数对x,y作初始化
{
setPoint(a,b);
}
voidPoint::setPoint(inta,intb){x=a;y=b;}//对数据成员置值
ostreamoperator<(ostream&output,constPointp)//重载插入运算符,输出对象数据
{
?output[<p.x<,<p.y];
returnoutput;
}
//带初始化式构造函数,首先调用基类构造函数
Circle::Circle(doubler,inta,intb):Point(a,b){setRadius(r);}
voidCircle::setRadius(doubler){radius=(r=0?r:0);}//对半径置值
doubleCircle::getRadius()const{returnradius;}//返回半径值
doubleCircle::area()const{return3.14159*radius*radius;}//计算并返回面积值
ostreamoperator(ostreamoutput,constCirclec)//输出圆心坐标和半径值
{
?outputCenter="[<<c.x,<c.y]"<;Radius="
setiosflags(ios::fixed|ios::showpoint)<setprecision(2)c.radius;
returnoutput;
}
//带初始化式构造函数,首先调用基类构造函数
Cylinder::Cylinder(doubleh,doubler,intx,inty):Circle(r,x,y){setHeight(h);}
voidCylinder::setHeight(doubleh){height=(h>=0?h:0);}//对高度置值
doubleCylinder::getHeight()const{returnheight;}//返回高度值
doubleCylinder::area()const{return
2*Circle::area()+2*3.14159*radius*height;}//计算并返回圆柱体的表面积
doubleCylinder::volume()const{returnCircle::area()*height;}//计算并返回圆柱体的体积
??//输出数据成员圆心坐标、半径和高度值
ostreamoperator(ostreamoutput,constCylindercy)
{
?outputCenter=<[<cy.x<,cy.y]<<;Radius="
setiosflags(ios::fixed|ios::showpoint)<setprecision(2)cy.radius
?<;Height=cy.heightendl;
returnoutput;
}
#includeiostream
#includeiomanip
usingnamespacestd;
intmain()
{
?Pointp(72,115);?//定义点对象并初始化
?cout<"Theinitiallocationofpis"p<endl;
p.setPoint(10,10);//置点的新数据值
?cout<"\nThenewlocationofpisp<endl;?//输出数据
Circlec(2.5,37,43);//定义圆对象并初始化
cout<\nTheinitiallocationandradiusofcare\n<c"\nArea=c.area()<\n;
?c.setRadius(4.25);c.setPoint(2,2);//置圆的新数据值
//输出圆心坐标和圆面积
?cout\nThenewlocationandradiusofcare\nc"\nArea=c.area()\n;
Cylindercyl(5.7,2.5,12,23);?//定义圆柱体对象并初始化
?//输出圆柱体各数据和表面积,体积
cout<"\nTheinitiallocation,radiusandheightofcylare\n<cyl
?Area="cyl.area()"\nVolume=cyl.volume()\n;
?//置圆柱体的新数据值
cyl.setHeight(10);cyl.setRadius(4.25);
cyl.setPoint(2,2);
?cout"\nThenewlocation,radiusandheightofcylare\n<<cyl
?<Area=<cyl.area()\nVolume=cyl.volume()\n";
system(pause);
}
遇到的问题和解决方法
心得体会
?
试验名称
试验四多态性
试验目的和要求
(1)把握C++中运算符重载的机制和运算符重载的方式;
(2)理解类型转换的必要性,把握类型转换的使用方法;
(3)理解多态性,把握虚函数的设计方法;
(4)把握纯虚函数和抽象类的使用方法。
试验内容
某小型公司,主要有三类人员:管理人员、计时人员和计件人员。现在,需要存储这些人的姓名、编号、时薪、工时、每件工件薪金、工件数,计算月薪并显示全部信息。
设计4个类,分别为雇员类(为抽象类)、管理人员类、计时人员类和计件人员类,其中管理人员类、计时人员类和计件人员类由雇员类派生得到。
主要仪器设备
台式或笔记本计算机:1台/人
试验记录(写出试验内容中的程序代码和运行结果)(可分栏或加页)
#include"stdafx.h
#includeiostream"
#includeiomanip
#includestring.h
usingnamespacestd;
//Employee.h
classEmployee
{
//雇员类—-抽象类
public:
?Employee(int,constchar*name_);
?virtual~Employee();?//虚析构函数
stringgetName();
longgetNumber();
?virtualdoubleearnings(double);?//纯虚函数,计算月薪
virtualvoidprint();
//虚函数,输出编号、姓名
protected:
?longnumber;//编号
stringname;?//姓名
};
//Manager.h
classManager:publicEmployee{//管理人员类
public:
?constManager(int,constchar*name_,double);
?~Manager(){}
?voidsetMonthlySalary(double);//置月薪
?virtualdoubleearnings();//计算管理人员月薪
virtualvoidprint();//输出管理人员信息
private:
doublemonthlySalary;?//私有数据,月薪
};
//HourlyWorker.h
classHourlyWorker:publicEmployee{//计时人员类
public:
?HourlyWorker(int,constchar*name_,double,int);
~HourlyWorker(){}
?voidsetWage(double);?//置时薪
?voidsetHours(int);
//置工时
?virtualdoubleearnings();//计算计时工月薪
?virtualvoidprint();?
//输出计时工月薪
private:
doublewage;//时薪
?doublehours;//工时
};
//PieceWorker.h
classPieceWorker:publicEmployee{//计件人员类
public:
PieceWorker(int,constchar*name_,double,int);
~PieceWorker(){}
?voidsetWage(double);//置每件工件薪金
?voidsetQuantity(int);//置工件数
virtualdoubleearnings();//计算计件薪金
?virtualvoidprint();//输出计件薪金
private:
?doublewagePerPiece;?//每件工件薪金
intquantity;//工件数
};
intmain()
{
cout<setiosflags(ios::fixed|ios::showpoint)<setprecision(2);
?Managerm1(10135,ChengShaoHua,1200);
?HourlyWorkerhw1(30712,ZhaoXiaoMing,5,8*20);
?PieceWorkerpw1(20382,XiuLiWei,0.5,2850);
//使用抽象类指针,调用派生类版本的函数
?Employee*basePtr;
basePtr=m1;basePtr-print();
basePtr=hw1;basePtr-print();
?basePtr=pw1;basePtr->print();
?system("pause");
}
Employee::Employee(intnu_,constchar*name_)
{
?number=nu_;
name=nam
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 证劵交易平台使用手册
- 农药与肥料使用指导作业指导书
- 保育师初级练习测试卷
- 母婴护理员初级练习测试题附答案
- 仓库管理工作计划模板
- 工作效率提升方案报告
- 地理人教版2024版七年级初一上册1.1宇宙中的地球教案02
- 技术方案选型表-技术方案选择
- 新一代办公软件使用手册
- 调研报告之行业市场现状分析
- 2025年长春医学高等专科学校单招职业技能测试题库及完整答案1套
- 2025年中国大唐集团有限公司重庆分公司高校毕业生招聘笔试参考题库附带答案详解
- 2025年西安铁路职业技术学院高职单招高职单招英语2016-2024历年频考点试题含答案解析
- 2024年心理咨询师题库附参考答案(达标题)
- 运输公司安全生产管理制度
- GB 11984-2024化工企业氯气安全技术规范
- 《信息论绪论》课件
- GA/T 2149-2024机动车驾驶人安全教育网络课程设置规范
- 企业环保知识培训课件
- Unit 3 Food and Culture Using Language 课件英语人教版(2019)选择性必修第二册
- 卵巢囊肿中医治疗
评论
0/150
提交评论