Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,251 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,156 people online right now. Registration is fast and FREE... Join Now!




Where should one put class declarations?

 
Reply to this topicStart new topic

Where should one put class declarations?, Do they belong in a header file? If not, where?

ramz
post 27 Mar, 2008 - 05:38 AM
Post #1


New D.I.C Head

*
Joined: 8 Jun, 2007
Posts: 10


My Contributions


I have a "style" question concerning building bigger programs. I am working on a project that I expect to grow slowly over a couple of years, so I've started distributing the code over several files to make it all easier to track. My question is in regards to the proper way to handle "class declarations"?

I tried putting the classes in ".cpp" files, but when I run make, it complains. From reading various forums it seems that header files describe classes so I'm putting them in headers. This works, but I don't know what the "right" thing to do is for C++. Is there any chance that my approach will come back and bite me? If so, what alterations are required and what precautions should I take?

CODE

#include <various packages>
using namespace std;

// various global declarations snipped

#include "wavelet.h" // defines wavelet class and related functions
#include "profile.h" // defines the profile class and related functions

int main () {
  void read_startup(), batch_run(wavelet w);
  read_startup();
  wavelet W("impulse", 1, 100);  
  batch_run(W); // uses profile class extensively
  return(0);
}

// other functions snipped


User is offlineProfile CardPM

Go to the top of the page

GWatt
post 27 Mar, 2008 - 06:32 AM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


A header file is a good place to put class declarations so that way, you just have to include the header file, not the entire class.

And you shouldn't define functions inside of methods. Define them at the top, or in an external header file.
User is online!Profile CardPM

Go to the top of the page

ramz
post 2 Apr, 2008 - 06:47 AM
Post #3


New D.I.C Head

*
Joined: 8 Jun, 2007
Posts: 10


My Contributions


Thanks. Slowly, I'm learning the details of C++. Very elegant language once it starts to click.


User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month