|
发表于 2009-11-4 00:27:19
|
显示全部楼层
IP:江苏扬州
我改了,还是不行
我刚开始学,对这方面的语法还不太熟,能请你说的详细点吗?
#include<iostream>
using namespace std;
class Boat
{
public:
Boat(int w=0) {weight=w;}
void coutB() {cout<<"Boat weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为boat的友元函数
private:
int weight;
};
class Car
{
Car(int w=0) {weight=w;}
void coutB() {cout<<"Car weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为car的友元函数
private:
int weight;
};
void totalWeight(Boat &p1,Car &p2)
{
int x,y;
x=p1.weight;
y=p2.weight; //通过对象访问其weight
cout<<"total weight="<<x+y<<endl;
}
void main()
{
Boat boat(120);
Car car(110); //生成对象
totalWeight(boat,car);
}
还有一个问题,就是友元函数对类的访问只能通过参数传递吗? |
|