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;
#include <iostream>
#include <sstream>
using namespace std;
class Car
{
public:
string make;
string model;
string color;
};
int main()
{
Car c;
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.
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.
What? What have forward declaration to do with the v-table?
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
#IFNDEF CAR_H
#DEFINE CAR_H
#include <iostream>
#include <string>
usingnamespace std;
class Car{
public:
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
#include "car.h"
/*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*/
int main(){
Car c;
c.make="Toyota";
c.model="Yaris";
c.color="red";
cout << "Car details; " << c.make << " " << c.model << " " << c.color << endl;
return 0;
}
Do you think the program just magically knows how to use something that hasn't been defined yet? The V-table is what holds the addresses of objects that have to be resolved at run-time.
I thought that was what the linker stage was doing. I have only heard the word v-table being used to describe the mechanism that is used to decide what implementation of a virtual function to call.
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 :)