读C++ Primer 写了个小例子——友员类的操作
#include
<
iostream.h
>
class A
... {
public:
A()
...{
x=5;
}
friend class B;
private:
int x;
} ;
class B
... {
public:
void disp1(A temp)
...{
temp.x++;
cout<<temp.x<<endl;
}
} ;
#include < iostream >
class a;
class b
... {
public:
int getData();
protected:
int x;
} ;
int b::getData()
... {
return x;
}
class a
... {
friend b;
public:
int getA();
protected:
int y;
} ;
int a::getA()
... {
return y;
}
// test.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
#include " Header1.h "
#include < string .h >
// #include <afxwin.h>
using namespace std;
int _tmain( int argc, _TCHAR * argv[])
... {
char first[] = "12345678901234567890";
char second[] = "12345678901234567891";
int result = memcmp( first, second, 19 );
if( result < 0 )
printf( "First is less than second. " );
else if( result == 0 )
printf( "First is equal to second. " );
else
printf( "First is greater than second. " );
cout<<"nihao"<<endl;
b v;
a v1;
cout<<v1.getA()<<endl;
cout<<v.getData()<<endl;
return 0;
}
class A
... {
public:
A()
...{
x=5;
}
friend class B;
private:
int x;
} ;
class B
... {
public:
void disp1(A temp)
...{
temp.x++;
cout<<temp.x<<endl;
}
} ;
#include < iostream >
class a;
class b
... {
public:
int getData();
protected:
int x;
} ;
int b::getData()
... {
return x;
}
class a
... {
friend b;
public:
int getA();
protected:
int y;
} ;
int a::getA()
... {
return y;
}
// test.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
#include " Header1.h "
#include < string .h >
// #include <afxwin.h>
using namespace std;
int _tmain( int argc, _TCHAR * argv[])
... {
char first[] = "12345678901234567890";
char second[] = "12345678901234567891";
int result = memcmp( first, second, 19 );
if( result < 0 )
printf( "First is less than second. " );
else if( result == 0 )
printf( "First is equal to second. " );
else
printf( "First is greater than second. " );
cout<<"nihao"<<endl;
b v;
a v1;
cout<<v1.getA()<<endl;
cout<<v.getData()<<endl;
return 0;
}