如何组织包括C ++中的多个类

时间:2023-02-05 11:36:18

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!

第三,我认为我已经涵盖了C ++的基础知识,但就是这样。当然,我很高兴学到更多东西!

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.

我正在编写一些代码来体验类。我创建了两个类,每个类都在一个不同的.h和.cpp文件中。现在每个人都使用标头iostream和string。

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

如何在没有任何问题的情况下包括这些内容? #pragma曾经足够吗?

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)

第二个问题是关于使用命名空间std:我应该把它放在哪里(我知道这不是一个坏用途,但只适用于一个小程序)

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.

在此先感谢您的回复。

3 个解决方案

#1


-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. (:

在某些情况下,您需要在两个不同的类中使用相同的10个标题,我认为将它们放在一个标题中可以帮助您更好地查看代码。是的,preprocesor定义也很好,不要忘了。 (:

#2


1  

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

在两个类标题中包含两次iostream和string都没有问题。

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

#pragma指令用于保护您自己类型的两个类型(typedef,classes)声明。

Hence it applies to your class headers'.

因此它适用于您的类标题'。

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

此外,使用#pragma指令存在缺点,如下所述:https://*.com/a/1946730/8438363

I recommend using preprocessor defines guards :

我建议使用预处理器定义防护:

#ifndef __MY_HEADER__
#define __MY_HEADER__


//... your code here

#endif

Hope this helps.

希望这可以帮助。

#3


0  

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

您将需要使用Include警卫。它们确保编译器仅包含每个“包含”头文件(#include“XXX.h”)一次。

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


-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. (:

在某些情况下,您需要在两个不同的类中使用相同的10个标题,我认为将它们放在一个标题中可以帮助您更好地查看代码。是的,preprocesor定义也很好,不要忘了。 (:

#2


1  

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

在两个类标题中包含两次iostream和string都没有问题。

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

#pragma指令用于保护您自己类型的两个类型(typedef,classes)声明。

Hence it applies to your class headers'.

因此它适用于您的类标题'。

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

此外,使用#pragma指令存在缺点,如下所述:https://*.com/a/1946730/8438363

I recommend using preprocessor defines guards :

我建议使用预处理器定义防护:

#ifndef __MY_HEADER__
#define __MY_HEADER__


//... your code here

#endif

Hope this helps.

希望这可以帮助。

#3


0  

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

您将需要使用Include警卫。它们确保编译器仅包含每个“包含”头文件(#include“XXX.h”)一次。

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.

但是,如果您正在创建一个小应用程序并且不介意在需要时重新编译/重建整个项目,那么将公共头文件放在专用头文件中是公平的游戏并保持代码清洁和小。