Thread: trouble understanding the source file structure

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    trouble understanding the source file structure

    Hello all,

    I'm having trouble understanding how a project source files are structured. I understand when it comes to the use of classes.... I think.

    An header file includes the class data members definition plus any member functions definitions I may deem appropriate to be treated as inline function members. The corresponding source file includes this header and defines all other function members. When I need to reuse this class, I add both files to my project and include the header file on any source file that declares an object of that class. So...

    Code:
    ---------- myClass.h ---------------
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    class myClass {
        int a;
    public:
        void seta(int x);
        int geta() { return a; };
    };
    #endif
    
    --------- myClass.cpp --------------
    #include "myClass.h"
    
    void myClass::seta(int x) {
        a = x;
    }
    
    ------------ main.cpp ---------------
    #include "myClass.h"
    
    int main()
    {
        /*.......*/
    }
    But what then when my main.cpp file grows too large and I start considering breaking it? I have a minimal understanding of function prototypes. I'm afraid Addison Wesley's C++ Primer 4th Edition, being otherwise an excellent book, fails to clearly explain this concept.

    When do I need to prototype a function? Why? And where? To ease your explanation, let's imagine my main.cpp has a couple of functions I want to move to a source file of their own. int doThis(int x) and void doThat(int x).

    Thanks for your time.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A function may only be defined once, that is, you can only give the function a body a single time:
    Code:
    // mystuff.cpp
    
    int doThis ( int x )
    {
      // This is the body
    }
    
    void doThat ( int x )
    {
      // Brackets mean a function definition
    }
    Now, in your main source you want to use these functions, but how do you know their names? How do you know their parameters and return values? Or more specifically, how does the compiler know that you're doing something legal? A function definition, or prototype.
    Code:
    // main.cpp
    #include <iostream>
    
    int doThis ( int x ); // I'm a function declaration
    void doThat ( int x ); // Also called a prototype
    
    int main()
    {
      std::cout<< doThis ( 1 ) <<'\n';
      doThat ( 0 );
    }
    Now when you link main.cpp and mystuff.cpp together, the linker more or less tosses all of the separate files into a single file:
    Code:
    int doThis ( int x ); // I'm a function declaration
    void doThat ( int x ); // Also called a prototype
    
    int main()
    {
      std::cout<< doThis ( 1 ) <<'\n';
      doThat ( 0 );
    }
    
    int doThis ( int x )
    {
      // This is the body
    }
    
    void doThat ( int x )
    {
      // Brackets mean a function definition
    }
    But before that happens, main.cpp doesn't know that doThis and doThat exist (they're in another file, so how can it?), so you need to declare them or main.cpp won't compile. You'll get an error about an undefined name.

    Now let's say you want to call these functions in multiple files. You can declare them in all of the files because that's perfectly legal, but it's easier to just put the declarations in a header file and include it wherever you need it:
    Code:
    // mystuff.h
    
    int doThis ( int x ); // I'm a function declaration
    void doThat ( int x ); // Also called a prototype
    Code:
    // mystuff.cpp
    
    int doThis ( int x )
    {
      // This is the body
    }
    
    void doThat ( int x )
    {
      // Brackets mean a function definition
    }
    Code:
    // main.cpp
    #include <iostream>
    #include "mystuff.h"
    
    int main()
    {
      std::cout<< doThis ( 1 ) <<'\n';
      doThat ( 0 );
    }
    It breaks down like this: main.cpp gets declarations by including mystuff.h and gets definitions by linking with mystuff.cpp.
    Last edited by Prelude; 05-26-2006 at 06:46 PM.
    My best code is written with the delete key.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It is a good idea to separate your program into managable files.

    Very important sentence. When you think about organizing a project, not necessarily every function or every class will need to have its own source and header files. What is more important is deviding your project into modules, or chunks where each chunk does a specific thing for the program -- and they get their own files.

    For all non-trivial programs, functions should have prototypes. Where these prototypes go depend on what the modules are.
    Last edited by whiteflags; 05-26-2006 at 02:44 PM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Perfect! Thanks Prelude. Your breakdown was great. It explained both with and without the use of an header file. And the why!

    Got it!

    Thanks too Citizen. Will keep it in mind.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    int doThis ( int x ); // I'm a function definition
    Did you mean: "I'm a function declaration." ?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    *stealthily edits her post*

    >Did you mean: "I'm a function declaration." ?
    I know not of this mistake to which you refer, good sir. I'm infallible, as everyone here very well knows.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to build, run Boland C++ 6 source file via VS2005?
    By userpingz in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2009, 03:25 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM