第6次作业Word格式.docx

上传人:b****5 文档编号:21529296 上传时间:2023-01-31 格式:DOCX 页数:17 大小:19.69KB
下载 相关 举报
第6次作业Word格式.docx_第1页
第1页 / 共17页
第6次作业Word格式.docx_第2页
第2页 / 共17页
第6次作业Word格式.docx_第3页
第3页 / 共17页
第6次作业Word格式.docx_第4页
第4页 / 共17页
第6次作业Word格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

第6次作业Word格式.docx

《第6次作业Word格式.docx》由会员分享,可在线阅读,更多相关《第6次作业Word格式.docx(17页珍藏版)》请在冰豆网上搜索。

第6次作业Word格式.docx

Circle(doubler);

~Circle();

doublegetArea();

doublegetPerimeter();

doublegetRadius();

voidsetRadius(double);

};

//以下为Circle类的实现部分:

circle.cpp

#include"

circle.h"

#include<

iostream.h>

Circle:

:

Circle()

radius=1;

cout<

<

"

no-argconstructoriscalled:

radius<

endl;

}

Circle(doubler)

radius=r;

1-argconstructoriscalled:

~Circle()

destructoriscalled:

"

doubleCircle:

getArea()

return3.14*radius*radius;

getPerimeter()

return2*3.14*radius;

getRadius()

returnradius;

voidCircle:

setRadius(doubler)

//测试Circle类以下为test.cpp

#include"

voidmain()

Circlea,b

(2);

//创建两个对象,对象名分别为a,b

radius="

a.getRadius()<

//输出b对象的半径

area="

a.getArea()<

//输出a对象的面积

perimeter="

a.getPerimeter()<

//输出a对象的周长

b.getRadius()<

b.getArea()<

;

//输出b对象的面积

b.getPerimeter()<

//输出b对象的周长

2.编写矩形类Rectangle,包含:

两个名为width和height的double型数据成员,分别表示矩形的宽和高。

无参构造函数,宽与高的值为1。

有1个参数的构造函数,高的值为1。

有2个参数的构造函数。

析构函数,输出”destructoriscalled”及宽、高的值。

单个数据成员的获取函数和设置函数以及对两个数据成员的同时获取的函数和同时设置的函数,各函数名为:

getWidth,getHeight,setWidth,setHeight,setWH,getWH。

一个名为getArea()的函数,返回矩形的面积。

一个名为getPerimeter()的函数,返回矩形的周长。

编写一个测试程序,它创建2个Rectangle对象,将第一个矩形的宽/高设置为4、5,第2个矩形的宽/高设置为3、2,并输出两个矩形对象的高、宽、面积和周长。

//以下为Rectangle类的声明部分,rectangle.h

classRectangle

doublewidth,height;

Rectangle();

Rectangle(double);

Rectangle(double,double);

~Rectangle();

doublegetWidth();

doublegetHeight();

voidgetWH(double&

double&

);

voidsetWidth(double);

voidsetHeight(double);

voidsetWH(double,double);

//以下为Rectangle类的实现部分,rectangle.cpp

rectangle.h"

Rectangle:

Rectangle()

width=1;

height=1;

no-argconstructoriscalled:

width<

"

height<

Rectangle(doublew)

width=w;

Rectangle(doublew,doubleh)

height=h;

2-argconstructoriscalled:

~Rectangle()

doubleRectangle:

returnwidth*height;

return2*(width+height);

getWidth()

returnwidth;

getHeight()

returnheight;

voidRectangle:

setWidth(doublew)

width=w;

setHeight(doubleh)

setWH(doublew,doubleh)

getWH(double&

w,double&

h)

w=width;

h=height;

//以下为测试Rectangle类,test.cpp

Rectanglea;

//创建a对象

doublew,h;

a.getWH(w,h);

//获得a的高与宽

width="

w<

height="

h<

a.getArea()<

a.getPerimeter()<

Rectangleb(10);

//创建b对象

b.getWidth()<

b.getHeight()<

b.getArea()<

b.getPerimeter()<

Rectanglec(20,3);

c.getWidth()<

c.getHeight()<

c.getArea()<

c.getPerimeter()<

3.编写圆柱体类Cylinder,包含:

名为radius的double型数据成员,表示圆的半径,名为height的double型数据成员,表示高。

无参构造函数,高与半径的值均为1。

单个数据成员的访问和更改函数,以及两个成员的访问和更改函数。

一个名为getSurfaceArea的函数,返回表面积;

一个名为getVolume的函数,返回体积。

编写一个测试程序,它创建1个Cylinder类对象c,将半径设置为2,高为10,分别输出对象c的半径、高、表面积和体积。

创建第2个对象d,将c赋给d,分别输出对象d的半径、高、表面积和体积。

//以下为Cylinder的声明部分,cylinder.h

classCylinder

doubleradius,height;

Cylinder();

Cylinder(doubleh,doubler);

doublegetHeight();

voidgetHR(double&

h,double&

r);

voidsetHR(double,double);

doublegetSurfaceArea();

doublegetVolume();

//以下为Cylinder类的实现部分,cylinder.cpp

Cylinder.h"

Cylinder:

Cylinder()

Cylinder(doubleh,doubler)

doubleCylinder:

voidCylinder:

getHR(double&

h,double&

r)

r=radius;

voidCylinder:

setHR(doubleh,doubler)

getSurfaceArea()

return2*3.14*radius*radius+2*3.14*radius*height;

getVolume()

return3.14*radius*radius*height;

//以下为test.cpp

cylinder.h"

Cylinderc;

doublea,b;

c.getHR(a,b);

a<

\t"

b<

c.getSurfaceArea()<

c.getVolume()<

Cylinderd(10,2);

doublex,y;

x=d.getHeight();

y=d.getRadius();

x<

y<

d.getSurfaceArea()<

d.getVolume()<

}

4.编写复数类Complex,包含:

两个数据成员,表示实部与虚部;

有2个参数的构造函数,且带默认参数值,值均为0

析构函数,输出"

destructoriscalled."

及实部与虚部的值。

同时设置两个数据成员的设置函数,且带默认参数值,默认值均为0;

同时获取两个数据成员的获取函数,以及每个数据成员的获取函数;

取模函数;

输出函数

编写测试程序,创建Complex类对象,分别对各个成员函数进行测试。

//以下为complex.h文件

classComplex

doublereal,imag;

Complex(doubler=0,doublei=0);

~Complex();

voidsetRI(double=0,double=0);

doublegetReal();

doublegetImag();

voidgetRI(double&

doublegetMod();

voidprint();

//以下为complex.cpp

complex.h"

math.h>

Complex:

Complex(doubler,doublei)

real=r;

imag=i;

constructoriscalled."

real<

imag<

~Complex()

voidComplex:

setRI(doubler,doublei)

doubleComplex:

getReal()

returnreal;

getImag()

returnimag;

getRI(double&

r,double&

i)

r=real;

i=imag;

getMod()

returnsqrt(real*real+imag*imag);

print()

if(imag>

0)

cout<

real<

+"

i"

endl;

elseif(imag<

<

else

//以下为test.cpp文件

#include<

Complexc1;

c1.getMod()<

c1.print();

Complexc2(2,2);

c2.getMod()<

c2.print();

Complexc3(-2.5);

c3.getMod()<

c3.print();

5.定义整型数组类CArray,包括:

两个数据成员,整型指针变量(表示数组首地址)及整型变量(表示数组长度)。

构造函数,有1个参数且带默认参数值5。

析构函数,释放动态内存。

设置某个数组元素的值

获取某个数组元素的值

测试该数组类:

创建一个包含有5个元素的对象a,为数组元素分别赋值为1,2,3,4,5,求数组元素的平均值,并输出各个数组元素与平均值。

创建一个包含有10个数组元素的数组,从键盘输入各个数组元素,而后计算平均值,并输出各个数组元素的值及平均值。

//carray.h,类的声明

classCArray

CArray(intn1=5);

//创建包含n1个数组元素的整型数组。

~CArray();

//析构函数,删除动态申请的数组空间

intgetAt(inti);

//得到下标为i的数组元素的值

voidsetAt(inti,intx);

//将下标为i的数组元素的值设置为x.

int*p;

//整型数组首地址

intn;

//数组中的元素个数

//carray.cpp,类的实现

carray.h"

CArray:

CArray(intn1)

n=n1;

p=newint[n];

//申请动态内存

constructoriscalled:

n<

~CArray()

delete[]p;

//释放动态内存

intCArray:

getAt(inti)

returnp[i];

voidCArray:

setAt(inti,intx)

p[i]=x;

//test.cpp,类的测试

carray.h"

CArrayt(10);

for(inti=0;

i<

10;

i++)

t.setAt(i,i+1);

intsum1=0;

for(i=0;

sum1+=t.getAt(i);

t.getAt(i)<

sum1/10<

CArrays;

intx;

plzenter5integers:

5;

{

cin>

>

x;

s.setAt(i,x);

intsum2=0;

s.getAt(i)<

sum2+=s.getAt(i);

sum2/5<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 自然景观

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1