头文件 friends.h
#ifndef FRIENDF
#define FRIENDF
namespace friends
{
class Box
{
public:
Box(double rlength=1.0,double rwidth=1.0,double rheight=1.0);
~Box();
void Show();
//friend double BoxSurface(const Box& other1,const Box& other2);
friend double BoxSurface(const Box& other1);
private:
double length;
double width;
double height;
};
}
#endif
定义文件:
friends.cpp
#include<iostream>
#include"friends.h"
using namespace std;
using namespace friends;
Box::Box(double rlength,double rwidth,double rheight):length(rlength),width(rwidth),height(rheight){}
Box::~Box()
{
cout<<"析构"<<endl;
}
void Box::Show()
{
cout<<"长:"<<length<<endl
<<"宽:"<<width<<endl
<<"高;"<<height<<endl;
}
主文件:
#include<iostream>
#include"friends.h"
using namespace std;
using namespace friends;
int main()
{
Box aa(10.0,20.0,30.0);
aa.Show();
Box bb(40.0,50.0,60.0);
bb.Show();
cout<<"两高: "<<BoxSurface(aa)<<endl;
return 0;
}
double BoxSurface(const Box& other1)
{
return
other1.height+other1.width+other1.length;
}