《面向对象程序设计基础R》实验指导和实验报告.docx
- 文档编号:10304520
- 上传时间:2023-02-10
- 格式:DOCX
- 页数:24
- 大小:190.60KB
《面向对象程序设计基础R》实验指导和实验报告.docx
《《面向对象程序设计基础R》实验指导和实验报告.docx》由会员分享,可在线阅读,更多相关《《面向对象程序设计基础R》实验指导和实验报告.docx(24页珍藏版)》请在冰豆网上搜索。
《面向对象程序设计基础R》实验指导和实验报告
《面向对象程序设计基础R》实验指导和实验报告(2017年)
实验报告☑实践报告□
课程名称:
面向对象程序设计基础R
实验、实践名称:
面向对象程序设计基础R
实验、实践地点:
逸夫楼201
专业班级:
软件1601学号:
2016005
学生姓名:
指导教师:
宋春花
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.#include"stdafx.h"
#include"iostream"
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<<"请输入圆的半径:
"< cin>>r; cout<<"圆的面积为: "< "< cout<<"请输入长方形的长和宽: "< cin>>m>>n; cout<<"长方形面积为: "< "< cout<<"请输入正方形的边长: "< cin>>m; cout<<"正方形的面积为: "< "< getchar();getchar(); return0; } //实验1-2.cpp: 定义控制台应用程序的入口点。 // #include"stdafx.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() { cout<<"Inputshape"< cout<<"ifcircle,inputc,ifrectangleinputr;ifsquareinputs"< charshape; cin>>shape; switch(shape) { case'c': {doubler; cout<<"inputradius"< cin>>r; Circler1(r); cout<<"circlearea="< system("pause"); break; } case'r': {doublelen,wid; cout<<"inputlengthandwidth"< cin>>len>>wid; Rectanglejx(len,wid);//构造矩形对象,宽为wid,高为len cout<<"rectanglearea="< system("pause"); break; } case's': {doublelen; cout<<"inputlength"< cin>>len; Squarefx(len);//构造正方形对象,边长为len cout<<"squarearea="< system("pause"); break; } default: cout<<"inputerror! "< break; } return0; } 3.#include"stdafx.h" #include"iostream" usingnamespacestd; classComplex { public: Complex(doublea,doubleb): real(a),image(b){}; Complex(Complex&c); voidDispaly(); private: doublereal; doubleimage; }; Complex: : Complex(Complex&c) { real=c.real; image=c.image; } voidComplex: : Dispaly() { cout<<"("< } intmain() { Complexc1(20,40); Complexc2(0,0); Complexc3(c1); c1.Dispaly(); c2.Dispaly(); c3.Dispaly(); system("pause"); } 4.#include"stdafx.h" #include"iostream" #include"math.h" usingnamespacestd; classRectangle{ protected: doubleLeft,Top,Right,Bottom; public: Rectangle(doublea,doubleb,doublec,doubled): Left(a),Top(b),Right(c),Bottom(d){}; voidShow(Rectangle&d); doubleDiagonal(Rectangle&c); }; doubleRectangle: : Diagonal(Rectangle&c){ doublex=c.Right-c.Left; doubley=c.Top-c.Bottom; doubled=sqrt((x*x+y*y)); returnd; } voidRectangle: : Show(Rectangle&d){ cout<<"左上角"<<"("< cout<<"右下角"<<"("< cout<<"斜角线长度¨¨"< } intmain(){ Rectangle*r1=newRectangle(10,10,20,20); r1->Show(*r1); deleter1; system("pause"); return0; } 遇到的问题和解决方法 心得体会 实验名称 实验二类与对象的特性 实验目的和要求 (1)掌握类的定义和对象的定义和使用; (2)掌握静态数据成员和静态成员函数的定义和使用方法; (3)理解类的作用域、对象的作用域及生存周期; (4)掌握函数调用中参数的传递; (5)掌握常量类型; (6)掌握友元函数和友元类的定义及使用。 实验内容 编写一个学生类。 (1)输出每个学生的姓名、学号、成绩; (2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。 主要仪器设备 台式或笔记本电脑: 1台/人 实验记录(写出实验内容中程序代码和运行结果)(可分栏或加页) #include"stdafx.h" #include 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(score lowestScore=score; } voidStudent: : StudentInformation() {cout<<"学号"< "< } voidStudent: : Output() {cout<<"学总数"< cout<<"平均成绩"< } 遇到的问题和解决方法 心得体会 实验名称 实验三继承与派生 实验目的和要求 (1)掌握类的继承和派生概念; (2)掌握派生类的定义与使用; (3)掌握派生类的构造函数与析构函数的应用及调用顺序; (4)理解赋值兼容原则的应用。 实验内容 考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。 建立3个类,分别为点类、圆类、圆柱类,点类派生得到圆类,圆类派生得到圆柱类。 主要仪器设备 台式或笔记本电脑: 1台/人 实验记录(写出实验内容中的程序代码和运行结果)(可分栏或加页) #include"stdafx.h" #include #include 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{ friendostream&operator<<(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;}//对数据成员置值 ostream&operator<<(ostream&output,constPoint&p)//重载插入运算符,输出对象数据 { output<<'['< 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;}//计算并返回面积值 ostream&operator<<(ostream&output,constCircle&c)//输出圆心坐标和半径值 { output<<"Center="<<'['< < : fixed|ios: : showpoint)< (2)< 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{return2*Circle: : area()+2*3.14159*radius*height;}//计算并返回圆柱体的表面积 doubleCylinder: : volume()const{returnCircle: : area()*height;}//计算并返回圆柱体的体积 //输出数据成员圆心坐标、半径和高度值 ostream&operator<<(ostream&output,constCylinder&cy) { output<<"Center="<<'['< < : fixed|ios: : showpoint)< (2)< <<";Height="< returnoutput; } #include #include usingnamespacestd; intmain() { Pointp(72,115);//定义点对象并初始化 cout<<"Theinitiallocationofpis"< p.setPoint(10,10);//置点的新数据值 cout<<"\nThenewlocationofpis"< Circlec(2.5,37,43);//定义圆对象并初始化 cout<<"\nTheinitiallocationandradiusofcare\n"< c.setRadius(4.25);c.setPoint(2,2);//置圆的新数据值 //输出圆心坐标和圆面积 cout<<"\nThenewlocationandradiusofcare\n"< Cylindercyl(5.7,2.5,12,23);//定义圆柱体对象并初始化 //输出圆柱体各数据和表面积,体积 cout<<"\nTheinitiallocatio
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序设计基础R 面向 对象 程序设计 基础 实验 指导 报告