-->

Is it correct that header files in C++ and abstrac

2020-07-09 09:35发布

问题:

I am a little with familiar with C++ and I know that for almost every header file, I must create source file to go with it.

Now I am looking into java interfaces and implementation and it looks as the same thing. At first you just name the variables and methods in one class and then you define them in other class.

Are these things in C++ and Java basically the same or similar?

回答1:

Java interfaces and C++ header/implementation files are different concepts.

C++ has a text compilation model. So, to use something - such as a function - in your code, the compiler must first have parsed a definition of that function. By putting things into header files that you want to use from many source files, it saves you having to re-write out the definition of a function because you can include the same header file into many source files that use the things in that header.

Functions in C++ can be declared by just writing the function name and arguments:

void PrintMessage(std::string text);

And they can be defined by writing the method body too:

void PrintMessage(std::string text)
{
  cout << text;
}  

You can only define a function once in a compilation unit (which is all the text the compiler sees after the #includes have been replaced with the text of the file they include). But, you can declare a function many times as long as the declarations are the same. You must define every function that gets called once. That's why you have a .cpp file for each .h file. The .cpp defines all of the functions that are declared in the .h file. The .h file gets included into all of the the .cpp files that use the functions and gets included once into the .cpp file that defines the functions.

Java works out where the definitions of functions are for you when it compiles a project as it looks at all of the files in the project. C++ only compiles one .cpp file at a time and only looks at #included header files.

A Java interface is equivalent to a C++ abstract base class. It's essentially a declaration of a set of methods including the types of arguments they take and the type of their return values. A Java interface or C++ abstract base class can be inherited by a Java class or C++ class which actually defines (implements) what those methods are.

In C++, when you create a class, you usually (there are exceptions) put the method declarations into a header file, and you put the definitions in the .cpp file. But, in Java, you only need to write definitions of methods, these definitions do the equivalent of C++ definition and declaration in one. You can put all the java method definitions in one file.



回答2:

No. Java's interface is C++'s abstract class. Consider this:

// Java
interface Entity {
  void func();
}

class EntityImpl implements Entity {
  public void func() {
    System.out.println("func()");
  }
}

vs.

// C++
struct Entity {
  virtual void func() = 0;
  virtual ~Entity() {}

// just to emphasize it's a BASE class
protected: Entity() {} // not required
};

struct EntityImpl : Entity {
  void func() {
    std::cout << "func()" << std::endl;
  }
};


回答3:

NO, An interface is an construct which even exist in c++ as abstract classes. It have nothing to do with the split in .hpp and .cpp files. An interface defines some set of functions which can then get overrided by child classes / implementation



回答4:

No, they are quite different.

Java has no equivalent to a "header file": the separation of declaration of API/data structures and implementation of your classes simply doesn't exist. If you want to reference some third-party classes then you'll need the .class files, which are exactly the same thing that you need when you want to run the third-party classes (note that they usually are stored in .jar files, but that's just for convenience).

And the closest thing that C++ has to Java interfaces are pure virtual classes: i.e. classes that define a set of methods but don't define any implementations for them. Just like in Java, you'll have to produce some sub-class of those for them to be really useful and these sub-classes will provide the implementation.



回答5:

They're very different.

A Java interface is more like a pure abstract class in C++, where none of the methods have implementations. They serve as a contract with an object to guarantee that methods can be called.

The key reason why interfaces exist is that Java does not have multiple inheritance, meaning a class can only extend one super class. A class can implement multiple interfaces, though, so you can define any number of operations you can perform on a class through any number of interfaces.



回答6:

Not really.

First, a Java interface cannot contain variables, only constants and methods.

Second, Java interfaces should be created only where necessary, i.e. where you have an API that several classes implement and you want to be able to write code that handles them uniformly. Most Java classes should not have a matching interface, and most Java interfaces should have two or more implementing classes.



回答7:

No header file mostly contains code that we can re use somewhere some how where interface is just a contract how things should be implemented its not the same



回答8:

C++ is old and it's built upon C, which is even older. In C++, a class is really a C-style struct with a pointer to a vtable, containing pointers to the starting address of each function in the class.

What you put in your header file is the definition of the class, ie the struct. What you put in our implementation are the function to which the vtable will point. This is why you need to include the definition (the "header") of a class in another file to be able to use it. This is also why you normally put the definition of a class in a separate file (.hpp). You strictly don't have to, but it would be difficult to use that class in other classes if you don't. But you could as well put all your code in just one, big text-file.

The C++ compiler will process each file independently, expanding all #include macros to include their definitions in each .cpp file. It will also translate all function-calls to the vtable notation, ie

someInstance->someMethod() becomes (someInstance->VTABLE[0]), assuming someMethod() is the first one declared in the definition of someIntance's class.

The compiler then creates the object files (so called translation units) and lays the resulting maching code of all the functions implemented in all .cpp files in a memory layout, with relocation tables for all pointers in all vtables. The linker later replaces resolves the symbols in the relocation table to the actual memory address of the functions.

Long story short, C++ looks the way it does for legacy purposes (and the fact the C is not object oriented). Java is object oriented from the get go, meaning that interfaces are something you MAY use, as opposed to C++ where class definitions is something you MUST have (struct and vtable, remember) and something you MUST include, one way or another, in every .cpp file where you want to use that class.