Is it good C++ to define classes in .cpp file? - c++

I am a beginner in C++. I have a question regarding C++ design.
My file contains A,B,C,D,E class definitions. Class A contains the API which is used by other applications. I have defined this in a .h file. Classes B,C,D & E define concrete classes by inheriting an abstract class which is provided by some library. These definitions are not used by any external application, but only used by class A.
I have seen online that all the class definitions are provided in an .h file and the function implementations in a .cpp file. My question here is, even though class B,C,D & E definitions are not used externally by anyone, should I define them in the .h file? If I do define them there anyway, I cannot expose them to other applications, right?

If a class is only used locally in one module, you can declare it in the .cpp file. This is actually good practice; don't expose more than necessary.
In a case where you need to define a class (or function, etc.) in a header (for example, to share it between several related .cpp file) but you do not want to expose it publicly, you can have a separate, private header file which is only included in the relevant places, but is not made public. This can be hinted at by appending "private" to the header file name.

Related

C++ helper functions in class, how to use them?

In my class in C++ I want to write a helper function (Which external users can't see or use) and found 2 ways to do that:
1) To declare it as private in the .h file and write the implementation in .cpp file.
2) To write the implementation directly in .cpp file without declaring it in the .h file.
What's the correct way, the difference or the advantage one have on the other?
Since you are not exposing this function through your interface, you should not declare it in the public header file. If you use it in a single cpp file, declare it in an anonymous namespace in that cpp file. If you use this function in multiple cpp files but you would still like to not make it a part of your interface, you can create internal header files that you #include in your source files, but aren't a part of your library's public interface.
The way is to create a class A that would be visible to the public, then create a descendant class B of this class and implement it privately. Then, when you are asked to create an A*, create a B* which is also an A* and return it. Your code sees a B* but the user of the class sees A* and may only access methods and variables declared in A. You also need to create/destroy your pointers with some function rather than with new/delete.
Pretty much like how COM implements IUnknown and stuff.

Is the Object-Class reationship in C++ different from other languages, say Java and C#?

I've been programming in Java and C# for a while and I guess I always thought of objects and classes as being one and the same but the C++ course I'm following has the classes in .h files and the objects .cpp files. Is this typically how a C++ program is structured? Is the class/object relationship different than other OOP's?
You might as well see classes declarations in a .cpp file, e.g., if the one who wrote this file decided to use some help-classes that weren't supposed to be part of the API presented in the .h file.
Also, you might see in a .h file an object that is being held within a class as its private member.
Indeed, Java "pushes" you towards a pretty strict source and class files organization, that in many cases will match your folders & files organization. C++ allows you much more freedom in this regard, but however has the notion of includeing header files.
The relationship between the class and the object is not different to other languages. The object is an instance of the class.
So for example:
Car myCar("licence-plate123");
Car is the class. myCar is an instance of the Class Car and therefore the object.
To seperate the Class declaration is more like a convention and has nothing to do with objects and classes.
The .h-File contains the class description. There you can see, which methods the class provides, and what parameters they need. The .cpp-Files describe how each method works. So the actual source-code.
But as I have already said: This is more like a convention. You can also write all of it in the .h-File. It's not pretty but it works

Writing classes in different file in c++

I am writing a c++ program which consists of 8 classes.
1.Vehicle
derived class 1. Seat
2.Door
2.Passenger
3.Settings
derived class 1. SeatSettings
2. InfotainmentSettings
4.PassengerLocation
I don't have any previous experience in c++. While writing the program should I write all the classes in a single .cpp file or in different .cpp file. If as different .cpp file then parent and derived class should be in same file ?.
Really you can write classes in any place of your programs. Inside h files, cpp files, inside other classes, inside methods and so on. C++ give complete control under source code of the application.
In general way you describe interface of a class in a h files, and implement methods in a cpp file. But you can implement inline or template methods in the h file.
I think that you should start from Classes guide, and when you will have knowledges about capability you can select right way to implement your classes.
You should make a separate .h and .cpp file pair for each class you implement in c++ unless it is a template class or struct
Separate the classes but keep the parent and derived classes in the same file.
Put your class declarations in .h files and the implementations in .cpp files and then include the .h file in its corresponding .cpp file.

Is defining classes in headers a good practice?

I have read that defining functions in headers is a bad practice, but how about classes? is defining them in the header files fine?
Unless this class is a pure template class or other class which is supposed to be inline, you'd better put class implementation into cpp files instead of header files. In a word, put interface in header files, while put implementations in cpp files.
P.S.
As #jogojapan said, I'm talking about the class implementation instead of class definition. That's because despite that OP is talking about class definition, I strongly doubt that he's actually meaning class implementation.

C++ project layout

I am completely confused as to the proper way to layout a C++ project.
I had all my classes in separate .cpp files, with their definitions in .h files. I then had one "header.h" which contained all the class headers, external dependencies and a few other things. But I wasn't able to use class names in the header files, where I needed to declare a pointer to one.
Can someone please explain the proper object-orientated layout for a C++ project.
You can fix the problem "wasn't able to use class names in the header files, where I needed to declare a pointer to one" by using forward class declarations, like:
class myClass;
However, having every class include a header.h that then includes every class is overkill. Instead, you should have each class specifically include only the classes and external dependencies that it actually needs.