为什么在创建新的 C++ 时会创建两个文件(.h 和 .cpp)班级?

发布于 2024-10-06 18:51:53 字数 1342 浏览 5 评论 0原文

大约 14 年前,我已经编写过一些 C++ 程序。我熟悉了 .NET 等新技术,我工作中主要使用这些技术。

现在,我正在编写一个简单的电话列表 Windows 应用程序,我想将其设为 C++,以便我可以更好地查看 C# 和 C++ 的差异。

让我说我已经注意到了差异!呵呵...因此,这些区别之一是,当从 Visual Studio 模板创建新的 C++ 类时,它不仅创建 .cpp 类文件,还创建一个头文件。

为什么会这样呢?为什么要为一个类创建一个 class1.h 和一个 class1.cpp 文件?

我记得头文件很可能是函数和对象的库,如果我们可以这么说,为了将来的重用,我记得正确吗?

问题

  1. 为什么添加新的 C++ 类时会创建两个文件(.h 和 .cpp)?
  2. 我应该在头文件中定义成员并在cpp文件中定义函数核心吗?
  3. 如果不是 2,那么在这个特定场景中头文件的用途是什么?

编辑 #1

那么我的代码应该是这样的吗?

// Customer.h header file
ref class Customer {
    private:
        char* _number, _name;
        long _phoneNumber;

    public:
        char[] get_number();
        void set_number(char* number);
        char[] get_name();
        void set_name(char* name);
        long get_phoneNumber();
        void set_phoneNumber(long phoneNumber);
        void set_name(char* name);
}

然后:

// Customer.cpp
#include <Customer.h>

char[] Customer::get_number() {
    return _number;
}

void Customer::set_number(char* number) {
    if (number != null && sizeof(number) < 1) return;
    _number = number;
}

// And the other members here...

现在我知道,我的代码中大多数都存在很多错误。如果您能帮助我纠正它们,以便我提高我的 C++ 技能,我会很高兴。

谢谢你帮我解决这个问题。

I have programmed a bit of C++ back about 14 years ago. I got acquainted to new technologies such as .NET with which I work mostly work with.

Now, I'm writing a simlpe phone list Windows Application which I want to make it C++ so that I can better view C# and C++ differences.

Let me say that I have already noticed a difference! Hehehe... Hence, one of those difference is that when creating a new C++ class from Visual Studio template, it creates not only the .cpp class file, but also a header file along with it.

Why is that so? Why creating a class1.h and a class1.cpp files for one classe?

I remember that header files are likely libraries of functions and objects, if we may say so, for future reuse, do I remember correctly?

Questions

  1. Why are there two files (.h and .cpp) created when adding a new C++ class?
  2. Should I define the members in the header file and define the functions core in the cpp file?
  3. If no to 2, what is the header file for in this particular scenario?

EDIT #1

Then should my code look like this?

// Customer.h header file
ref class Customer {
    private:
        char* _number, _name;
        long _phoneNumber;

    public:
        char[] get_number();
        void set_number(char* number);
        char[] get_name();
        void set_name(char* name);
        long get_phoneNumber();
        void set_phoneNumber(long phoneNumber);
        void set_name(char* name);
}

Then:

// Customer.cpp
#include <Customer.h>

char[] Customer::get_number() {
    return _number;
}

void Customer::set_number(char* number) {
    if (number != null && sizeof(number) < 1) return;
    _number = number;
}

// And the other members here...

Now I know, there most be plenty of errors in my code. I'll be happy if you help me correct them so that I can improve my C++ skills.

Thanks for helping me figuring it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜你已久 2024-10-13 18:51:53

C++ 中的函数和类必须在使用前声明,这只是一个声明,表明这些函数可以从此文件中使用。这些声明是使用头文件(.hpp/.h 文件)导入的。

要声明函数,您可以这样写:

返回类型 函数名称 ( 参数 );

所以这个函数:

int factorial (int x)
{
  if (x == 0)
    return 1;
  return x * factorial (x - 1);
}

会像这样预先声明:

int factorial (int x);

声明类是这样的:

class 类名 { 函数和变量声明 };

因此,带有方法 bar 和公共成员变量 bazclass Foo 看起来像这样:


foo.hpp:

#ifndef FOO_HPP_
#define FOO_HPP_

class Foo
{
public:
  int baz;

  void bar ();
};

#endif

<代码>foo.cpp:

#include "foo.hpp"
#include <iostream>

void Foo::bar ()
{
  std::cout << baz << std::endl;
}

Functions and Classes in C++ must be declared before their use, this is simply a declaration saying that these functions can be used from this file. These declarations are imported using header files (.hpp/.h files).

To declare a function, you would write this:

return type function name ( arguments );

So this function:

int factorial (int x)
{
  if (x == 0)
    return 1;
  return x * factorial (x - 1);
}

Would be predeclared like this:

int factorial (int x);

Declaring classes is like this:

class class name { function and variable declarations };

So class Foo with method bar, and public member variable baz would look like this:


foo.hpp:

#ifndef FOO_HPP_
#define FOO_HPP_

class Foo
{
public:
  int baz;

  void bar ();
};

#endif

foo.cpp:

#include "foo.hpp"
#include <iostream>

void Foo::bar ()
{
  std::cout << baz << std::endl;
}
戏蝶舞 2024-10-13 18:51:53

类在标头中声明,大多数功能在 .cpp 文件中定义。该工具正在帮助您实现接口与实现的分离。

考虑头文件和 .cpp 文件之间的分离的方法是:

  • 头文件包含供其他文件使用的声明。
  • .cpp 文件包含实现。

原因是您希望大量文件能够利用功能,但您只想在一处实际定义该功能。

Classes are declared in a header and most functionality is defined in a .cpp file. The tool is helping you accomplish this separation of interface from implementation.

The way to think about the separation between header and .cpp file is:

  • Headers contain declarations to be used by other files.
  • .cpp files contain implementation.

The reason is you want lots of files to be able to utilize functionality, but you only want to actually define that functionality in one place.

昔日梦未散 2024-10-13 18:51:53

头文件声明/定义一个类。它包含成员函数、成员数据、友元等。通常,它不包括太多(如果有的话)的实现(模板是例外)。

实现文件 (*.cpp) 就是这样:它们实现类。

你不必使用实现文件(如果你告诉 VS 向导你想要创建一个内联类,它只会创建头文件),但通常这只适用于模板类(例如 STL 类和许多boost 库中的类)或非常简单的类。

Header files declare/define a class. It contains the member functions, member data, friends, etc. Typically, it does not include much (if any) of the implementation (templates are the exception).

Implementation files (*.cpp) are just that: they implement the class.

You do not have to use an implementation file (if you tell the VS wizard that you want to create an inline class, it will only create the header file), but typically that is only done for template classes (e.g. the STL classes and many of the classes in the boost library) or very simple classes.

权谋诡计 2024-10-13 18:51:53

C++ 编译需要头文件,因为它的工作方式与 C# 几乎没有区别。

在 C# 中,二进制模块之间的链接始终是动态的。在C++中,静态链接和动态链接是有区别的。要详细说明,我需要花整整一个小时的时间来写,但我们可以用最短的方式来说。

在 C++ 中,每个 .cpp 文件都单独编译为目标文件。该文件需要链接到其他文件才能形成二进制文件。头文件声明了包含该头文件的 .cpp 文件所需的所有方法签名,该文件调用为类定义的方法。

在 C# 中,所有文件都被编译成一个二进制文件,即。编译器始终知道您正在编译的成员可以看到哪些类和方法。

Header files are required by the C++ compilation, because it works hardly different from C#.

In C#, linkage between binary modules is always dynamic. In C++, there is difference between static and dynamic linkage. To go into the detail, it would take me a full hour of writing, but let's say it the shortest possible way.

In C++, every .cpp file is separately compiled into an object file. That file needs to be linked to the others in order to form a binary file. The header file declares all the method signatures required by the .cpp file in which it's included, which is the file that invokes methods defined for the class.

In C#, all files get compiled into a single binary file, ie. the compiler always knows what classes and methods are visible by a member you are compiling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文