在 C++ 中构建简单函数以继承 3 类
Building simple function to inherit 3 classes in C++
我创造了 3 个 classes - 祖母、母亲和女儿。我写了一段代码,使女儿 class 继承自母亲 class,而母亲 class 继承自祖母 calss。
GrandMother.h :-
#ifndef GRANDMOTHER_H
#define GRANDMOTHER_H
class GrandMother
{
public:
GrandMother();
~GrandMother();
};
#endif // GRANDMOTHER_H
Mother.h :-
#ifndef MOTHER_H
#define MOTHER_H
class Mother: public GrandMother
{
public:
Mother();
~Mother();
};
#endif // MOTHER_H
Daughter.h :-
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter: public Mother
{
public:
Daughter();
~Daughter();
};
#endif // DAUGHTER_H
GrandMother.cpp :-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
GrandMother::GrandMother()
{
cout << "Grand Mother Constructor!!" << endl;
}
GrandMother::~GrandMother()
{
cout << "Grand Mother Deconstroctor" << endl;
}
Mother.cpp :-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
Mother::Mother()
{
cout << "Mother Constructor!!" << endl;
}
Mother::~Mother()
{
cout << "Mother Deconstroctor" << endl;
}
Daughter.cpp:-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
Daughter::Daughter()
{
cout << "Daughter Constructor!!" << endl;
}
Daughter::~Daughter()
{
cout << "Daughter Deconstroctor" << endl;
}
main.cpp :-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
int main(){
//GrandMother granny;
//Mother mom;
Daughter baby;
}
当我 运行 代码时,它给了我以下错误:-
error: expected class-name before '{' token
任何人都可以告诉我我的代码的哪一部分是错误的。
您的 header 不是 self-contained,因此您必须按正确的顺序包含它们:
#include "GrandMother.h"
#include "Mother.h"
#include "Daughter.h"
但它很脆弱。
正确的方法是使 header 自包含:
#ifndef GRANDMOTHER_H
#define GRANDMOTHER_H
class GrandMother
{
public:
GrandMother();
~GrandMother();
};
#endif // GRANDMOTHER_H
#ifndef MOTHER_H
#define MOTHER_H
#include "GrandMother.h"
class Mother: public GrandMother
{
public:
Mother();
~Mother();
};
#endif // MOTHER_H
#ifndef DAUGHTER_H
#define DAUGHTER_H
#include "Mother.h"
class Daughter: public Mother
{
public:
Daughter();
~Daughter();
};
#endif // DAUGHTER_H
我创造了 3 个 classes - 祖母、母亲和女儿。我写了一段代码,使女儿 class 继承自母亲 class,而母亲 class 继承自祖母 calss。
GrandMother.h :-
#ifndef GRANDMOTHER_H
#define GRANDMOTHER_H
class GrandMother
{
public:
GrandMother();
~GrandMother();
};
#endif // GRANDMOTHER_H
Mother.h :-
#ifndef MOTHER_H
#define MOTHER_H
class Mother: public GrandMother
{
public:
Mother();
~Mother();
};
#endif // MOTHER_H
Daughter.h :-
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter: public Mother
{
public:
Daughter();
~Daughter();
};
#endif // DAUGHTER_H
GrandMother.cpp :-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
GrandMother::GrandMother()
{
cout << "Grand Mother Constructor!!" << endl;
}
GrandMother::~GrandMother()
{
cout << "Grand Mother Deconstroctor" << endl;
}
Mother.cpp :-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
Mother::Mother()
{
cout << "Mother Constructor!!" << endl;
}
Mother::~Mother()
{
cout << "Mother Deconstroctor" << endl;
}
Daughter.cpp:-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
Daughter::Daughter()
{
cout << "Daughter Constructor!!" << endl;
}
Daughter::~Daughter()
{
cout << "Daughter Deconstroctor" << endl;
}
main.cpp :-
#include<iostream>
#include "Mother.h"
#include "Daughter.h"
#include "GrandMother.h"
using namespace std;
int main(){
//GrandMother granny;
//Mother mom;
Daughter baby;
}
当我 运行 代码时,它给了我以下错误:-
error: expected class-name before '{' token
任何人都可以告诉我我的代码的哪一部分是错误的。
您的 header 不是 self-contained,因此您必须按正确的顺序包含它们:
#include "GrandMother.h"
#include "Mother.h"
#include "Daughter.h"
但它很脆弱。
正确的方法是使 header 自包含:
#ifndef GRANDMOTHER_H
#define GRANDMOTHER_H
class GrandMother
{
public:
GrandMother();
~GrandMother();
};
#endif // GRANDMOTHER_H
#ifndef MOTHER_H
#define MOTHER_H
#include "GrandMother.h"
class Mother: public GrandMother
{
public:
Mother();
~Mother();
};
#endif // MOTHER_H
#ifndef DAUGHTER_H
#define DAUGHTER_H
#include "Mother.h"
class Daughter: public Mother
{
public:
Daughter();
~Daughter();
};
#endif // DAUGHTER_H