Thread: [Question] Include Files Inheritance

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    2

    [Question] Include Files Inheritance

    I tried to find through the forum and online. Also I have read through the header files section of cprogramming.com but could not find the answer. I hope someone here could shed some light to me.

    myheader.h
    Code:
    void myFunction(int x);
    myheader.cpp
    Code:
    #include "myheader.h"
    void myFunction(int x)
    {
        [do something here]
    }
    myheader2.h
    Code:
    #include "myheader.h"
    
    void myFunction2(int x);
    myheader2.cpp
    Code:
    #include "myheader.h"
    #include "myheader2.h"
    
    void myFunction2(int x)
    {
        myFunction(12);
        [do something here]
    }

    main.cpp
    Code:
    #include "myheader2.h"
    
    myFunction(12);
    myFunction2(12);
    I have 2 questions, they are in red.

    1. Where to include the header file of "myheader.h". Is it in "myheader2.h" or "myheader2.cpp" or both?

    2. When I include "myheader2.h" in "main.cpp", will it automatically 'inherit' the "myheader.h"?

    Thanks in advance.
    Last edited by superblacksmith; 12-15-2018 at 10:21 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. Ask yourself: do I use what was declared in this header in this file? It seems that you call myFunction from myheader2.cpp, so you should include myheader.h there rather than in myheader2.h

    2. Yes if you include myheader.h in myheader2.h (it isn't "inherited" but included), which is why you should have inclusion guards to prevent problems where the same header could be included more than once, indirectly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    2
    Quote Originally Posted by laserlight View Post
    1. Ask yourself: do I use what was declared in this header in this file? It seems that you call myFunction from myheader2.cpp, so you should include myheader.h there rather than in myheader2.h

    2. Yes if you include myheader.h in myheader2.h (it isn't "inherited" but included), which is why you should have inclusion guards to prevent problems where the same header could be included more than once, indirectly.
    1. Thanks. Understood.

    2. Thanks. Will check on include guards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-30-2013, 01:11 AM
  2. Replies: 1
    Last Post: 11-06-2011, 06:20 PM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. about #include files and header files
    By bulletbutter in forum C++ Programming
    Replies: 9
    Last Post: 04-18-2008, 10:24 AM
  5. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM

Tags for this Thread