Re: First to learn C if learning C++?
On 2014-10-12 16:56, Emanuel Berg wrote:
JiiPee <no@notvalid.com> writes:
yes. On videos the top C++ people they always say
the code should be as short as possible and simple.
Using C++ and classes does that.
OO and programming with classes does not make the code
"shorter".
OO makes programs "simpler" only when the purpose of
the program lend itself naturally to modelling: like
simulation, games, certain abstract situations with
clear building blocks (e.g., a hierarchical
scheduler), and so on. OO is not "always better".
Also: Short code is not always an advantage. Most
often it is because it is fast to read and write. But
sometimes short code gets really cryptical and
difficult to maintain. Short code doesn't imply
execution speed. It can actually be the other way
around as there isn't any optimization of obscure
hacks.
So lets's compare. :-)
std::string a = "Sample string";
std::string b = a;
or
char a[] = "Sample string";
char* b = malloc(strlen(a) + 1);
strcpy(b, a);
/* and sometimes later: */
free(b);
For example for-loop becomes very short some
times... with C you cannot do such things.
Yes you can: for-loop is the same in C and C++ and
several other languages as well.
Newer C++ has better for loops as well, like:
for (auto x : v)
std::cout << x;
will output all elements in the sequence v, like a vector, or a string
or an array, or ...
Bo Persson