Thread: Classes with seperate .h and .cpp file

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Classes with seperate .h and .cpp file

    File: Test.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    Test()
    {
          name = "Abc";
         cout << name;
    }
    Headerfile:
    Code:
    #ifndef TEST_H
    #define TEST_H
    
    class Test                   
    {
    protected:
        char* name;                
    
    public:
        Test();
    };
    
    #include "..\Test.cpp"  //Path is correct
    
    #endif
    My Main
    Code:
    #include <iostream>
    #include "..\HeaderFiles\Test.h" //Path is correct.
    
    int main()
    {
    cout << "Hello World"; // :D
    Test t = new Test();
    
    return 0;
    }
    If I put everything in one file (one header) it works, to then include this into the main.cpp:
    Code:
    #ifndef GREN_H
    #define GREN_H
    
    class Test
    {
    protected:
        char* name;
    public:
        Test();
    };
    
    Test::Test()
    {
    
    }
    #endif

    Edit:
    I got this to work! But:
    Why can I not do it the way I want to: putting the .cpp file into the header, instead of putting the header into the .cpp file. Am I doing something wrong, or this cannot be done depending on the compiler ("Thats how the compiler is built")?

    Writing the top then import the bottom, or writing the bottom to later import the top, should not make a difference?

    I find this way more logical for me, so really wanna pull this off!
    Last edited by ManyTimes; 04-08-2010 at 05:55 PM.

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    The header is a preview of what is defined in the code.

    So a .h is a PREVIEW of what is coming - we don't need the code here, bit we know it is defined elsewhere.

    The .cpp is the actual working code.

    Think of how global functions work you define them at the top of the code then elaborate and write their code at the bottom of the code block. (assuming you are doing them above and below the int main() block).

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    if you include your cpp file in your .h file you will cause the compiler to compile that code for a second time in it's build process.
    As most build process's compile each .cpp file into .obj files.
    a .cpp file may not have changed since the last build and is therefore skipped from compilation.
    by including your .cpp file into your .h file you cause the compiler to compile all the code even the code in the .obj file is still valid.
    To be honest I think you may have problems with builds in larger projects.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Depends how you call the compiler. You do not need to build objects either.
    Last edited by MK27; 04-09-2010 at 10:04 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting template classes between .h and .cpp files
    By Neo1 in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2010, 05:47 AM
  2. extern Global array in seperate .h file
    By rocketman03 in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:03 AM
  3. Using classes in another .cpp file
    By ryeguy in forum C++ Programming
    Replies: 13
    Last Post: 12-30-2007, 11:50 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  5. Replies: 4
    Last Post: 05-28-2002, 04:14 AM