Why are there two files created (.h and .cpp) when creating a new C++ class?
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
- Why are there two files (.h and .cpp) created when adding a new C++ class?
- Should I define the 开发者_如何学编程members in the header file and define the functions core in the cpp file?
- 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.
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;
}
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.
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.
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.
精彩评论