main.cpp
#include <QCoreApplication> #include "father.h" #include <iostream> using namespace std; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Father *baba=new Father(); baba->callchild(); Child *haizi=new Child(baba); haizi->callFather(); Mother *mama1=new Mother(baba); mama1->callFather(); Mother *mama2=new Mother(haizi); mama2->callchild(); Child *haizi2=new Child(mama1); haizi2->classMother(); cout<<endl; return a.exec(); } father.cpp #include "father.h" #include <iostream> Father::Father()\ :name("Lao Hua") { } Father::Father(Child *child):child(child){} Father::Father(Mother *mother):mother(mother){} void Father::callchild(){ cout<<endl<<"I am calling my child."; child->answer(); } void Father::callMother(){ cout<<endl<<"I am calling my wife!"; mother->answer(); } void Father::answer(){ cout<<endl<<name<<"is waiting for you."; } father.h #ifndef FATHER_H #define FATHER_H #include <string> #include "child.h" #include "mother.h" using namespace std; class Child; class Mother; class Father { public: Father(); Father(Child *child); Father(Mother *mother); string name; Child *child; Mother *mother; void callchild(); void callMother(); void answer(); }; #endif // FATHER_H mother.cpp #include "mother.h" #include <iostream> Mother::Mother() :name("Li") { } Mother::Mother(Child *child):child(child){} Mother::Mother(Father *father):father(father){} void Mother::callchild(){ cout<<endl<<"I am calling my child!"; child->answer(); } void Mother::callFather(){ cout<<endl<<"I am calling my husband!"; father->answer(); } void Mother::answer(){ cout<<endl<<name<<" is here!"; } mother.h #ifndef MOTHER_H #define MOTHER_H #include <string> #include "father.h" #include "child.h" using namespace std; class Father; class Child; class Mother { public: Mother(); Mother(Father *father); Mother(Child *child); string name; Child *child; Father *father; void callFather(); void callchild(); void answer(); }; #endif // MOTHER_H child.cpp #include "child.h" #include <iostream> Child::Child() :name("wang") { } Child::Child(Father *father):father(father){} Child::Child(Mother *mother):mother(mother){} void Child::answer(){ cout<<endl<<name<<"is here!"; } void Child::classMother(){ cout<<endl<<"I am calling my mother!"; mother->answer(); } void Child::callFather(){ cout<<endl<<"I am calling my father!"; father->answer(); } child.h #ifndef CHILD_H #define CHILD_H #include <string> #include "father.h" #include "mother.h" using namespace std; class Father; class Mother; class Child { public: Child(); Child(Father *father); Child(Mother *mother); string name; Father *father; Mother *mother; void answer(); void callFather(); void classMother(); }; #endif // CHILD_H