Thread: 3 Files of OO Language

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    46

    3 Files of OO Language

    While doing some reading on C++ I noticed there is a way to create files in the true object oriented fashion. Tto create 3 seperate files, a specification file, an implementation file, and a client file. I have never learned to make 3 seperate files. I have always had everything in what I think to be 1. I use VS.net by the way for my programs. What does my book mean when using 3 seperate files? Do i have to merge these files together after creating them so they work in unison and can compile together? I appoligize if this sounds silly, I am just a bit confused after reading this section. Thanks

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I thought there was a section in the FAQ about this, but skimming the FAQ I couldn't find it. So....


    If is a common practice in OOP to put a class declaration in a .h file and the method definitions for the class in a .cpp file with the same name as the .h file. You need to link these files to the driver file containing main() (or some other entry point). IF you are using an IDE like VC6.0, .Net, BCB, or DevC++, you just have to list the .h file in the include list like this:

    #include "myClass.h";

    before calling main(). In the above example, myClass.h must be in the same directory as the file containing main(). If it isn't, then you need to type the complete file path between the double quotes, not just the file name.

    The exception to this practice is if you are using templated classes, in which case you need to put the class declaration and function definitions all in the .h file.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    elad, great, thanks for shedding some light, my next question is, do you know how would i go about actualyl creating a ".h" file or specification file in VS.net?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    also I have a few classes/struct that I need to create in a header file or specification file, do they all have to be seperate header files? My code is below:

    Code:
    const int MAX = 10;
    struct car{
    char AorD;
    char license[6];
    time carTime;      //this is struct Time  - should it be in another .h ?
    char AM_PM[2];
    int no_moves;
    };
    
    struct Time{
    int hours;
    char : ;
    int minutes;
    };

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    If they are classes/structs with no associated functions, they do not need to be each in a separate header file. The purpose of having a header and cpp file for one class is because it causes linking problems. In your main cpp file, you need to include the class definition and prototypes by including the header file, so you can use them. The class cpp file also needs to include the prototypes for its own purposes. The big problems start to arise when you have classes using other classes using other classes. I think the problems are redefinition, but I'm not sure. So, to be safe with them, if you have a class with its own functions, keep the class definition and prototypes in the header file, and keep the functions inside the cpp file. It makes the code alot cleaner. (Of course, if you are making a templated class, then you have to have everything in one header file, as it doesn't actually generate code).

    So your time and car structs/classes do not need to be separated, as they don't have their own functions.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM