当前位置:文档之家› 福建农林大学计算机与信息学院实验报告

福建农林大学计算机与信息学院实验报告

doubleCircumference(){return2*(len+wid);}
doubleArea(){returnlen*wid;}
doublegetl(){returnlen;}
doublegetw(){returnwid;}
voidcharge(doublea,doubleb){len=a;wid=b;}
p2.print();
cout<<"p2周长:"<<p2.Circumference()<<endl;
cout<<"p2面积:"<<p2.Area()<<endl;
cout<<"p2的长度:"<<p2.getl()<<endl;
cout<<"p2的宽度:"<<p2.getw()<<endl;
p2.charge(5.0,6.0);
cout<<"修改后的矩形的尺寸:";
p2.print();
return0;
}
5.调试程序所有sy4_1,写出输出结果,分析输出结果。
6.分析调试sy4_2,回答sy4_2的问题。
7.
程序如下:
//sy4_3
#include<iostream>
usingnamespacestd;
classstudent
cout<<"班级平均分为:";
cout<<student::average()<<endl;
return0;
}
8.程序如下:
//sy4_4
#include<iostream>
usingnamespacestd;
classRuler;
classBook
{
public:
Book(inti=0){weight=i;}
{
floats;
intn;
studenta[10];
cout<<"输入学生的个数(10个以内):";
cin>>n;
cout<<"输入学生成绩:";
for(inti(0);i<n;i++)
{
cin>>s;
a[i].scoretotalcount(s);
}
cout<<"班级总分为:";
cout<<student::sum()<<endl;
5调试下列程序,写出输出结果,并分析输出结果。
//sy4_1.cpp
#include<iostream>
usingnamespacestd;
classMy
{
public:
My(intaa)
{
A=aa;
B-=aa;
}
staticvoidfun(Mym);
private:
intA;
staticintB;
{
TPointp1(4,9);
TPointp2(p1);
TPointp3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return0;
}
在该程序中,将TPoint类的带有两个参数的构造函数进行修改,在函数体内增添下述语句:
cout<<"Contructoriscalled.\n"
{
i=x;
cout<<"Contstructoriscalled."<<i<<endl;
}
Heapclass::Heapclass()
{
cout<<"DefaultContstructoriscalled."<<endl;
}
Heapclass::~Heapclass()
{
cout<<"Defaultiscalled."<<endl;
voidstudent::scoretotalcount(floats)
{
score=s;
total+=score;
count++;
}
floatstudent::sum(){returntotal;}
floatstudent::average(){returntotal/count;}
intmain()
福建农林大学计算机与信息学院实验报告
系(教研室):计算机系专业:计算机科学与技术年级:08级实验课程:面向对象程序设计
姓名:郑彬学号:实验室号:_513_计算机号:2
实验时间:9月29日、10月13日指导教师签字:成绩:
实验二类与类的成员函数的定义方式,理解类成员的访问控制方式。
intgetx(){returnX;}
intgety(){returnY;}
private:
intX,Y;
};
TPoint::TPoint(TPoint&p)
{
X=p.X;
Y=p.Y;
cout<<"Copy-initializentionConstructoriscalled\n";
}
intmain()
};
voidMy::fun(Mym)
{
cout<<"A="<<m.A<<endl;
cout<<"B="<<m.B<<endl;
}
intMy::B=100;
intmain()
{
MyP(6),Q(8);
My::fun(P);
Q.fun(Q);
return0;
}
6.分析并调试程序,完成下列问题。
//sy4_2.cpp
3.写出修改后的程序的结果,并解释输出结果
修改后的程序:
#include<iostream>
usingnamespacestd;
classHeapclass
{
public:
Heapclass(intx);
Heapclass();
~Heapclass();
private:
inti;
};
Heapclass::Heapclass(intx)
//sy3_2.cpp
#include<iostream>
usingnamespacestd;
classTPoint
{
public:
TPoint(intx,inty){X=x;Y=y;}
TPoint(TPoint&p);
~TPoint(){cout<<"Destructoriscalled\n";}
}
intmain()
{
Heapclass*pa1,*pa2,*pa3;
pa1=newHeapclass(4);
pa2=newHeapclass;
pa3=newHeapclass(5);
if(!pa1||!pa2||!pa3)
{
cout<<"OutofMemory!"<<endl;
return0;
}
cout<<"Exitmain"<<endl;
{
public:
voidscoretotalcount(floats);
staticfloatsum();
staticfloataverage();
private:
floatscore;
staticfloattotal;
staticintcount;
};
floatstudent::total=0;
intstudent::count=0;
deletepa1;
deletepa2;
deletepa3;
return0;
}
4#include<iostream>
usingnamespacestd;
classRectangle
{
public:
Rectangle(){len=0;wid=0;}
Rectangle(doubleLen,doubleWid){len=Len;wid=Wid;}
private:
doublex,y;
};
doubledist(My&a,My&b)
{
doubledx=a.x-b.x;
doubledy=a.y-b.y;
returnsqrt(dx*dx+dy*dy);
}
intmain()
{
Mym1,m2(15),m3(13,14);
Mym4(m3);
cout<<"Thedistancel:"<<dist(m1,m3)<<endl;
~Aa(){cout<<"Destructor"<<a<<endl;}
voidprint(){cout<<a<<endl;}
相关主题