



#include "iostream.h"
class B;
class A
{
int x;
public:
A(){}
A(int x){this->x=x;}
void set(B);
int get(){return x;}
};
class B
{
int y;
public:
B(int y){this->y=y;}
friend A;
};
void A::set(B b){x=b.y;}
void main()
{
A a(1);
B b(2);
cout<<a.get()<<endl;
a.set(b);
cout<<a.get()<<endl;
}
【2】
#include<iostream.h>
#include<string.h>
class Teacher;
class Student
{
private:
char name[10];
public:
Student(char n[]){strcpy(name,n);}
friend void display(Student,Teacher);
};
class Teacher
{
private:
char name[10];
public:
Teacher(char n[]){strcpy(name,n);}
friend void display(Student,Teacher);
};
void display(Student s,Teacher t)
{
cout<<"the student is:"<<s.name<<endl;
cout<<"the teacher is:"<<t.name<<endl;
}
void main()
{
Student s("Li Hui");
Teacher t("Wang Ping");
display(s,t);
}