Amateur c++ student here who has gotten to the point where I'm comfortable with classes somewhat, however I'm a bit obsessive compulsive. I've been learning by just continually adding to a program, however it's all in my main.cpp. I use visual studio 2005, and would like to take certain classes and move them over to their own file to keep my program direction organized, so when I create a class for this in VS it creates a .cpp and a .h . A bit lost of what to put in each. Should my class functions exist in the cpp, and then what goes in the h? declarations?

the . h starts with

#pragma once

class newClass
{
public:
	newClass(void);
public:
	~newClass(void);
};

and the .cpp goes

#include "newClass.h"

newClass::newClass(void)
{
}

newClass::~newClass(void)
{
}

Recommended Answers

All 4 Replies

Amateur c++ student here who has gotten to the point where I'm comfortable with classes somewhat, however I'm a bit obsessive compulsive. I've been learning by just continually adding to a program, however it's all in my main.cpp. I use visual studio 2005, and would like to take certain classes and move them over to their own file to keep my program direction organized, so when I create a class for this in VS it creates a .cpp and a .h . A bit lost of what to put in each. Should my class functions exist in the cpp, and then what goes in the h? declarations?

the . h starts with

#pragma once

class newClass
{
public:
	newClass(void);
public:
	~newClass(void);
};

and the .cpp goes

#include "newClass.h"

newClass::newClass(void)
{
}

newClass::~newClass(void)
{
}

I'm not too familiar with Visual Studio 2005 or the "#pragma once" directive, so no comment there. I'd add something like this to the top of the .h file:

#ifndef NEW_CLASS_H
#define NEW_CLASS_H

Then at the bottom, put this:

#endif

I also think you can get rid of the void arguments in these function. They probably don't hurt, but I don't see the need for them.

Other than that, it looks right. Visual C++ will figure out the makefile and how to link for you so unless you have specific needs, you won't need to create your own makefile.

#pragma once is the better way of doing it. ifdef, endif are outdated.

#pragma once is the better way of doing it. ifdef, endif are outdated.

These wikipedia articles discuss a little bit of the pros and cons of using #pragma once and offer a way to use both it and #ifndef for portable code. I don't know whether #ifndef is considered outdated or not. I do know that #ifndef is still currently taught to new first semester C++ programmers in at least some universities. Of course that doesn't necessarily mean that it isn't outdated :) .

http://en.wikipedia.org/wiki/Include_guard#Use_of_.23include_guards
http://en.wikipedia.org/wiki/Pragma_once

#pragma once is the better way of doing it. ifdef, endif are outdated.

not at all. :) #pragma once is only supported by a few compilers. #ifndef, #define and #endif are supported by all compilers. :)

commented: Good answer - #pragma is non-portable +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.