Thread: doin' classes in header files?

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    doin' classes in header files?

    Is it possible to create all of you classes in a header file (i think I saw somebody do that) instead of doing it in the .cpp file? If so, could you please demonstrate?

    Thanks
    -Chris

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    It's not only possible, but extremely recommendable. The class declaration is only a model for a compiler. It doesn't have any code as you can't put any code in header, except inlined code, but that's an another case. So:


    Code:
    // In Myclass.h:
    
    class MyClass
    {
      public:
        MyClass();
        ~MyClass();
    
        // function declarations and whatever...
    
    
        private:
    
         // variables and functions and whatever...
    };
    
    
    // In any .cpp file:
    
    #include "myclass.h"
    
    int WhatEverFunc(void)
    {
      MyClass MyObject;
    
      // yada yada...
    
      return whatever;
    }
    You can use the class in any .cpp file you #include it as it'd be defined there. There is a short example, I hope it helped.
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Thanks heaps, that should prove useful. But I just noticed that in your constructor and destructor, you didn't actually writing them you just called them...i'll explain:

    you wrote:
    public:
    MyClass();
    ~MyClass();

    I thought it was meant to be:
    public:
    MyClass() // constructor
    {
    //stuff like num1 = 0; etc...
    }
    ~MyClass
    {
    }

    What's going on here...?
    Last edited by face_master; 11-14-2001 at 03:36 AM.

  4. #4
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Oh yeah, I forgot the major point, sorry
    You must have a MyClass.cpp file where you write your classes code.

    Code:
    // in Myclass.cpp
    
    #include "myclass.h"
    
    // constructors and destructor
    
    MyClass::MyClass()
    {
      // code here
    }
    
    MyClass::~MyClass()
    {
      // code here
    }
    
    
    // function bodies
    
    void MyClass::AnyFunc(int Variable)
    {
      // code here
    }
    And you must compile this .cpp file also. Usually compilers do it automatically and link it to your project.
    Making error is human, but for messing things thoroughly it takes a computer

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    so what you wrote in your previous post needs this extra cpp file in order to work?

  6. #6
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Basicly... yes.

    You could write all the code in the header, but then it'd be all inline code. If you have the code in .cpp file the code will be generated once. The code you have in header will be generated every time you call it, so if you call a function which code is in header you'll have it's code multiple times in .exe file. This can increase the size of your .exe file very much.

    It must not be the MyClass.cpp file. The name doesn't make difference, or you could put the code in .cpp file you have other code, but it wouldn't make the code easy to read and debug. So it's recommendable to have a .cpp file same name as the .h/.hpp file.
    Making error is human, but for messing things thoroughly it takes a computer

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    you have created the constructors in the cpp file, can this be done in the .h file like I have done?

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You can write all of the code for one class in the header. You don't need a .cpp file if you don't want to, but it's the normal way.

    Code:
    // clas.h
    #include <stdio.h>
    
    class clas
    {
    public:
        clas( int x )
        {
            i = x;
        }
        print()
        {
            printf( "%d",i );
        }
    private:
        int i;
    };
    
    // main cpp
    
    #include "clas.h"
    
    int main()
    { 
        clas c(5);
        c.print();
        return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Yes, but then the constructor will be inlined code. If the constructor is long, then it'll be better to declare it in .cpp file.
    Making error is human, but for messing things thoroughly it takes a computer

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    yeah, doing it all the the cpp file seems like it may prevent some problems along the line. Thanks for explaining it all to me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Static variables and header files
    By drrngrvy in forum C++ Programming
    Replies: 8
    Last Post: 12-02-2006, 01:27 PM
  3. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM