A beginner on a third-party chat recently told me that they planned on buying a C book, with plans on moving onto C++
These are the arguments they've given me:
- C is simpler/more elegant
- C is just C++ without OOP
- C++ will "spoil" you
- C++ forces things on you |
Let me tackle these individually:
C is simpler/more elegant
I have to admit I chuckled a bit when I heard this. C may be simpler (it has much, much less features than C++ does), but it's far from being elegant.
Here are two examples of C and C++ that do pretty much the same thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
// C++
class Object
{
public:
void Output()
{
std::cout << x << "\n";
}
void DoTask()
{
x = 0;
}
private:
int x;
};
/////////////////////////////////////////
// C
struct object_t
{
// Data and stuff
int x;
};
object_t* CreateObject()
{
// While allocating memory dynamically here is not necessary,
// for some reason I've seen (a lot of) C code that abuses it...
object_t* object = (object_t*)malloc(sizeof(object_t));
return object;
}
void DestroyObject(object_t* object)
{
// Do some deinitialization
free(object);
}
void ObjectOutput(object_t* object)
{
printf("%i%s", object->x, "\n");
}
void ObjectDoTask(object_t* object)
{
object->x = 0;
}
| |
This is how you would use them, respectively:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// C++
int main()
{
Object object;
object.DoTask();
object.Output();
return 0;
}
// C
int main()
{
object_t* object = CreateObject();
ObjectDoTask(object);
ObjectOutput(object);
DestroyObject(object);
return 0;
}
| |
Which looks more elegant to you? Simplicity isn't always better.
C is just C++ without OOP
The person I was arguing with claimed he knew both C++ and C, but this (along with the fact that they were pondering on whether to learn C++ or C first?) definitely showed me that they were a beginner.
Idiotmatic C and C++ are completely different. Sure, your archaic C code will most likely work with little (if any) change, but it's going to be frowned upon by other C++ programmers. This is the same in C. Don't write C like a C++ programmer. This doesn't mean to abuse OOP in C++. i.e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// Instead of
class Vector2
{
public:
int GetX() const{return x;}
int GetY() const{return y;}
void SetX(int x) {this->x = x;}
void SetY(int y) {this->y = y;}
private:
int x;
int y;
};
// Just use this
struct vector2_t
{
int x;
int y;
};
| |
C++ will "spoil" you
I'm not going to lie, my first reaction was:
"Wut?"
There's a difference between spoiling and having a convenience feature.
Let's say you own a bike shop. You have hired have a human worker. His productivity is low, and you have the choice to replace him with a new machine that can put out 4 times as many bikes as the human worker, and costs less to maintain. Which would you choose? (Excuse my bad analogy, but I think it gets the point across)
C++ forces things on you
...
C++ doesn't force you to do anything. If you want, you can write in pure C and your program will run fine. You don't pay for what you don't use. If you want to write performance-critical code, then restrict yourself to a special subset of C++ if you want. It's not uncommon that not every feature of C++ is exploited in code.
__________________________________________________
I'm going to say this, and I have a feeling that a lot of programmers will disagree with me. By learning C++, you're going to eventually learn idiomatic C if you want to do anything non-trivial. There is a 99.9 percent chance that you're eventually going to interface with C libraries at least once. After you interface with several, you're going to quickly up on idiomatic C (provided they're written that way). It should be trivial to write a C application that's not frowned upon by C programmers once you're a reasonably knowledgeable in C++.
C isn't going to help you at all. It's just a waste of time to learn. Most of your time picking up C++ is going to be spent "unlearning" certain practices when writing C code.
The discussion ended with the beginner angrily leaving the discussion, so I've created this topic in hopes that they'd see it. If you change your mind, send me a PM.
(Sorry for this low-quality post. I'm tired and have to do a few things, I had a bit of spare time so I decided to write this)