So I can get everything to compile, and I understand (I think) how include guards prevent the same definitions from being pulled twice, but what I don't know is if my header file should also include the header file for the class it uses, if my class file already includes it.
Child.hpp
// Child.hpp
#ifndef CHILD_H
#define CHILD_H
class Child {
public:
Child();
};
#endif
Child.cpp
// Child.cpp
#include "Child.hpp"
Child::Child() {
// example
}
Parent.hpp
Should I also include Child.hpp here even though it's already included in Parent.cpp? I understand the header guard would prevent Child being defined twice, but is it considered best practice? Or should I exclusively include Child.hpp here?
// Parent.hpp
#ifndef PARENT_H
#define PARENT_H
class Parent {
public:
Parent();
Parent(Child child);
};
#endif
Parent.cpp
// Parent.cpp
#include "Child.hpp"
#include "Parent.hpp"
int main() {
Parent parent;
return 0;
}
Parent::Parent() {
// example
}
Parent::Parent(Child child) {
// example
}
We were just given one example in class, and it essentially said that in Parent.cpp
it should include Parent.hpp
(makes sense) and Child.hpp
.
It would seem I would want to include Child.hpp
in Parent.hpp
as the Parent
class relies on the Child
class, but either way Child.hpp
gets included.