0

First I'm a beginner at programming so don't expect me to understand every code-specific word.

Second I'm sometimes slow on the uptake.

Third I think I covered the Basics of C++ but that's it. I'm happy to learn more of course!

To my Question:

I'm programming a little code to experience with classes. I made two classes, each one in a different .h and .cpp file. Now each one is using the headers iostream and string.

How should I include those without making any Problems? Is #pragma once sufficient?

Second question is about using namespace std: where should i put it(I know it isn't a bad use but ist only for a small Programm)

First Header:

#pragma once

#include <iostream>
#include <string>  

//using namespace std Nr.1

class one
{

};

Second Header:

#pragma once

#include <iostream>
#include <string>  

//using namespace std Nr.2

class two
{

};

Finally Main:

#include "one.h"
#include "two.h"

//using namespace std Nr.3

int main()
{
   return 1;
};

Thanks in advance for responding.

Fomps
  • 1
  • 2

3 Answers3

1

There's no problem including twice iostream and string in both class headers'.

The #pragma directive is used to protect two declarations of types (typedef, classes) of your own types.

Hence it applies to your class headers'.

Moreover, there are drawbacks using #pragma directive as stated here : https://stackoverflow.com/a/1946730/8438363

I recommend using preprocessor defines guards :

#ifndef __MY_HEADER__
#define __MY_HEADER__


//... your code here

#endif

Hope this helps.

Theforgotten
  • 140
  • 7
  • Yes to include guards. No to this particular form. Names that contain two consecutive underscores (`__MY_HEADER__`) and names that begin with an underscore followed by a capital letter are reserved for use by the implementation. Don't use them in your code. – Pete Becker Aug 09 '17 at 12:18
  • Can you provide a link to the guideline/rule stating the form of include guards please ? – Theforgotten Aug 09 '17 at 14:01
0

You will need to use Include guards.They ensure the compiler includes each "included" header file(#include "XXX.h") only Once.

But if you are creating a small application & don't mind recompiling/rebuilding your entire project if need be, then putting the common header files in a dedicated header file is fair game & keeps code clean & small.

-1

You can also make a header with all the common dependecies you need and include it in each class that needs those dependecies. Here is a simple example:

Core.h

#include <iostream>
#include <string>
// include most common headers

using namespace std;

One.h

#pragma once
#include "Core.h"
// if you need a header only for this class, put it here
// if you need a header in mutiple classes, put in Core.h

namespace one {

    class One
    {
    public:

        One();
       ~One();

        void sumFromFirstNamespace(string firsNumber, string secondNumber)
        {
          //convert string to int
          int first = stoi(firsNumber);
          int second = stoi(secondNumber);

          int result = first + second;

          cout << result << endl;
        }
    };

}

Two.h

#pragma once
#include "Core.h"
// if you need a header only for this class, put it here
// if you need a header in mutiple classes, put in Core.h

namespace two{
    class Two
    {
    public:
        Two();
       ~Two();

        void sumFromSecondtNamespace(string firsNumber, string secondNumber)
        {
          //convert string to int
          int first = stoi(firsNumber);
          int second = stoi(secondNumber);

          int result = first + second;

          cout << result << endl;
       }

    };

}

main.cpp

#include "One.h"
#include "Two.h"

int main()
{

     one::One firstClass;
     two::Two secondClass;

     firstClass.sumFromFirstNamespace("10", "20");
     secondClass.sumFromSecondtNamespace("20", "30");

}

There can be cases where you need the same 10+ headers in two different classes, i think that puting them in one header helps you see the code better. And yes, preprocesor defines are also good, don't forget that. (:

Mara Black
  • 1,666
  • 18
  • 23
  • Bad idea. That common header introduces dependencies in every source file that uses it: any change in that header requires recompiling **every** source file that uses it. Each source file should include the headers that **it** needs. – Pete Becker Aug 09 '17 at 12:17