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.
Where to put class definitions
Page 1 of 112 Replies - 309 Views - Last Post: 15 February 2012 - 08:10 PM
Replies To: Where to put class definitions
#2
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.
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.
#3
Re: Where to put class definitions
Posted 14 February 2012 - 01:36 PM
Yup! What I needed to know, thanks!
#4
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.
#5
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
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
#6
Re: Where to put class definitions
Posted 15 February 2012 - 09:01 AM
#7
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:
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?
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?
#8
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
Jim
#9
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
#10
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:
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
//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
#11
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.
#12
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
Jim
This post has been edited by jimblumberg: 15 February 2012 - 05:41 PM
#13
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!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|