1、 《C++面向对象程序设计》实验报告 实验序号:2 实验项目名称:类和对象 学 号 姓 名 专业 实验地点 指导教师 吴芸 实验时间 2013-3-21 一、实验目的及要求 (1)理解类和对象的概念; (2)了解C++在非面向对象方面对C功能的扩充与增强。 (3)初步掌握使用类和对象编制C++程序。 (4)掌握对象数组、对象指针和string类的使用方法。 (5)掌握使用对象、对象指针和对象引用作为函数参数的方法。 (6)掌握类对象作为成员的使用方法。 (7)掌握
2、静态数据成员和静态成员函数的使用方法。
(8)理解友元的概念和掌握友元的使用方法。
二、实验设备(环境)及要求
Micorsoft Visual C++ 6.0
三、实验内容与步骤(题目、算法和结果描述)
1、输入下列程序。
#include
3、<"Destructor is called\n";} int getx() { return x;} int gety() { return y;} private: int x,y; }; Coordinate::Coordinate(Coordinate &p) { x=p.x; y=p.y; cout<<"Copy-initialization Constructour is called\n"; } int main() { Coordinate p1(3,4); Coordinate p2(p1); Coordinat
4、e p3=p2;
cout<<"p3=("< 5、
Coordinator p4;
Coordinator p5(2);
调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?
【运行结果截图】:
【解释】:把带有两个参数的构造函数改为带有默认参数的构造函数,那么后添加的两条语句编译的时候就不会出错了。
(4) 经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。
【运行结果分析】:p1是调用有两个参数的构造函数,p2和p3都是p1的拷贝,但他们调用的是使用对象引用作为函数参数的构造函数,p4和p5都是调用两个参数的构造函数,p4由于没有给出实参,所以使用默认参数,即 6、x=1,y1;而p5给出一个实参,所以x=2,y=1.
2、设计一个4*4魔方程序,让魔方的各行值的和等于各列值的和,并且等于两对角线的和,例如以下魔方,各行各列及两对角线值的和都是64.
31 3 5 25
9 21 19 15
17 13 11 23
7 27 29 1
【提示】:求4*4的魔方的一般步骤如下:
(1) 设置初始魔方的起始值和相邻元素之间的差值。例如上述魔方的初始魔方的起始值(first)和相邻元素之间的差值(step)分别为:first=1; step=2;
(2) 设置初始魔方元素的值,例如上述魔方 7、的初始魔方为:
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
(3) 生成最终魔方。方法如下:
1) 求最大元素值与最小元素值的和sum,该实例的sum是:1+31=32
2) 用32减去初始魔方所有对角线上元素的值,然后将结果放在原来的位置,这样就可以求得最终魔方。
本题的魔法类magic的参考框架如下:
class magic
{public:
void getdata();
void getfirstmagic();
void generatemagic();
8、 void printmagic();
private:
int m[4][4];
int step;
int first;
int sum;
}
【运行结果截图】:
3、设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:
A(x1,y1), B(x2,y2),
Distance1=d1
Distance2=d2
其中:x1、y1、x2、y2为指定坐标值,d1和d2为两个坐标点之间的距 9、离。
【提示】:类Location的参考框架如下:
class Location
{public:
Location(double,double); //构造函数
double getx(); //成员函数,取x坐标值
double gety(); //成员函数,取y坐标值
double distancee(Location &); //成员函数,求给定两点之间的距离
friend double distancee(Location &,Location &); //友元函数,求给定两点之间的距离 10、
private:
double x,y;
}
【运行结果截图】:
4、声明一个Student类,在该类中包括一个暑假成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全部成绩的平均值。在main函数中,输入某班同学的成绩,并调用上述函数求出全班同学的成绩之和和平均分。
【Student类的框架】
class Student
{
public:
11、void account();
static float sum();
static float average();
private:
float score;
static float total_score;
static int count;
};
【运行结果截图】:
5、使用C++的string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前5个字符的字符串是: Germany Japan America Britain France
按逆转后的顺序输出字符串是:France Britain America Japan Germ 12、any
【运行结果截图】:
6、定义一个圆类(Circle),属性为半径(radius)和圆周长、面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,
圆周长和面积在构造函数中生成)和复制构造函数。
【运行结果截图】:
7、教材P134 习题[3.33]和[3.34]
【运行结果截图】:
四、分析与讨论(记录实验过程中出现的主要问题和心得体会)
五、教师评语
签名:
日期:
成绩
附:程序源代码
1、#include 13、ss Coordinate
{
public:
Coordinate(int x1=1,int y1=1)
{ x=x1;
y=y1;
cout<<"constructor is called.\n"; }
Coordinate(Coordinate &p);
~Coordinate()
{ cout<<"Destructor is called\n";}
int getx()
{ return x;}
int gety()
{ return y;}
private:
int x,y;
};
Coordinate::Coo 14、rdinate(Coordinate &p)
{
x=p.x;
y=p.y;
cout<<"Copy-initialization Constructour is called\n";
}
int main()
{
Coordinate p1(3,4);
Coordinate p2(p1);
Coordinate p3=p2;
cout<<"p3=("< 15、iostream>
using namespace std;
class magic
{
public:
void getdata();
void getfirstmagic();
void generatemagic();
void printmagic();
private:
int m[4][4];
int step;
int first;
int sum;
};
void magic::getdata()
{
cout<<"please input fir 16、st and step : ";
cin>>first>>step;
}
void magic::getfirstmagic()
{
int i,j,t;
t=first;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
m[i][j]=first;
first+=step;
}
sum=t+m[3][3];
}
void magic::generatemagic()
{
int i,j;
for(i=0;i<4;i++)
m[i][i]= 17、sum-m[i][i];
for(i=0,j=3;i<4,j>=0;i++,j--)
m[i][j]=sum-m[i][j];
}
void magic::printmagic()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout< 18、rstmagic();
A.generatemagic();
A.printmagic();
return 0;
}
3、#include 19、
double x,y;
};
Location::Location(double r,double i)
{
x=r;
y=i;
}
double Location::getx()
{
return x;
}
double Location::gety()
{
return y;
}
double Location::distance(Location &t)
{
double dx=x-t.x;
double dy=y-t.y;
return sqrt(dx*dx+dy*dy);
}
double distan 20、ce(Location &a , Location &b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return (sqrt(dx*dx+dy*dy));
}
int main()
{
Location A(-1,-1);
Location B(-2,2);
cout<<"A("< 21、nce1="< 22、oid Student::account(float score1)
{
score=score1;
total_score+=score;
count++;
}
float Student::sum()
{
float k=total_score;
return k;
}
float Student::average()
{
float t=total_score/(count-1);
return t;
}
float Student::total_score=0.0;
int Student::count=0;
23、
int main()
{
float score1;
int n=0;
Student a;
cout<<"-----请输入学生成绩-----"< 24、<"the total score is : "< 25、
cout< 26、
}
Circle::Circle(const Circle & p)
{
r=2*p.r;
}
double Circle::R()
{
return r;
}
double Circle::Area()
{
return 3.14159265*r*r;
}
double Circle::Circumference()
{
return 2*3.14159265*r;
}
int main()
{
Circle p1(1.0);
cout<<"the circle's R is : "< 27、ndl;
cout<<"the circle's Area is : "< 28、ce()< 29、 qu;
int price;
public:
book(int);
int getx();
int ride();
};
book::book(int i)
{
qu=i;
price=10*qu;
}
int book::ride()
{
return price*qu;
}
int main()
{
book a[5]={1,2,3,4,5};
for(int i=0;i<5;i++)
cout< 30、rn 0;
}
3.34
#include






