Results 1 to 9 of 9

Thread: Headers and CPP files

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Question Headers and CPP files

    Can someone explain to me what code goes in cpp files, and what code is supposed to go in header files?
    thanks

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The header files are for declarations. Declaration of all things you want shared among modules (cpp files) to be exact.

    The cpp files are where the actual code goes.

    Like this:
    Code:
    // Declarations, go into headers (.h)
    void func(int i1, int i2);
    int func2();
    class cl
    {
      int m1, m2;
    public:
      cl();
      void method(int i);
      int inline_method() {
        return m1 * m2;
      }
    };
    extern int g_i;
    
    // Definitions, go into modules (.cpp)
    void func(int i1, int i2)
    {
      cl obj;
      obj.method(i1);
      cout << i2 << ':' << obj.inline_method() << endl;
    }
    
    int func2()
    {
      return g_i;
    }
    
    cl::cl()
    {
      m1 = m2 = 0;
    }
    
    void cl::method(int i)
    {
      m1 = i / 5;
      m2 = i % 5;
    }
    
    // Even though this looks like a declaration
    // it's actually a definition.
    int g_i;
    int inline_method() {
    return m1 * m2;
    }
    This is actually a definition, but inline functions are an exception to the rule. They always go into the headers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    i'm still a bit confused. say i have three cpp files--main, classes, and functions. main needs to be able to recognize references to classes and functions, and functions to classes and classes to functions. Should I put all the prototypes in one header and then include that header in each cpp file? How should i work this?
    thanks again

  4. #4
    Member
    Join Date
    Nov 2002
    Location
    Whaaaaat...........
    Posts
    44
    You declare your functions in your header files, then call them in your .cpp files. Not too hard....

    Ok if ur really lost, just follow along with me.

    Make a header file and typ the following (this is VC++):
    PHP Code:
    double Square (double x);

    double Cube (double x); 
    ok now make a .cpp file and type this:
    PHP Code:
    #include "Your Header File's Name.h"

    double Square (double Value)
    {
    double SquareReturn;

    SquareReturn Value Value;

    return 
    SquareReturn;

    double Cube (double Value)
    {
    double CubeReturn;
    CubeReturn Value Value Value;

    return 
    CubeReturn;

    Ok and make one more .cpp, make this the main:
    PHP Code:
    #include "Your Header's Name.h"
    #include <iostream>

    main ()
    {
    double Number;
    double SquaredNumberCubedNumber;

    Number 5;

    SquaredNumber Square (Number);
    CubedNumber Cube (Number);

    ard::cout << "Sqaure of 5 = " << SqauredNumber >> std::endl;
    srd::cout << "Cube of 5 = " << CubedNumber << std::endl;

    return 
    0;

    Ok now you used a header and 2 cpp files to create a more efficient, easier to read program that tells you the square of 5 and the cube of 5. I hope this helped!!!!!!

    This may have some spelling errors, the VC++ complier is very fussy about spelling, so this is just eye candy, not neceserally a thing to try out, its just to let u understand the concepts.
    A way to find answers, is to ask questions, for a question that has been asked may seem stupid for 5 minutes, but a question never asked will remain unknown forever.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But you can also have more than one header. It's common to have one header per class, one (or more in large projects) for functions and one for independent constants and global variables.
    Global variables are to be avoided btw...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    I tried moving my class declarations into a .h, and my class definitions into a .cpp, but i still have errors, and i'm not sure why. if could you could please look at the project and tell what is the best way to arrange to the code i have, i would much appreciate it.
    thanks
    Attached Files Attached Files

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You had a few case errors (speed instead of Speed) and missed to include necessary header files.

    And you used a strange string class that I can not approve of (besides I don't have it, so I had to remove it to test this for me).
    I replaced the old headers with the new ones and your string class with the C++ standard string class.

    This project compiles and runs very well, exept that the race itself runs too fast.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    CornedBee,
    thanks for the help.
    the string class is the one that the AP College Board approves for the AP C++ exam in May, and since I'm taking it, i need to be familiar with using it. I just forgot to include it. The reason for the fast races is becase i was doing some probability testing, and i was tired of waiting. But thanks again for the help!
    jmiller

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I wonder why AP approves of apstring, this strange String of yours but not the Standard string. It's the best of them and the only that has any application outside of programming courses.
    Curse AP!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width