C++面向对象程序设计实验报告文档格式.docx

上传人:b****8 文档编号:22585977 上传时间:2023-02-04 格式:DOCX 页数:18 大小:16.77KB
下载 相关 举报
C++面向对象程序设计实验报告文档格式.docx_第1页
第1页 / 共18页
C++面向对象程序设计实验报告文档格式.docx_第2页
第2页 / 共18页
C++面向对象程序设计实验报告文档格式.docx_第3页
第3页 / 共18页
C++面向对象程序设计实验报告文档格式.docx_第4页
第4页 / 共18页
C++面向对象程序设计实验报告文档格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

C++面向对象程序设计实验报告文档格式.docx

《C++面向对象程序设计实验报告文档格式.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计实验报告文档格式.docx(18页珍藏版)》请在冰豆网上搜索。

C++面向对象程序设计实验报告文档格式.docx

voidprint();

};

【circle.cpp】

#include"

circle.h"

circle:

:

circle(floatx,floaty):

pi(3.14)

r=x;

h=y;

}

voidcircle:

print()

cout<

<

"

底面积为:

r*r*pi<

endl;

体积为:

r*r*pi*h<

【main.cpp】

voidmain()

circlea(1,2);

a.print();

实验结果:

3.14

6.28

实验三

用类实现计算两点之间的距离。

(可以定义点类(Point),再定义一个类(Distance)描述两点之间的距离,其数据成员为两个点类对象,两点之间距离的计算可设计由构造函数来实现。

【point.h】

#ifndefpoint_h

#definepoint_h

classpoint

{private:

intx1;

inty1;

point(int,int);

#endif

【point.cpp】

#include"

point.h"

point:

point(intx,inty)

{x1=x;

y1=y;

【distance.h】

math.h>

#ifndefdistance_h

#definedistance_h

namespaceme

classdistance

pointp1;

pointp2;

distance(intx1,intx2,inty1,inty2);

【distance.cpp】

distance.h"

me:

distance:

distance(intx1,intx2,inty1,inty2):

p1(x1,y1),p2(x2,y2)

{cout<

”两点间的距离为:

”<

sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);

me:

distances(1,2,1,2);

两点间的距离为:

1.41

 

实验四

定义学生类(姓名,学号),生日类(年,月,日),利用友元类,输出某个学生的姓名,学号,及其生日。

【student.h】

birthday.h"

classstudent

char*name;

intID;

student(char*s,intn);

voidprint(birthday&

a);

【birthday.h】

#ifndefbi

#definebi

classbirthday

intyear,mouth,day;

friendclassstudent;

birthday(intx,inty,intz);

【student.cpp】

student.h"

student:

student(char*s,intn)

ID=n;

name=newchar[strlen(s)+1];

strcpy(name,s);

voidstudent:

print(birthday&

b)

姓名:

name<

学号:

ID<

生日:

b.year<

b.mouth<

b.day<

birthday:

birthday(intx,inty,intz)

{year=x;

mouth=y;

day=z;

iostream"

voidmain()

studenta("

tjl"

117);

birthdayb(1990,9,1);

a.print(b);

tjl

117

199091

实验五

student(name,id,total),total为静态成员,在student中设置静态函数来处理total。

在main()函数中显示name,id,total

char*name;

intid;

staticinttotal;

student(char*na,intid1);

voidprint();

staticvoidprint1();

【total.cpp】

string.h"

intstudent:

total=0;

student(char*na,intid1)

id=id1;

name=newchar[strlen(na)+1];

strcpy(name,na);

++total;

print1()

total;

name:

cout<

id:

id<

studenta("

tang"

61);

studentb("

guo"

45);

a.print();

b.print();

print1();

tang

61

guo

45

实验六

基类--学生类(姓名,学号),子类--大学生类(专业),继承方式:

公有继承,在主函数中显示某个大学生的信息。

#ifndef_st1

#define_st1

intid;

~student();

~student()

{delete[]name;

{id=id1;

【cstudent.h】

#ifndef_cst

#define_cst

classcstudent:

publicstudent

char*project;

~cstudent();

cstudent(char*na,intid1,char*pro);

voidprint1();

【cstudent.cpp】

cstudent.h"

cstudent:

~cstudent()

{delete[]project;

cstudent(char*na,intid1,char*pro):

student(na,id1)

{project=newchar[strlen(pro)+1];

strcpy(project,pro);

voidcstudent:

{student:

print();

project:

project<

main()

{cstudenta("

17,"

computer"

);

a.print1();

17

conputer

实验七

基类shape,派生类circle(半径R),tangcle(长a,宽b),它们都有求面积的函数area(),利用虚函数求两个派生类对象的面积

【shape.h】

#ifndef_shape

#define_shape

classshape

virtualvoidarea();

【shape.cpp】

shape.h"

voidshape:

area()

{}

classcircle:

publicshape

floatr;

constfloatpi;

circle(floatr1);

voidarea();

};

circle(floatr1):

{r=r1;

pi*r*r<

【tangcle.h】

classtangcle:

floata,b;

tangcle(floatc,floatd);

【tangcle.cpp】

tangcle.h"

tangcle:

tangcle(floatc,floatd)

{a=c;

b=d;

voidtangcle:

a*b<

{shape*p;

circleb

(2);

tangclec(3,4);

p=&

c;

p->

area();

b;

12.56

12

实验八

重载减法运算符,使得两个复数可以相减

【complex.h】

iostream.h>

#ifndef_COMPLEX_H

#define_COMPLEX_H

classcomplex

doublea;

doubleb;

complex(doublea,doubleb);

friendvoidoperator-(complexx,complexy);

【complex.cpp】

complex.h"

complex:

complex(doublea1,doubleb1)

{a=a1;

b=b1;

voidoperator-(complexx,complexy)

实部是:

x.a-y.a<

虚部是:

x.b-y.b<

complexA1(1.0,2.0),A2(3.0,4.0);

A1-A2;

-2

虚部:

-2

实验九

将文件text1的内容拷贝到文件text2中

#include<

fstream>

intmain()

{

ifstreamin("

H:

\\text1.txt"

if(!

in)

{

cerr<

readtext1error"

exit

(1);

}

ofstreamout("

\\text2.txt"

out)

writetext2error"

charch;

while(in)

in.get(ch);

out.put(ch);

return0;

}实验结果:

text1:

55555;

复制到text2显示55555

文件中复制成功

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

当前位置:首页 > 初中教育 > 科学

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

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