资源描述
《C++面向对象程序设计》实验报告
实验序号:2 实验项目名称:类和对象
学 号
姓 名
专业
实验地点
指导教师
吴芸
实验时间
2013-3-21
一、实验目的及要求
(1)理解类和对象的概念;
(2)了解C++在非面向对象方面对C功能的扩充与增强。
(3)初步掌握使用类和对象编制C++程序。
(4)掌握对象数组、对象指针和string类的使用方法。
(5)掌握使用对象、对象指针和对象引用作为函数参数的方法。
(6)掌握类对象作为成员的使用方法。
(7)掌握静态数据成员和静态成员函数的使用方法。
(8)理解友元的概念和掌握友元的使用方法。
二、实验设备(环境)及要求
Micorsoft Visual C++ 6.0
三、实验内容与步骤(题目、算法和结果描述)
1、输入下列程序。
#include<iostream>
using namespace std;
class Coordinate
{
public:
Coordinate(int x1,int y1)
{ x=x1;
y=y1; }
Coordinate(Coordinate &p);
~Coordinate()
{ cout<<"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);
Coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return 0;
}
(1) 写出程序的运行结果。
【运行结果截图】:
(2) 将Coordinator类中带有两个参数的构造函数进行修改,在函数体内增添下列语句: cout<<”constructor is called.\n”;
【运行结果截图】:
【运行结果分析】:带有两个参数的构造函数在主函数中只被调用了一次,其他的都是调用使用对象引用作为函数参数的构造函数。
(3) 按下列要求进行调试:在主函数体内,添加下列语句:
Coordinator p4;
Coordinator p5(2);
调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?
【运行结果截图】:
【解释】:把带有两个参数的构造函数改为带有默认参数的构造函数,那么后添加的两条语句编译的时候就不会出错了。
(4) 经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。
【运行结果分析】:p1是调用有两个参数的构造函数,p2和p3都是p1的拷贝,但他们调用的是使用对象引用作为函数参数的构造函数,p4和p5都是调用两个参数的构造函数,p4由于没有给出实参,所以使用默认参数,即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) 设置初始魔方元素的值,例如上述魔方的初始魔方为:
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();
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为两个坐标点之间的距离。
【提示】:类Location的参考框架如下:
class Location
{public:
Location(double,double); //构造函数
double getx(); //成员函数,取x坐标值
double gety(); //成员函数,取y坐标值
double distancee(Location &); //成员函数,求给定两点之间的距离
friend double distancee(Location &,Location &); //友元函数,求给定两点之间的距离
private:
double x,y;
}
【运行结果截图】:
4、声明一个Student类,在该类中包括一个暑假成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全部成绩的平均值。在main函数中,输入某班同学的成绩,并调用上述函数求出全班同学的成绩之和和平均分。
【Student类的框架】
class Student
{
public:
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 Germany
【运行结果截图】:
6、定义一个圆类(Circle),属性为半径(radius)和圆周长、面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,
圆周长和面积在构造函数中生成)和复制构造函数。
【运行结果截图】:
7、教材P134 习题[3.33]和[3.34]
【运行结果截图】:
四、分析与讨论(记录实验过程中出现的主要问题和心得体会)
五、教师评语
签名:
日期:
成绩
附:程序源代码
1、#include<iostream>
using namespace std;
class 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::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);
Coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
Coordinate p4;
Coordinate p5(2);
return 0;
}
2、#include<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 first 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]=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<<m[i][j]<<'\t';
}
cout<<endl;
}
}
int main()
{
magic A;
A.getdata();
A.getfirstmagic();
A.generatemagic();
A.printmagic();
return 0;
}
3、#include<iostream.h>
#include<math.h>
class Location
{
public:
Location(double,double);
double getx();
double gety();
double distance(Location &);
friend double distance(Location & , Location &);
private:
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 distance(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("<<A.getx()<<","<<A.gety()<<"),B("<<B.getx()<<","<<B.gety()<<")"<<endl;
double d=A.distance(B);
cout<<"Distance1="<<d<<endl;
cout<<"Distance2="<<distance(A,B)<<endl;
return 0;
}
4、#include<iostream>
using namespace std;
class Student
{
public:
void account(float);
static float sum();
static float average();
private:
float score;
static float total_score;
static int count;
};
void 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;
int main()
{
float score1;
int n=0;
Student a;
cout<<"-----请输入学生成绩-----"<<endl;
cout<<"----若要退出输入按00----"<<endl;
do{
cin>>score1;
a.account(score1);
if(score1==00)
break;
n++;
}while(1);
cout<<"the class size is : "<<n<<endl;
cout<<"the total score is : "<<a.sum()<<endl;
cout<<"the average score is : "<<a.average()<<endl;
return 0;
}
5、#include<iostream>
#include<string>
using namespace std;
int main()
{
int i;
string ch[5];
for(i=0;i<5;i++)
cin>>ch[i];
for(i=4;i>=0;i--)
cout<<ch[i]<<" ";
cout<<endl<<endl;
return 0;
}
6、#include<iostream>
using namespace std;
class Circle
{
private:
double r,area,circumference;
public:
Circle(double);
Circle(const Circle &);
double R();
double Area();
double Circumference();
};
Circle::Circle(double t)
{
r=t;
}
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 : "<<p1.R()<<endl;
cout<<"the circle's Area is : "<<p1.Area()<<endl;
cout<<"the circel's circumference is : "<<p1.Circumference()<<endl;
Circle p2(p1);
cout<<"the circle's R is : "<<p2.R()<<endl;
cout<<"the circle's Area is : "<<p2.Area()<<endl;
cout<<"the circel's circumference is : "<<p2.Circumference()<<endl;
Circle p3(3.05);
cout<<"the circle's R is : "<<p3.R()<<endl;
cout<<"the circle's Area is : "<<p3.Area()<<endl;
cout<<"the circel's circumference is : "<<p3.Circumference()<<endl;
return 0;
}
7、
3.33
#include<iostream>
using namespace std;
class book
{
private:
int 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<<a[i].ride()<<' ';
cout<<endl;
return 0;
}
3.34
#include<iostream>
using namespace std;
class book
{
private:
int 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};
book *p;
p=&a[4];
for(int i=4;i>=0;i--)
{
cout<<p->ride()<<' ';
p--;
}
cout<<endl;
return 0;
}
展开阅读全文