1、实验报告三
实验名称:静态成员函数和友元 学时安排:课内1+课外3
实验类别:综合性实验 实验要求:3人1组
完成人:学号 姓名
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、实验目的和任务
1)掌握静态、动态生存期的概念;
2)掌握静态数据成员和函数成员的概念
3)掌握友元函数的含义,友元函数和成员函数的区别,掌握友元函数设计的方法
二、实验原理介绍
验证性实验,通过运行课本例题,了解并熟悉以下过程:
建立类及对象,用
2、类的成员函数和对象访问类的成员;
利用建立类的构造函数,完成类的成员的初始化。
三、实验设备介绍
软件需求: Visual C++ 6.0
硬件需求:建议配置是Pentium III 450以上的CPU处理器,64MB以上的内存,200MB的自由硬盘空间、CD-ROM驱动器、24位真彩色显示卡、彩色显示器。
四、实验内容
1、设计并测试一个矩形类(Rectangle),属性为矩形的左下与右上角的坐标,矩形水平放置。操作为计算矩形周长与面积。类对象的数据由友元函数来进行访问。并对比友元函数、成员函数和普通函数使用上的不同。
#include
#include
3、
using namespace std;
class Rectangle
{
public:
Rectangle(int t=0, int l=0, int d=0, int r=0)
{
top=t;
left=l;
down=d;
right=r;
}
~Rectangle() {};
//Circum() {return 2*(fabs(top-down)+fabs(left-right));}
//Area() {return (fabs(top-down))*(fabs(left-right))}
fr
4、iend void cal(Rectangle r);//括号中要写上类名和内容
private:
int top,left,down,right;
};
void cal(Rectangle r) //不用带参数
{
cout<<"矩形的长是:"<5、abs(r.top-r.down))*(fabs(r.left-r.right))<
#include
using namespace std;
class Boat
{
public:
Boat(double w)
6、{
weight=w;
}
friend double totalWeight(Boat a)
{return a.weight;}
private:
double weight;
};
class Car
{
public:
Car(double w)
{
weight=w;
}
friend double totalWeight(Car b)
{return b.weight;}
private:
double weight;
};
void main()
{
Boat aa(300.785678);
C
7、ar bb(400);
cout<<"totalweight:"<