首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

一个种的友元类能访问类的私有数据

2012-11-08 
一个类的友元类能访问类的私有数据// test.cpp : Defines the entry point for the console application./

一个类的友元类能访问类的私有数据

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CAnimal
{
public :
 int m_sex;
public:
 void SetSex(int sex)
 {
  m_sex = sex;
 }
 //******************************
 //定义友元函数
 friend int GetSex(CAnimal& am);

 ///********************************
 //声明友类
 friend class CCat;
};

int GetSex(CAnimal& am)
{
 return am.m_sex ;
}

class CCat
{
public:
 int GetSex(CAnimal& am)
 {
  return am.m_sex ;
 }
};


int main(int argc, char* argv[])
{
 CAnimal animal;
 CCat  kitty;
 animal.m_sex = 'M';

 int sex =kitty.GetSex(animal);
 return 0;
}

热点排行