0

I am new to C++ and I found the following code in a text book:

enter image description here

For what I see they do not use a header; I am using Visual C++ and the structure that they give me is with a header so I programmed the following:

//parts.h
#pragma once

class parts
{
private:
    int idModel;
    int idPart;
    float cost;
public:
    parts(void);
    virtual ~parts(void);
    void setPart(int,int np,float);
    void showParts();
};

//parts.cpp
#include <iostream>
#include "parts.h"

using namespace std;

parts::parts(void)
{
}

parts::~parts(void)
{
}
void parts::setPart(int nm, int np, float c)
{
    idModel=nm;
    idPart=np;
    cost=c;
}
void parts::showParts()
{
    cout<<"Model, "<<idModel;
    cout<<" part "<<idPart;
    cout<<" cost\n "<<cost;
}

//main program
#include <iostream>
#include "parts.h"

using namespace std;

int main(void)
{
    parts p;
    p.setPart(569,10,55.55);
    p.showParts();
    system("PAUSE");
}

so which one is the correct one? to use or not to use headers in this situation? what would be the possible drawbacks of not doing that.

Thanks

Little
  • 3,363
  • 10
  • 45
  • 74
  • 3
    In general you should split up your declarations in the header, and the definitions in the cpp file. I think they did it all in one file just for the sake of brevity. – Cory Kramer Mar 07 '14 at 15:58
  • 1
    A well structured program improves clarity and easy to maintain. What you have done is correct. Not sure why you doubt the way you wrote. – Mahesh Mar 07 '14 at 15:58
  • In constructor you should at least set all your variables to `0` – Quest Mar 07 '14 at 16:05
  • See also [this question](http://stackoverflow.com/q/1001639/96780) and in particular [my answer](http://stackoverflow.com/a/1001749/96780). Well, while you're there, take a look at the other answers, too. – Daniel Daranas Mar 07 '14 at 16:11

3 Answers3

2

Header files, as you've discovered, aren't required. The main benefit of header files is a speedier compile time for larger projects. As your project grows, CPP files that are unchanged do not need to be recompiled at compile time.

Other than that you gain some readability points and over-all, it's the convention. You should use them unless you're only writing tiny one time use programs.

Jeremy Villegas
  • 193
  • 1
  • 8
  • You should add that this convention applies to _publicly accessible_ classes and methods. If `part` here has a helper class, say `part_parser`, which is only used in the context of one `part` member function, its best to keep delcaration _and_ definition of the helper class inside the .cpp file. – Fabian Knorr Mar 07 '14 at 16:06
  • The main benefit of header files is "declarations visible to several CPP files" ... How do you intent to use the `parts` class in foobar.cpp if there is no header file? – manuell Mar 07 '14 at 16:06
0

It is good to have all libraries included in every file. That improves code readability because everything what is used in file is shown on top.

Saerumn
  • 1
  • 1
0

Most programs are divided into several source files, to give more manageable chunks of source to work with. In the case of C++, it is common to have a source file defining a class (or a set of intimately related classes). This also serves to hide implementation details. To tell other source files what the interface to the class is (its layout, the methods offered, and such details) those definitions are placed in a header file. This header file is #included both in the definitions file (to have the compiler check both agree) and in each other source file using the class.

You certainly can write your program as one big file, or even copy the definitions of the used classes by hand into each file using them. Why each of those is a bad idea is left as an exercise.

An added benefit is that if a class definition doesn't change, its defining file doesn't have to be recompiled, thus speeding up the building.

vonbrand
  • 11,412
  • 8
  • 32
  • 52