编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛  
全能 ASP / PHP / ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
 13 12
发新话题
打印

友元函数问题

友元函数问题

友元函数不能访问类的私有变量是怎么回事啊?    帮忙分析下原因

TOP

可以
学习需要安静。。海盗要重新来过。。

TOP

但是6.0对友员支持不好..你要用命名空间隔忾
学习需要安静。。海盗要重新来过。。

TOP

要全部贴出来吗?

TOP

恩..贴出来看看啊
学习需要安静。。海盗要重新来过。。

TOP

是用命名空间,我是初学者,还在试;

TOP

头文件 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;
}

TOP

我在线等着啊...谢谢你的帮忙呢.

TOP

#include<iostream>
using namespace std;
namespace a{
class demo
{
private:
     static int i;
     int j;
public:
    demo():j(0){};
    friend ostream& operator<<(ostream & ,demo &);
};
int demo::i=0;
ostream& operator<<(ostream & os,demo &a)
{
    os<<a.j<<endl;
    return os;
}
}
int main(void)
{
    a::demo d;
    cout<<d;
    return 0;
}
学习需要安静。。海盗要重新来过。。

TOP

#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
#include<iostream>
#include"friends.h"
using namespace std;
//using namespace friends;
friends::Box::Box(double rlength,double rwidth,double rheight):length(rlength),width(rwidth),height(rheight){}
friends::Box::~Box()
{
    cout<<"析构"<<endl;
}
void friends::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 friends::BoxSurface(const Box& other1)
{
    return  other1.height+other1.width+other1.length;
}
学习需要安静。。海盗要重新来过。。

TOP

 13 12
发新话题