首页 > 代码库 > 2000行之mother、father、child

2000行之mother、father、child

//child.h
#ifndef CHILD_H
#define CHILD_H
#include <string>
using namespace std;
class Mother;
class Father;
class Child
{
public:
    Mother *mama;
    Father *baba;
    Child();
    string name;
    void answer();
    void callParent();
};

#endif // CHILD_H
//child.cpp
#include "child.h"
#include "mother.h"
#include "father.h"
#include <iostream>
using namespace std;
Child::Child():name("xiao Hua")
{
    //mama=new Mother;
    //baba=new Father;
}

void Child::answer(){
    cout<<endl<<name<<" is here!";
}

void Child::callParent()
{
    cout<<endl<<"I am calling my father!";
    cout<<endl<<"Father is not here!";
    //mama->answer();
    //baba->answer();
}
//mother.h
#ifndef MOTHER_H
#define MOTHER_H
#include <string>
using namespace std;
class Child;
class Mother
{
    Child *child;
    string name;
public:
    Mother();
    void callChild();
    void answer();
};

#endif // MOTHER_H
//mother.cpp
#include "mother.h"
#include "child.h"
#include <iostream>
using namespace std;
Mother::Mother():name("yyyyy")
{
    child=new Child;
}
void Mother::callChild(){
    cout<<endl<<"mama is calling my child!";
    child->answer();
}
void Mother::answer(){
    cout<<endl<<name<<" is here waiting for you!";
}
//father.h
#ifndef FATHER_H
#define FATHER_H

#include <string>
using namespace std;
class Child;
class Father
{
public:
    Father();
    string name;
    Child *child;
    void callChild();
    void answer();
};

#endif // FATHER_H
//father.cpp
#include "father.h"
#include "child.h"
#include <iostream>
using namespace std;
Father::Father():name("Lao Hua")
{
    child=new Child;
}

void Father::callChild(){
    cout<<endl<<"baba am calling my child!";
    child->answer();
}

void Father::answer(){
    cout<<endl<<name<<" is here waiting for you!";
}
//main.cpp
#include <QCoreApplication>
#include <iostream>
#include "father.h"
#include "child.h"
#include "mother.h"
using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Child son;
    Father baba1;
    Mother mama1;
    son.callParent();
    baba1.callChild();
    mama1.callChild();
    cout<<endl;
    return a.exec();
}

 

2000行之mother、father、child