12 Replies - 309 Views - Last Post: 15 February 2012 - 08:10 PM Rate Topic: -----

#1 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Where to put class definitions

Posted 14 February 2012 - 12:51 PM

So I have one parent class, two child classes, and one struct. I put all there definitions in one file called "definitions.h" then I have a seperate .cpp file for each of the class's methods code. I'm unclear on what best practices say, should each class/struct have its own file for its definition? So 6 or 7 files in total? Seems odd.
Is This A Good Question/Topic? 0
  • +

Replies To: Where to put class definitions

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 919
  • View blog
  • Posts: 6,443
  • Joined: 07-September 06

Re: Where to put class definitions

Posted 14 February 2012 - 01:34 PM

Typically all classes have their own header and implementation file, where they both have the same filename and a different extension (.h verse .cpp or .cxx). This is so you can quickly and easily include a class if you want, but don't have to include a TON of additional classes you don't need/ use. So, if you have 3 classes that would be 6 files right there.

Now, I personally don't normally use structs (I find classes to work much nicer), but I would suggest doing the same thing for structs (especially if you implement struct methods). If you are just using a struct to hold data (variables only) then a simple header file would suffice (after all the cpp file would be empty), though even there you may want to have the empty C++ file around just so you can use generic rules for your make file (which will greatly simplify life).

Hope that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Re: Where to put class definitions

Posted 14 February 2012 - 01:36 PM

Yup! What I needed to know, thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Zachari  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 28-February 11

Re: Where to put class definitions

Posted 14 February 2012 - 02:21 PM

The purpose of a class is to be able to write it once, use it for multiple programs. For example, if you've created a Binary Search Tree class mixed with all sorts of stuff in it like extra structs and classes, you're always including that in the code every time you use that, which could obfuscate your code a bit.
Was This Post Helpful? 0
  • +
  • -

#5 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Re: Where to put class definitions

Posted 15 February 2012 - 08:36 AM

So I now have,

MainHeader.h - generic includes
MainClass.h
NickNames.h - MainClass child
DirNames.h - MainClass child
nameList.h - struct the class's use
project.cpp
MainClass.cpp
NickNames.cpp
DirNames.cpp

I can't seem to figure out how to get then all included properly without having the same file included in a file twice
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: Where to put class definitions

Posted 15 February 2012 - 09:01 AM

Do you have include guards in each of your header files?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Re: Where to put class definitions

Posted 15 February 2012 - 04:28 PM

Yeah I believe thats what I need to be using but having a hard time wrapping my head around it. So far I've figured the following files need each other:

mainHeader.h



MainClass.h

	mainHeader.h
	nameList.h


NickNames.h

	mainHeader.h
	MainClass.h


DirNames.h
	
	mainHeader.h
	MainClass.h


nameList.h

	mainHeader.h


MainClass.cpp

	mainHeader.h
	MainClass.h
	nameList.h


NickNames.cpp

	mainHeader.h
	MainClass.h
	NickNames.h


DirNames.cpp

	mainHeader.h
	MainClass.h
	DirNames.h


dsad1.cpp

	mainHeader.h



Didn't see a edit button lol, maybe im blind, but the dsad1.cpp is the "project.cpp", I figure it needs the mainHeader and the class/struct definitions?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: Where to put class definitions

Posted 15 February 2012 - 04:36 PM

Please post the complete contents of one of your include files.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Re: Where to put class definitions

Posted 15 February 2012 - 04:43 PM

//mainHeader.h

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <sstream>
using namespace std;



//MainClass.h

class MainClass
{
	protected:
		struct nameList *head;
		struct nameList *tail;
		struct nameList *newBlock;
		struct nameList *curr;
		struct nameList *before;
		string buffer;

	public:
		void enterInfo(string stringOne, string stringTwo);
		void findPosition(void);
		void printList(string printWhat);
		void MainClass::deleteList(void);
		MainClass::MainClass(void);
};



//NickNames.h

class NickNames : public MainClass
{
	public:
		void scanDir(void);
		string NickNames::searchName(string realName);
};



DirNames.h

class DirNames : public MainClass
{
	public:
		void getDirectory(void);
		string DirNames::returnDir(void);
		void DirNames::SetCurr(void);
		string DirNames::returnName(void);
};




Thats my class's? I have a header for my nameList struct as well, then all the cpp files i posted above
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: Where to put class definitions

Posted 15 February 2012 - 04:56 PM

You need include guards in each of your header files. Something like:
//MainClass.h
#ifndef MAINCLASS_H_ // beginning
#define MAINCLASS_H_ 

class MainClass
{
	protected:
		struct nameList *head;
		struct nameList *tail;
		struct nameList *newBlock;
		struct nameList *curr;
		struct nameList *before;
		string buffer;

	public:
		void enterInfo(string stringOne, string stringTwo);
		void findPosition(void);
		void printList(string printWhat);
		void MainClass::deleteList(void);
		MainClass::MainClass(void);
};


#endif // MAINCLASS_H_ // end



Also you should never have a "using statement" inside a header file. And you should never rely on some other file including the proper include files. Having a header file like your mainheader.h is considered bad practice. You should only include the bare minimum header files in each source file. Only include the required header files in in each file.

Jim
Was This Post Helpful? 1
  • +
  • -

#11 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Re: Where to put class definitions

Posted 15 February 2012 - 05:28 PM

Ok thanks, so I got rid of my mainHeader file, now for NickNames.h an DirNames.h I need MainClass.h to be included, than my dsad.cpp I need both NickNames.h and DirNames.h included but that would unclude MainClass.h twice? Im having a hard time understanding the include guards.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: Where to put class definitions

Posted 15 February 2012 - 05:40 PM

The include guards prevent the header from being included more than once in the same compilation unit. Every header file should be guarded against multiple inclusion with header guards.

Jim

This post has been edited by jimblumberg: 15 February 2012 - 05:41 PM

Was This Post Helpful? 1
  • +
  • -

#13 Scar.  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-October 10

Re: Where to put class definitions

Posted 15 February 2012 - 08:10 PM

Oh ok gotcha, the way include guards work just clicked lol, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1