#include<iostream>
using namespace std;
#include"exp3_1.h"
int main()
{
Girl g("sss",20);
Boy b("sll",18);
g.Print();
b.Print();
g.VisitBoy(b);
// b.VisitGirl(g);
return 0;
}
//头文件exp3_1.h
#include<iostream>
#include<string>
using namespace std;
class Boy; //向前引用
class Girl
{
char name[20]; //类数据成员默认私有
int age;
public:
Girl(char N[],int A); //构造函数
~Girl() //析构函数
{
cout<<"Girl destructing...\n";
}
void Print();
void VisitBoy(Boy &); //在其中访问Boy类的私有成员
};
class Boy
{
char name[20];
int age;
friend Girl; //将Girl类作为Boy类的友元类
public:
Boy(char N[],int A);
~Boy()
{
cout<<"Boy destructing...\n";
}
void Print();
// void VisitGirl(Girl &);
};
//Girl类的成员函数
Girl::Girl(char N[],int A) //Girl类的构造函数的实现代码
{
strcpy(name,N);
age=A;
cout<<"Girl constructing...\n";
}
void Girl::Print() //Girl类的输出函数实现代码
{
cout<<"Girl's name: "<<name<<endl;
cout<<"Girl's age: "<<age<<endl;
}
void Girl::VisitBoy(Boy &boy) //通过Girl类中的VisitBoy函数访问Boy类中的数据成员
{
cout<<"Boy's name: "<<boy.name<<endl;
cout<<"Boy's age: "<<boy.age<<endl;
}
//Boy类的成员函数
Boy::Boy(char N[],int A)
{
strcpy(name,N);
age=A;
cout<<"Boy constructing...\n";
}
void Boy::Print()
{
cout<<"Boy's name: "<<name<<endl;
cout<<"Boy's age: "<<age<<endl;
}
/*
Boy::VisitGirl(Girl &girl)
{
cout<<"Girl's name: "<<girl.name<<endl;
cout<<"Girl's age: "<<girl.age<<endl;
}
*/
交互式爱情