《面向对象程序设计基础R》实验指导和实验报告Word格式.docx
- 文档编号:19812363
- 上传时间:2023-01-10
- 格式:DOCX
- 页数:24
- 大小:190.81KB
《面向对象程序设计基础R》实验指导和实验报告Word格式.docx
《《面向对象程序设计基础R》实验指导和实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《《面向对象程序设计基础R》实验指导和实验报告Word格式.docx(24页珍藏版)》请在冰豆网上搜索。
constdoublepi=3.141592;
doublearea(doubler);
doublearea(doublea,doubleb);
doubleperimer(doubler);
doubleperimer(doublea,doubleb);
doublearea(doubler)
{
doubles;
s=pi*r*r;
returns;
}
doublearea(doublea,doubleb)
s=a*b;
doubleperimer(doubler)
doublep;
p=2*pi*r;
returnp;
doubleperimer(doublea,doubleb)
p=2*(a=b);
int_tmain(intargc,_TCHAR*argv[])
doubler;
doublem;
doublen;
cout<
<
"
请输入圆的半径:
endl;
cin>
>
r;
圆的面积为:
area(r)<
"
圆的周长为:
perimer(r)<
请输入长方形的长和宽:
m>
n;
长方形面积为:
area(m,n)<
长方形的周长为:
perimer(m,n)<
请输入正方形的边长:
m;
正方形的面积为:
area(m,m)<
正方形的周长为:
perimer(m,m)<
getchar();
getchar();
return0;
//实验1-2.cpp:
定义控制台应用程序的入口点。
//
#include"
constdoublepi=3.14;
classCircle{
public:
doublearea(){returnx*x*pi;
}
Circle(doublea):
x(a){}
private:
doublex;
};
classRectangle{
Rectangle(doublex,doubley):
len(x),wid(y){}//写出矩形类的构造函数
doublearea(){returnlen*wid;
}//写出计算矩形面积函数
doublelen,wid;
classSquare{
Square(doublex):
len(x){}//写出正方形类的构造函数
doublearea(){returnlen*len;
}//写出计算正方形面积函数
doublelen;
intmain()
cout<
Inputshape"
<
endl;
ifcircle,inputc,ifrectangleinputr;
ifsquareinputs"
charshape;
cin>
shape;
switch(shape)
{
case'
c'
:
{doubler;
cout<
inputradius"
cin>
r;
Circler1(r);
circlearea="
r1.area()<
system("
pause"
);
break;
r'
{doublelen,wid;
inputlengthandwidth"
len>
wid;
Rectanglejx(len,wid);
//构造矩形对象,宽为wid,高为len
rectanglearea="
jx.area()<
//输出矩形面积
s'
{doublelen;
inputlength"
len;
Squarefx(len);
//构造正方形对象,边长为len
squarearea="
fx.area()<
//输出正方形面积
default:
inputerror!
3.#include"
classComplex
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()
("
real<
+"
image<
i"
)"
Complexc1(20,40);
Complexc2(0,0);
Complexc3(c1);
c1.Dispaly();
c2.Dispaly();
c3.Dispaly();
system("
4.#include"
math.h"
classRectangle{
protected:
doubleLeft,Top,Right,Bottom;
Rectangle(doublea,doubleb,doublec,doubled):
Left(a),Top(b),Right(c),Bottom(d){};
voidShow(Rectangle&
d);
doubleDiagonal(Rectangle&
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){
左上角"
Left<
"
Top<
右下角"
Right<
Bottom<
斜角线长度¨
¨
d.Diagonal(d)<
intmain(){
Rectangle*r1=newRectangle(10,10,20,20);
r1->
Show(*r1);
deleter1;
遇到的问题和解决方法
心得体会
实验二类与对象的特性
(1)掌握类的定义和对象的定义和使用;
(2)掌握静态数据成员和静态成员函数的定义和使用方法;
(3)理解类的作用域、对象的作用域及生存周期;
(4)掌握函数调用中参数的传递;
(5)掌握常量类型;
(6)掌握友元函数和友元类的定义及使用。
编写一个学生类。
(1)输出每个学生的姓名、学号、成绩;
(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。
实验记录(写出实验内容中程序代码和运行结果)(可分栏或加页)
#include<
iostream>
//定义学生类
classStudent{
intno;
//学号
charname[10];
//姓名
doublescore;
//成绩
staticinttotalNumber;
//学生人数
staticdoubletotalScore;
//总成绩
staticdoublelowestScore;
//最低成绩
staticdoublehighestScore;
//最高成绩
Student(intno_,char*name_,doublescore_);
//构造函数
staticvoidOutput();
//输出相关信息
voidStudentInformation();
//输出学生基本信息
intStudent:
totalNumber=0;
//静态数据初始化
doubleStudent:
highestScore=0.;
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();
Student:
Student(intno_,char*name_,doublescore_)
{no=no_;
strcpy(name,name_);
score=score_;
totalNumber++;
totalScore+=score;
if(score>
highestScore)
highestScore=score;
if(score<
lowestScore)
lowestScore=score;
voidStudent:
StudentInformation()
{cout<
学号"
no<
姓名"
name<
成绩:
score<
Output()
学总数"
totalNumber<
总成绩"
totalScore<
平均成绩"
totalScore/totalNumber<
最高成绩"
highestScore<
最低成绩"
lowestScore<
实验三继承与派生
(1)掌握类的继承和派生概念;
(2)掌握派生类的定义与使用;
(3)掌握派生类的构造函数与析构函数的应用及调用顺序;
(4)理解赋值兼容原则的应用。
考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。
建立3个类,分别为点类、圆类、圆柱类,点类派生得到圆类,圆类派生得到圆柱类。
实验记录(写出实验内容中的程序代码和运行结果)(可分栏或加页)
#include<
iomanip>
classPoint{
friendostream&
operator<
(ostream&
constPoint&
Point(int=0,int=0);
//带默认参数的构造函数
voidsetPoint(int,int);
//对点坐标数据赋值
intgetX()const{returnx;
intgetY()const{returny;
intx,y;
//Point类的数据成员
classCircle:
publicPoint{
constCircle&
//友元函数
Circle(doubler=0.0,intx=0,inty=0);
//构造函数
voidsetRadius(double);
//置半径
doublegetRadius()const;
//返回半径
doublearea()const;
//返回面积
doubleradius;
//数据成员,半径
classCylinder:
publicCircle{
operator<
(ostream&
constCylinder&
Cylinder(doubleh=0.0,doubler=0.0,intx=0,inty=0);
voidsetHeight(double);
//置高度值
doublegetHeight()const;
//返回高度值
//返回面积
doublevolume()const;
//返回体积
doubleheight;
//数据成员,高度
Point:
Point(inta,intb)//构造函数,调用成员函数对x,y作初始化
setPoint(a,b);
voidPoint:
setPoint(inta,intb){x=a;
y=b;
}//对数据成员置值
ostream&
output,constPoint&
p)//重载插入运算符,输出对象数据
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;
}//返回半径值
area()const{return3.14159*radius*radius;
}//计算并返回面积值
output,constCircle&
c)//输出圆心坐标和半径值
Center="
c.x<
c.y<
Radius="
<
setiosflags(ios:
fixed|ios:
showpoint)<
setprecision
(2)<
c.radius;
//带初始化式构造函数,首先调用基类构造函数
Cylinder:
Cylinder(doubleh,doubler,intx,inty):
Circle(r,x,y){setHeight(h);
voidCylinder:
setHeight(doubleh){height=(h>
h:
}//对高度置值
doubleCylinder:
getHeight()const{returnheight;
}//返回高度值
area()const{return2*Circle:
area()+2*3.14159*radius*height;
}//计算并返回圆柱体的表面积
volume()const{returnCircle:
area()*height;
}//计算并返回圆柱体的体积
//输出数据成员圆心坐标、半径和高度值
output,constCylinder&
cy)
cy.x<
cy.y<
cy.radius
Height="
cy.height<
Pointp(72,115);
//定义点对象并初始化
Theinitiallocationofpis"
p<
p.setPoint(10,10);
//置点的新数据值
\nThenewlocationofpis"
//输出数据
Circlec(2.5,37,43);
//定义圆对象并初始化
\nTheinitiallocationandradiusofcare\n"
c<
\nArea="
c.area()<
\n"
c.setRadius(4.25);
c.setPoint(2,2);
//置圆的新数据值
//输出圆心坐标和圆面积
\nThenewlocationandradiusofcare\n"
Cylindercyl(5.7,2.5,12,23);
//定义圆柱体对象并初始化
//输出圆柱体各数据和表面积,体积
\nTheinitiallocation,radiusandheightofcylare\n"
cyl
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序设计基础R 面向 对象 程序设计 基础 实验 指导 报告