I have been learning C++ for the past few days, and it seems a very interesting language to learn. But as a rookie in the area, I am finding a lot of information, some of it conflicting, which is mixing me up.
Regarding one of the many things I'd like to clear up is the structure of .h files and .cpp files. I know that header files are used to reference .cpp files, and therefore an advantage would be that once run, an application will not need to go through the .cpp files since they would be referenced in the .h files (I hope I got this correct!!)
I have no problems with OO programming, and understood may C++ concepts, but the syntax is still giving me a hard time. I'd like to implement a simple class called Car. This car will have a structure of three properties, make, model, color. I would like to instantiate this class from main and add values to various instances of Car.
I managed to do this when I declared everything in main as follows;
using namespace std;
c.make = "Toyota";
c.model = "Yaris";
c.color = "White";
... an advantage would be that once run, an application will not need to go through the .cpp files since they would be referenced in the .h files
C and C++ are not scripting languages, once the source files are compiled the text inside of them doesn't matter to the resulting binary in regard to execution. The extension of a source file is only for the benefit of the human looking at it but as a general rule .h and .hpp files are used for forward declaration where as .c and .cpp files are linked to and compiled.
Understanding the #include<...> preprocessor command may clear some things up for you. It literally tells the linker to prepend the contents of the file into that point of the object file it is building. If you visualise that and remember that a forward declaration tells the compiler to look in the V-table at run time for the objects definition then it starts to make more sense.
base on your code. i write a simply header file . just copy and paste will do . haha.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
string make , model , color; /*you should
not put it in public ,
not a good practice , try put in into
private data and access them by using accessor and mutator*/
#endif // car.h
so inside ur main.cpp , you should include the car.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*Why no include <iostream> because
in your head file u did it ,
when you include the header ,
the function and those include things
will be share together*/
cout << "Car details; " << c.make << " " << c.model << " " << c.color << endl;
In c++ you can't use something before is been declared.
So you declare functions for the type and number of parameters,
declare classes/structs for the sizeof and members,
and forward declare classes to say `the pointer points to a class, no to a function'
Later is the linker's job to direct the calls to functions and method to the appropriate code.
According to how do you link there are several options:
_ all the code is in the executable (you don't need to provide libs separately)
_ the code is in the libraries (that you need to provide).
You could also use a plug-in system (runtime load)
A virtual table is related to polymorphism.
A field is added to your class, is a pointer to the table of virtual methods.
At runtime that pointer is dereferenced in order to see which method to call, just like a function pointer.
Thank you everyone for your replies, what a good healthy community! :)
My main question is, how, and what is the best practice for using .h and .cpp files?
As you can see from this single thread, many disagreements rise, and this gets me confused as to which is the correct answer. I even find conflicting information in books! I can accept that there is no one best practice, but I think that I asked something basic (hope I'm not mistaken).
I can tackle simple Win32 console applications with relative ease, but using just once main.cpp class. For readability and standards conformity, I'd love to learn how to spread code over a variety of classes. If anyone can point me into the right direction, I don't mind reading or watching videos - and I really want to grasp .h and .cpp files before proceeding to other stuff :)