#ifndef MYTEST_H_INCLUDED
#define MYTEST_H_INCLUDED
#include <iostream>
class Student2;
class Student1
{
private:
friend class Student2;
std::string m_name;
int m_age;
float m_height;
int m_score;
public:
Student1():m_name("Student1"), m_age(24), m_height(167.5), m_score(85) {}
void GetFriend(Student2 &);
void ShowInfomation();
};
class Student2
{
private:
friend class Student1;
std::string m_name;
int m_age;
float m_height;
int m_score;
public:
Student2():m_name("Student2"), m_age(25), m_height(175.5), m_score(90) {}
void GetFriend(Student1 &);
void ShowInfomation();
};
inline void Student1::GetFriend(Student2 & stu)
{
std::cout << m_name << "\'s good friend is:" << stu.m_name << std::endl;
std::cout << stu.m_name << "\'s information:\n\t\t\t";
stu.ShowInfomation();
}
inline void Student1::ShowInfomation()
{
std::cout << "age:" << m_age << "\n\t\t\t"
<< "height:" << m_height << "\n\t\t\t"
<< "score:" << m_score << std::endl;
}
inline void Student2::GetFriend(Student1 & stu)
{
std::cout << m_name << "\'s good friend is:" << stu.m_name << std::endl;
std::cout << stu.m_name << "\'s information:\n\t\t\t";
stu.ShowInfomation();
}
inline void Student2::ShowInfomation()
{
std::cout << "age:" << m_age << "\n\t\t\t"
<< "height:" << m_height << "\n\t\t\t"
<< "score:" << m_score << std::endl;
}
#endif
#include "mytest.h"
int main()
{
Student1 stu1;
Student2 stu2;
stu2.GetFriend(stu1);
stu1.GetFriend(stu2);
return 0;
}
- 执行结果: