C++中前置声明

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值