c/c++基础(十九) 友元

本文不赘述友元的概念,直接通过代码示例介绍C/C++中的友元函数和友元类。首先展示友元函数的使用,然后讲解如何声明和定义友元类。接着探讨友元作为类的成员函数的情况,并提供相应的M文件声明与定义。最后,给出了测试类的代码,帮助读者更好地理解和应用友元。参考了多个技术博客进行深入学习。
摘要由CSDN通过智能技术生成

概念的东西不讲了,直接上代码:


1.友元函数:


Point.类的声明与定义:

#pragma once
#ifndef _POINT_H
#define _POINT_H

class Point
{
	public:
		void getXY();
		friend double distanceXY(Point &a,Point &b);

	public:
		Point(double x,double y);
		~Point(void);

	private:
		double x;
		double y;
};

#endif

#include "stdafx.h"
#include "Point.h"
#include<cmath>
#include<iostream>
using namespace std;

Point::Point(double x,double y)
{
	this->x=x;
	this->y=y;
}


Point::~Point(void)
{
}

void Point::getXY()
{
	cout<<"x: "<<x<<" y: "<<y<<endl;
}

double distanceXY(Point &a,Point &b)
{
	double dx=a.x-b.x;
	double dy=a.y-b.y;
	return sqrt(dx*dx+dy*dy);
}

测试文件TestProject.cpp:

#include "stdafx.h"
#include "Point.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	Point p1(3.0,4.0),p2(5,6);
	p1.getXY();
	p2.getXY();

	double dis=distanceXY(p1,p2);
	cout<<"distance is : "<<dis<<endl;
	return 0;
}

输出:



二,友元类:


类A的声明与定义:

#pragma once
class A
{
	public:
		void getXY();
		friend class B;

	public:
		A(double x,double y);
		~A(void);

	private:
		double x;
		double y;
};


#include "stdafx.h"
#include "A.h"
#include<cmath>
#include<iostream>
using namespace std;

A::A(double x,double y)
{
	this->x=x;
	this->y=y;
}


A::~A(void)
{
}

void A::getXY()
{
	cout<<"A x: "<<x<<"	y: "<<y<<endl;
}

类B的声明与定义:

#pragma once
#include "A.h"
class B
{

public:
	B(void);
	~B(void);

public:
	void setX(A &a);
	void setY(A &b);
};

#include "stdafx.h"
#include "B.h"


B::B(void)
{
}


B::~B(void)
{
}

void B::setX(A &a)
{
	a.x=0;
	a.getXY();
}

void B::setY(A &a)
{
	a.y=0;
	a.getXY();
}

测试类如下:

#include "stdafx.h"
#include "A.h"
#include "B.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	A a(5.0,5.0);
	B b;
	b.setX(a);
	b.setY(a);
	
	return 0;
}

结果如下:



三:友元是类的成员函数


M文件声明与定义:

#pragma once
#ifndef _M_H
#define _M_H

#include "N.h"
class M
{
	public:
		void getXY();
		friend void N::print(M& m);

	public:
		M(double x,double y);
		~M(void);

	private:
		double x;
		double y;

	private:
		void getXY2();
};

#endif

#include "stdafx.h"
#include "M.h"
#include<cmath>
#include<iostream>
using namespace std;

M::M(double x,double y)
{
	this->x=x;
	this->y=y;
}


M::~M(void)
{
}

void M::getXY()
{
	cout<<"M getXY x: "<<x<<"	y: "<<y<<endl;
}

void M::getXY2()
{
	cout<<"我是M中的私有方法"<<endl;
}

N的声明与定义:


#pragma once
#ifndef _N_H
#define _N_H

class M;
class N
{

public:
	N(void);
	~N(void);

public:
	void print(M &m);
	void print2(M &m);
};

#endif


#include "stdafx.h"
#include "N.h"
#include "M.h"

N::N(void)
{
}


N::~N(void)
{
}

void N::print(M &m)
{
	m.getXY();
	m.getXY2();
}

void N::print2(M &m)
{
	//m.getXY();//--------这里如果打开注释会报错
	//m.getXY2();
}



测试类:

#include "stdafx.h"
#include "M.h"
#include "N.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	M m(5.0,5.0);
	N n;
	n.print(m);
	n.print2(m);
	return 0;
}

打印结果:



参考文章:http://www.cnblogs.com/BeyondAnyTime/archive/2012/06/04/2535305.html

                     http://baike.baidu.com/view/1066547.htm?fr=aladdin

                     http://www.cnblogs.com/uniqueliu/archive/2011/08/02/2125590.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值