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

上传人:b****8 文档编号:9449926 上传时间: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

C++面向对象程序设计实验报告

实验报告

姓名:

***

学号:

************

学校:

桂林航天工业高等技术专科学校

指导老师:

***

实验2

实验题目:

用类定义,输入半径和高,输出圆柱体的底面积和体积

实验程序代码:

【circle.h】

#include

usingnamespacestd;

classcircle

{

private:

floatr,h;

constdoublepi;

public:

circle(floatx,floaty);

voidprint();

};

【circle.cpp】

#include"circle.h"

circle:

:

circle(floatx,floaty):

pi(3.14)

{

r=x;

h=y;

}

voidcircle:

:

print()

{

cout<<"底面积为:

"<

cout<<"体积为:

"<

}

【main.cpp】

#include

usingnamespacestd;

#include"circle.h"

voidmain()

{

circlea(1,2);

a.print();

}

实验结果:

底面积为:

3.14

体积为:

6.28

实验三

实验题目:

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

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

实验程序代码:

【point.h】

#include

usingnamespacestd;

#ifndefpoint_h

#definepoint_h

classpoint

{private:

intx1;

inty1;

public:

point(int,int);

};

#endif

【point.cpp】

#include"point.h"

point:

:

point(intx,inty)

{x1=x;y1=y;}

【distance.h】

#include

usingnamespacestd;

#include"point.h"

#include

#ifndefdistance_h

#definedistance_h

namespaceme

{

classdistance

{private:

pointp1;

pointp2;

public:

distance(intx1,intx2,inty1,inty2);

};

【distance.cpp】

#include

usingnamespacestd;

#include"distance.h"

#include"point.h"

me:

:

distance:

:

distance(intx1,intx2,inty1,inty2):

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

{cout<<”两点间的距离为:

”<

【main.cpp】

#include

usingnamespacestd;

#include"distance.h"

#include"point.h"

voidmain()

{

me:

:

distances(1,2,1,2);

}

实验结果:

两点间的距离为:

1.41

 

实验四

实验题目:

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

实验程序代码:

【student.h】

#include

usingnamespacestd;

#include"birthday.h"

classstudent

{

private:

char*name;

intID;

public:

student(char*s,intn);

voidprint(birthday&a);

};

【birthday.h】

#include

usingnamespacestd;

#ifndefbi

#definebi

classbirthday

{

private:

intyear,mouth,day;

friendclassstudent;

public:

birthday(intx,inty,intz);

voidprint();

};

#endif

【student.cpp】

#include"student.h"

student:

:

student(char*s,intn)

{

ID=n;

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

strcpy(name,s);

}

voidstudent:

:

print(birthday&b)

{

cout<<"姓名:

"<

cout<<"学号:

"<

cout<<"生日:

"<

}

【birthday.h】

#include"birthday.h"

birthday:

:

birthday(intx,inty,intz)

{year=x;

mouth=y;

day=z;}

【main.cpp】

#include"iostream"

usingnamespacestd;

#include"student.h"

#include"birthday.h"

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

实验程序代码:

【student.h】

#include

usingnamespacestd;

classstudent

{

private:

char*name;

intid;

staticinttotal;

public:

student(char*na,intid1);

voidprint();

staticvoidprint1();

};

【total.cpp】

#include

usingnamespacestd;

#include"student.h"

#include"string.h"

intstudent:

:

total=0;

student:

:

student(char*na,intid1)

{

id=id1;

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

strcpy(name,na);

++total;

}

voidstudent:

:

print1()

{cout<

voidstudent:

:

print()

{cout<<"name:

"<

cout<<"id:

"<

【main.cpp】

#include

usingnamespacestd;

#include"student.h"

voidmain()

{

studenta("tang",61);

studentb("guo",45);

a.print();

b.print();

student:

:

print1();

}

实验结果:

name:

tang

id:

61

name:

guo

id:

45

实验六

实验题目:

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

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

实验程序代码:

【student.h】

#include

usingnamespacestd;

#ifndef_st1

#define_st1

classstudent

{

private:

char*name;

intid;

public:

~student();

student(char*na,intid1);

voidprint();

};

#endif

 

【student.cpp】

#include

usingnamespacestd;

#include"string.h"

#include"student.h"

student:

:

~student()

{delete[]name;}

student:

:

student(char*na,intid1)

{id=id1;

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

strcpy(name,na);

}

voidstudent:

:

print()

{cout<<"name:

"<

cout<<"id:

"<

}

 

【cstudent.h】

#include

usingnamespacestd;

#include"student.h"

#ifndef_cst

#define_cst

classcstudent:

publicstudent

{

private:

char*project;

public:

~cstudent();

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

voidprint1();

};

#endif

 

【cstudent.cpp】

#include

usingnamespacestd;

#include"string.h"

#include"cstudent.h"

#include"student.h"

cstudent:

:

~cstudent()

{delete[]project;}

cstudent:

:

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

student(na,id1)

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

strcpy(project,pro);

}

voidcstudent:

:

print1()

{student:

:

print();

cout<<"project:

"<

【main.cpp】

#include

usingnamespacestd;

#include"string.h"

#include"student.h"

#include"cstudent.h"

main()

{cstudenta("tang",17,"computer");

a.print1();

}

实验结果:

name:

tang

id:

17

project:

conputer

实验七

实验题目:

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

实验程序代码:

【shape.h】

#include"iostream"

usingnamespacestd;

#ifndef_shape

#define_shape

classshape

{

public:

virtualvoidarea();

};

#endif

【shape.cpp】

#include"shape.h"

voidshape:

:

area()

{}

【circle.h】

#include"shape.h"

classcircle:

publicshape

{private:

floatr;

constfloatpi;

public:

circle(floatr1);

voidarea();

};

【circle.cpp】

#include"circle.h"

circle:

:

circle(floatr1):

pi(3.14)

{r=r1;}

voidcircle:

:

area()

{cout<

【tangcle.h】

#include"iostream"

#include"shape.h"

usingnamespacestd;

classtangcle:

publicshape

{private:

floata,b;

public:

tangcle(floatc,floatd);

voidarea();

};

【tangcle.cpp】

#include"tangcle.h"

tangcle:

:

tangcle(floatc,floatd)

{a=c;b=d;}

voidtangcle:

:

area()

{cout<

【main.cpp】

#include"shape.h"

#include"circle.h"

#include"tangcle.h"

main()

{shape*p;

circleb

(2);

tangclec(3,4);

p=&c;

p->area();

p=&b;

p->area();

}

实验结果:

12.56

12

实验八

实验题目:

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

实验程序代码:

【complex.h】

#include

#ifndef_COMPLEX_H

#define_COMPLEX_H

classcomplex

{

public:

doublea;

doubleb;

public:

complex(doublea,doubleb);

voidprint();

friendvoidoperator-(complexx,complexy);

};

#endif

【complex.cpp】

#include

#include"complex.h"

#include"string.h"

complex:

:

complex(doublea1,doubleb1)

{a=a1;b=b1;}

【main.cpp】

#include

#include"complex.h"

#include"string.h"

voidoperator-(complexx,complexy)

{cout<<"实部是:

"<

cout<<"虚部是:

"<

voidmain()

{

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

A1-A2;

}

实验结果:

实部是:

-2

虚部:

-2

实验九

实验题目:

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

实验程序代码:

#include

#include

usingnamespacestd;

intmain()

{

ifstreamin("H:

\\text1.txt");

if(!

in)

{

cerr<<"readtext1error"<

exit

(1);

}

ofstreamout("H:

\\text2.txt");

if(!

out)

{

cerr<<"writetext2error"<

exit

(1);

}

charch;

while(in)

{

in.get(ch);

out.put(ch);

}

return0;

}实验结果:

text1:

55555;复制到text2显示55555

文件中复制成功

 

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

当前位置:首页 > 工程科技 > 能源化工

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

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