HI,
1. Because that's how it worked in C, so it works that way in C++ too. The C language used pointers heavily, and pointer arithmetic.
std::vector was invented to get around the problems that arrays had. std::vector is a templated class, not just a pointer to a hopefully contiguous area of memory. if you look at the documentation for std::vector you will see that it has some member functions, STL algorithms work with it, and perhaps the best thing, one can keep pushing more objects on the end, it automatically resizes.
2. there are 32 bit and 64 bit systems, they have 32 bit and 64 bit memory addresses respectively.
3. Partly because one should think in C++, not C: the two are quite different. Also:
https://stackoverflow.com/questions/2872543/printf-vs-cout-in-c
4. See num 3.
5. The reason one does this is because classes are reused. If you have code somewhere that needs to use a class, then
#include
the header file for it. The compiler can find the relevant cpp file by itself.
MyClass.hpp // the class declaration and definition
1 2 3 4 5 6 7 8 9 10 11
|
class MyClass {
public:
MyClass() = default;
MyClass( int IntegerArg);
double MyFunction(double FredArg);
private:
int Integer;
};
| |
MyClass.cpp function definitions
1 2 3 4 5 6 7 8 9 10 11 12
|
#pragma once // put header guards as well if you want
#include MyClass.hpp
MyClass::MyClass(const int IntegerArg)
: // colon introduces member initialization list
Integer{IntegerArg} // brace initialization, initializes Integer with IntegerArg value
{} // function body , do validation here
double MyClass::MyFunction(const double FredArg) {
return FredArg;
}
| |
I always forget to put header guards. I need to make it more of a habit. |
If you use an IDE, it should do it for you. Use the IDE to make a class, rather than just typing it into files. It should create both files for you as well, maybe other stuff too - you may be able to specify what member variable and functions you want - it could make function stubs for these.