Can c++ code by dividing .cpp and .h in java?

Asked 2 years ago, Updated 2 years ago, 33 views

When coding for c++ assignments, In the .h file,

class declaration, private variable declaration, public functions (parameters); After doing this, cpp implemented the function.

Can Java code in this way? It was comfortable because .h had the same design.

java

2022-09-22 18:54

1 Answers

It's going to be a long story.

First, a three-line summary

C/C++ compiles on a per-file basis. .cpp files are compiled one by one, but .h or .hpp are not compiled.

Then, when and where the header compiles, it is stuffed into the .cpp file with the preprocessor before compiling. That's the #include syntax.

The reason why I'm doing this is because C/C++ doesn't support language-level loading of code developed by others. (C++20 has a module...)

Instead, when compiling, leave other people's code blank, and link other people's code at the link stage.

But even if you try to leave it blank, you need to know what the function looks like to compile it, right? So, before compiling, insert what other people's functions or classes look like in the .cpp file with #include.

Java is the language that came after C++. You want to fix the problem. So there is a function to load code developed by others at the language level. That's the import syntax. You've seen a lot of chords at the top, right?

Also, the "header separation" of C/C++ has often been a headache. If you work on a little bigger project, you'll experience a lot of cases where you can't compile, including the same header here and there (Ugh). So I made it not to share at all.

By the way, Java compiles in "packages."

I have a lot to say about the same design feeling you mentioned.

Once the design has become necessary, it means that the code is becoming difficult to understand. It's mental to look at the longer code and take care of it. At this point, you should try to isolate the code at the appropriate level. It's the best if you can see and understand the entire sauce.

Oh, of course, if you need to. But how do you judge this need... This is one of the lifelong challenges of programmers. Search for things like software engineering, design patterns and YAGNI later.

But even within the code, there's a point where you need this "design." It's more of a promise than a blueprint, so there's this thing called the strong interface.

The interface must have this function in the class! It's a promise. There is only a function definition. No, rather, I blocked the body of the function from being used. The class that inherits an interface must implement all the functions that the interface says, "There should be a function like this!"

However, the interface does not have the same meaning as the header file. Because the interface is a promise that many classes have to keep...

Learn about the interface, too. A similar abstract class (abstract class).

The funny thing is that C++ doesn't have an interface, so it's a class.

There are times when it is a little uncomfortable to define the class as a whole. So in C#, the language next to Java, there is a thing called partial class. Of course, it's different from the header file you mentioned..

Classes declared as partial class cannot be separated by definitions, but you can split the same class and write it to a different file. These classes are combined into one when compiling.

Duck 1.cs

partial class duck {
    public void quack();
}

Duck 2.cs

partial class duck {
    public void waddle();
}

Compilation results

class duck {
    public void quack();
    public void waddle();
}


2022-09-22 18:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.