实验4 含有类的静态成员与类的友元的C++程序设计
专业:计算机科学与技术班级:10计本1班学号:姓名:
实验地点:B102实验时间:2011/11/1 指导教师:李佐勇
一、实验目的
1.理解类的静态成员解决共享问题的机制;
2.掌握类的静态数据成员与静态函数成员的实现方法;
3.理解利用友元关系实现数据共享的机制,掌握类的友元函数以及友元类的实现方法;
4.学习多文件结构在C++程序中的使用。
二、实验环境
一台PC机,Windows XP操作系统,Visual C++ 6.0开发环境。
三、实验内容
1.设计一个解决王婆卖瓜问题的程序。
王婆卖瓜,每卖一个瓜,需记录该瓜的重量,还要记录所卖出的总重量和总个数,同时还允许退瓜。
设计一个具有静态数据、函数成员的watermelon类。
实现提示:西瓜类有3个数据成员:重量weight、总重量total_weight以及总个数total_number。
因为不论西瓜是否卖出,总重量total_weight和总个数total_number这两个数据总是要保留的,且这两个数据与单个的西瓜无关联,因此这两个数据要申明为静态数据成员。
成员函数:卖瓜用构造函数模拟,退瓜用析构函数模拟,瓜重用disp成员函数给出屏幕提示。
为了用不与特定对象相联系的静态成员函数来访问静态数据,还需要定义一个显示总重量和总个数的静态成员函数total_disp。
2.设计一个程序,其中有3个类,即CBank、BBank和GBank,分别为中国银行类、工商银行类和农业银行类。
每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另有一个友元函数total用于计算储户在这3家银行中的总存款数。
3. 设计一个程序,其中有2个类,Point类为点类,包含2个私有数据x和y,表示点的坐标,line类为直线类,包含3个私有数据a、b和c,表示直线方程ax+by+c=0。
另有一个友元函数dist,用于计算一个点到直线的距离。
点与直线之间的距离计算公式如下:
2 2b a c
by
ax
d
++
+
=
要求:
①将Point与Line类的定义放在头文件head.h中;
②将Point与Line类的实现部分放在PL.cpp中;
③主函数(类的使用)文件定义为:Lab04_3.cpp。
四、实验记录
1.#include<iostream>
using namespace std;
class watermelon
{
public:
watermelon(double w){
weight=w;
total_weight+=w;
total_number++;
}
~watermelon(){
total_weight-=weight;
total_number--;
}
static void total_disp(){
cout<<"Now total weight is:"<<total_weight<<endl;
cout<<"Now total number is:"<<total_number<<endl;
}
void disp(){
cout<<"The weight is:"<<weight<<endl;
}
private:
double weight;
static double total_weight;
static int total_number;
};
double watermelon::total_weight=0.0;
int watermelon::total_number=0;
void main(){
watermelon W1(25.8);
W1.disp();
watermelon::total_disp();
watermelon W2(23.5);
W2.disp();
watermelon::total_disp();
watermelon W3(36.2);
W3.disp();
watermelon::total_disp();
}
2. #include<iostream>
using namespace std;
class CBank
{
public:
friend int total();
CBank()
{
int w=10000;
balance=w;
}
private:
int balance;
};
class BBank
{
public:
friend int total();
BBank()
{
int w=20000;
balance=w;
}
private:
int balance;
};
class GBank
{
public:
friend int total();
GBank() {
int w=30000;
balance=w;
}
private:
int balance;
};
int total(){
CBank a;
BBank b;
GBank c;
int balance;
balance=a.balance+b.balance+c.balance;
return balance;
}
void main(){
int cunqian;
cunqian=total();
cout<<"The saving number is: "<<cunqian<<endl;
}
3.
//head.h
class line;
class point
{
public:
point(int x,int y);
friend double dist(point &p,line &l);
private:
int x,y;
};
class line
{
public:
line(int a,int b,int c);
friend double dist(point &p,line &l);
private:
int a,b,c;
};
//PL.cpp
#include"head.h"
#include<iostream>
#include<cmath>
using namespace std;
point::point(int x1,int y1){
x=x1;y=y1;
}
line::line(int a1,int b1,int c1){
a=a1;b=b1;c=c1;
}
double dist(point &p,line &l){
cout<<"x="<<p.x<<",y="<<p.y<<endl;
cout<<"a="<<l.a<<",b="<<l.b<<",c="<<l.c<<endl;
double t=sqrt(l.a*l.a+l.b*l.b);
return abs(l.a*p.x+l.b*p.y+l.c)/t;
}
//lab04_3.cpp
#include"head.h"
#include<iostream>
#include<cmath>
using namespace std;
int main(){
point p1(4,5);
line l1(3,4,5);
cout<<dist(p1,l1)<<endl;
return 0;
}
五、思考题
1.类与结构体的区别?
答:在c里结构体只能定义数据成员,不能有函数成员,但可以定义函数指针来模拟函数。
在C++中,结构体和类除了默认的访问方式和默认的继承方式不同外,其它都一样。
结构体默认是公有,类默认是私有
2.类的静态成员函数可以通过对象名调用吗?
答:静态成员函数可以直接访问该类的静态数据和函数成员。
而访问非静态成员,必须通过对象名。
六、实验小结。