Thread: C++ Object Oriented and Multiple Files Basics?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    C++ Object Oriented and Multiple Files Basics?

    Hi all,

    I'm trying to code a very very simple program with multiple files (like for Java, i have many classes, all in seperate files)

    How do I achieve that?

    Do I have Main.cpp, SomeObject.h, SomeObject,cpp, AnotherObject.h, and AnotherObject.cpp?

    Do I also need a header file for my main program?

    And how do I link all of them together? Do I have to put #include SomeObject.h, #include AnotherObject.h in my Main.cpp? Or do I just compile all of them together and they will be linked?

    If I were, to say put these files into my own defined namespace, do I still use #include SomeObject.h, or do I type using SomeNamespace::SomeObject ??

    Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Have all the headers files #included in the .cpp files. If you're compiling on the command line, then just type all of the .cpp names (*.cpp ? ), use a makefile, or switch to an IDE and add all of the .cpp files to the project.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Multiple Files

    Multiple header and source files are used for organization; they are not necessary for the program to run.

    Namespaces
    Last edited by Sentral; 03-25-2007 at 11:49 AM.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Hmmm, so if I have 5 classes (errr, 5 .cpp files), I should have 5 header files, and in each of the source files, I have 5 (or 4) #includes to include every single header?

    Edit: Sentral... oh thanks. Didn't notice that in the FAQ.

    Can I also ask, MUST i use the #ifndef directives? and how do i name them?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I have 5 (or 4) #includes to include every single header?
    No, not necessarily. Only #include the header files that declare the classes (or other stuff) you want to use in that source file. Also, if one class declaration in a header file needs another class declaration, then you should #include the other header in that one.

    >> MUST i use the #ifndef directives? and how do i name them?
    Yes. You pretty much have to use them. There are some platform specific alternatives but it makes little sense to use those exclusively. People name them in different ways. The point is that the name should be unique to your program and any other program that might use that header file. Also, it is common practice to use all capital letters and underscores where you might have spaces or periods. So take the file name (like Data.h) and make it all uppercase with underscores instead of periods: DATA_H

Popular pages Recent additions subscribe to a feed