1. 程式人生 > >C++實驗4 友元

C++實驗4 友元

五、實驗結果及分析

#include<iostream>

using namespace std;

class CPU;

class CpuFrdMemb

{public:

void gf(int a,int b){cout<<"he="<<a+b<<endl;}

void play( const CPU & k);

};

class CPU

{

friend class CpuFriend;

private:

int x;

public:

int gf(int w){x=w;return w;}

friend void PrintCpuFrd(CPU & a);

friend void CpuFrdMemb::play(const CPU & k);

};

class CpuFriend

{public:

      void Set(int i) {

  F.x =i;

  int s;

  s=i*i;

cout<<"s="<<s<<endl;

  }

private:

   CPU F;

};

 void PrintCpuFrd(CPU & a)

 {cout<<"x="<<a.x<<endl;

 };

 void CpuFrdMemb::play( const CPU & k){int p;p=(k.x)*100;cout<<p<<endl;}

void main()

{int x=10;

CPU a;

a.gf(x);

PrintCpuFrd(a);

CpuFriend c;

c.Set(x);

CpuFrdMemb d;

d.gf(5,6);

d.play(a);

getchar();

}