C++实验报告模版Word文件下载.docx
《C++实验报告模版Word文件下载.docx》由会员分享,可在线阅读,更多相关《C++实验报告模版Word文件下载.docx(43页珍藏版)》请在冰豆网上搜索。
□验证□综合■设计□创新实验日期:
实验成绩:
一.实验内容
1、类与对象
2、长方形类
二.实验环境
PC微机,Windows操作系统,VisualC++6.0
三.实验代码
类与对象:
#include<
iostream>
cmath>
usingnamespacestd;
classPoint
{
public:
Point(intxx=0,intyy=0)
{
X=xx;
Y=yy;
countP++;
cout<
<
"
第"
countP<
个"
Point构造函数被调用"
;
"
X<
Y<
endl;
}
Point(Point&
p);
~Point()
countP--;
Point析构函数被调用"
intGetX(){returnX;
}
intGetY(){returnY;
//voidGetC(){cout<
Objectid="
private:
intX,Y;
staticintcountP;
};
Point:
:
Point(Point&
p)
X=p.X;
Y=p.Y;
countP++;
cout<
Point拷贝构造函数被调用"
//类的组合
classLine
Line(Pointxp1,Pointxp2);
Line(Line&
L);
doubleGetLen(){returnlen;
Pointp1,p2;
doublelen;
//组合类的构造函数
Line:
Line(Pointxp1,Pointxp2):
p2(xp2),p1(xp1)
Line构造函数被调用"
doublex=double(p1.GetX()-p2.GetX());
doubley=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
//组合类的拷贝构造函数
Line(Line&
L):
p2(L.p2),p1(L.p1)
Line拷贝构造函数被调用"
len=L.len;
intPoint:
countP=0;
//主函数
intmain()
Pointmyp1(1,1),myp2(4,5);
Lineline(myp1,myp2);
Lineline2(line);
Thelengthofthelineis:
line.GetLen()<
Thelengthoftheline2is:
line2.GetLen()<
getchar();
return0;
长方形类:
Point(intxx=0,intyy=0){X=xx;
}//Point类构造函数
p){X=p.X;
Y=p.Y;
}//Point类拷贝构造函数
classRectangle
Rectangle(Pointxp1,Pointxp2);
//组合类的构造函数
doubleArea();
//求面积函数
Rectangle:
Rectangle(Pointxp1,Pointxp2):
p1(xp1),p2(xp2)
{
//求面积函数
doubleRectangle:
Area()
return(p1.GetX()-p2.GetX())*(p1.GetY()-p2.GetY());
voidmain()
Pointmyp1(8,5),myp2(6,1);
//建立Point类的对象
RectangleRec(myp1,myp2);
//建立Rectangle类的对象
长方形的面积为:
Rec.Area()<
四.实验结果
功能实现:
---(4)第五章实验
学号:
一、实验内容
1、静态数据成员
2、静态函数成员
3、对象数组
4、友元类,友元函数测试
二、实验环境
三、实验代码
静态数据成员:
~Point(){countP--;
voidGetC(){cout<
PointA(4,5);
PointA,"
A.GetX()<
"
A.GetY();
A.GetC();
PointB(A);
PointB,"
B.GetX()<
B.GetY();
B.GetC();
静态函数成员:
点类:
~Point(){countP--;
staticvoidGetC(){cout<
主文件1:
#include"
point.h"
inti1=3;
staticinti2=3;
voidother1();
voidother2();
voidfun1();
externvoidfun2();
externvoidfun3();
voidfun3()
文件1中的函数fun3()被调用!
PointA,X="
Y="
A.GetY()<
PointB,X="
B.GetY()<
Point:
GetC();
我在这个文件中定义了一个整型变量i1,它具有文件作用域,此时它的值为:
i1<
接着调用函数other1()"
other1();
我在此还定义了一个整型变量i2,但给它加了static修饰,此时它的值为:
i2<
接着调用函数other2()"
other2();
fun1();
fun2();
fun3();
为什么在这里输出的是‘文件2中的函数fun3()被调用!
’而不是‘文件1中的函数’"
fun3()被调用!
因为文件2中fun3()前加了static修饰,就将函数的作用域限制在了"
当前的编译单元,在文件1中不可见!
也就不能被文件1中的main函数调用了,同时"
我还在文件1中定义了一个同名函数fun3(),但其输出的内容不一样,可用来判断"
到底是哪个文件中函数被调用!
实验证明是文件1中函数被调用!
endl<
上面的两个例子说明了一个问题,那就是‘加了static修饰的变量和函数只能在本"
文件中执行和调用,不能再在外部文件中被调用,即使是加了extern修饰也不行!
’"
主文件2:
externinti1;
voidother1()
i1++;
函数other1()被调用,i1的值为:
(这里的函数other1()是在另一个.cpp文件中定义的,对i1自加1)"
externinti2=2;
voidother2()
i2++;
函数other2()被调用,i2的值为:
(这里的函数other2()是在另一个.cpp文件中定义的,对i2自加1)"
为什么在这里i2的值是3而不是4呢?
因为在other2()这个文件中,给i2指定值为2,加1后当然为3了"
那为什么在这里要给i2指定值呢?
因为在这个文件中用extern声明i2连接时会出错,i2在文件1中声明为static类型,"
不能被外部文件引用!
voidfun1()
文件2中的函数fun1()被调用!
voidfun2()
文件2中的函数fun2()被调用!
staticvoidfun3()
文件2中的函数fun3()被调用!
对象数组:
员工类:
classemployee
protected:
intindividualEmpNo;
intgrade;
floataccumPay;
staticintemployeeNo;
employee();
~employee();
voidpromote(int);
voidSetaccumPay(float);
intGetindividualEmpNo();
intGetgrade();
floatGetaccumPay();
Main1:
#include<
#include"
employee.h"
intemployee:
employeeNo=1000;
employee:
employee()
individualEmpNo=employeeNo++;
grade=1;
accumPay=0.0;
~employee()
voidemployee:
promote(intincrement)
grade+=increment;
SetaccumPay(floatpa)
accumPay=pa;
GetindividualEmpNo()
returnindividualEmpNo;
Getgrade()
returngrade;
floatemployee:
GetaccumPay()
returnaccumPay;
Main2:
employeea[4];
floatpa;
for(inti=0;
i<
4;
i++)
请输入下一个雇员的月薪:
cin>
>
pa;
a[i].promote(i+1);
a[i].SetaccumPay(pa);
\t编号\t级别\t月薪"
\t"
a[i].GetindividualEmpNo()<
a[i].Getgrade()<
a[i].GetaccumPay()<
友元类测试:
Class类:
classX;
//前向引用声明
classY//定义一个类Y
voidg(X&
x);
classX//定义一个类X
friendvoidY:
g(X&
//类Y的成员函数是类X的友元函数
friendclassZ;
//类Z是类X的友元类
friendvoidh(X&
//普通函数h(X&
x)是类X的友元函数
X(intI){i=I;
intGeti()
returni;
inti;
//Y的成员函数f(X&
x)的实现部分
voidY:
x)
x.i++;
x.i<
classZ//定义一个类Z
voidf(X&
//Z的成员函数f(X&
x)(也是类Y的友元函数)的实现部分
voidZ:
f(X&
x.i+=5;
//函数h(X&
voidh(X&
x.i+=10;
Main文件:
#ifndefclass_h
#defineclass_h
class.h"
#endif
Xmyx
(1);
Ymyy;
Zmyz;
myx.i的初始值:
myx.Geti()<
执行函数g(x)后,myx.i的值:
myy.g(myx);
执行函数f(x)后,myx.i的值:
myz.f(myx);
执行函数h(x)后,myx.i的值:
h(myx);
友元函数测试2:
classcar;
//前向引用声明
classboat//定义一个类boat
friendinttotalweight(car&
c,boat&
b);
boat(intw){weight=w;
intweight;
classcar//定义一个类car
car(intw){weight=w;
intweight;
//boat和car的友元函数totalweight的实现,计算二者的重量和
inttotalweight(car&
b)
returnc.weight+b.weight;
carC(1000);
boatB(1234);
Thetotalweightofthecarandtheboatis:
totalweight(C,B)<
四、实验结果
友元函数测试1:
---(5)第六章实验
某同学学号:
1、掌握数组的基本使用方法。
2、练习通过动态内存分配实现动态数组,并体会指针在其中的作用。
使用对象数组的方法改造“employee”函数的对象。
代码如下:
//employee.h
//employee.cpp
//5_11.cpp
请输入下一位雇员的月薪"