[First Program] Base Convertor

Hey guys, this is my first program i have written in C++ tell me what you think!!!
It is a base convertor (yes, i know there is a method that does this, thought it would be good practice.)

Im a intermediate to experienced java programmer, so my first program in C++ was pretty easy. Alot of the syntax is the same, the only thing i dont like about C++ is the way the arrays are setup, so as you can see, i used vectors(instead of lists, for the vector.at()).

Any suggestions for my next C++ program/ comments on this one are much appreciated. Im not much familiar with pointers, i dont really see a point to them, if somebody could explain why i would use a pointer instead of accessing a var directly, that would be great too.

variables can be passed by value (the way you're accustomed to) or by reference (by using references and pointers).

Imagine passing around vectors/arrays of many large objects by value. That's a lot of info for the computer to handle. Much faster way is to pass by reference, meaning the *addresses* of those large objects can be used to manipulate them, without shuffling all their data as well.

It's mostly a more efficient (in many cases, much more efficient) way of dealing with objects.

There's also the difference between stack & heap memory, each has their merits, and i suggest you read up on that as well to help you understand better when to use pointers as opposed to references.

EDIT: actually, in java things are passed by reference automatically, except for primitives. So java references are much like c++ pointers. So, think of it this way:

Ahh, i get it. But for most programs on a computer, you wont really need them will you? Will you notice a difference in most programs? Ive had to deal with java heap memory, but that is when im dealing with an extremely large image(like 100,000X100 pixels).

It would probably be good to get to know them, and i guess if your making a game, you will have so much data that u need pointers for the game to run correctly without lag, is that right?
Thanks for you input btw.

The advantage of pass-by-pointer has been superseded by pass-by-reference, so that's not much of a reason any more (some code that is intended for both C/C++ will have to use pointers). As far as the stack goes, generally you get 1MB of stack (although you can dig into the system to change that), so anything that plans to use more than that is generally going to need memory from the heap.

The containers mean we don't have to use new (and pointers) nearly so much as the equivalent C code for things like lists.

If you intend to start writing classes that use polymorphism, you'll need pointers for that.

EDIT: About the code: it seems like it would be a better thing to do the reverse in decToBase instead of in each of the other functions, especially since decToBase is callable from user code and they might get all confused. Note that reverse is built into the standard library, so you would probably want to do that instead of hand-rolling your own. (And if you do want to roll your own, there is reverse_iterator in a vector.)

Perhaps in trivial programs it doesn't make a huge performance difference, but if you're going to continue to program in c++, i'd sure invest the time in getting your brain wrapped around pointers/references.

I'd estimate the vast majority of programs that were written in c++ that make it on to your computer make heavy use of pointers.

Along the same lines as the previous comment: in several places you're returning a vector from a function. This makes sense in Java, but in C++, the vector is actually stored on the stack (the way you've written it). So you end up making at least one complete copy of the vector, which isn't very efficient at all.

There are a number of ways of getting around this. One way is that you can dynamically allocate the vector, putting it on the heap:

That's pretty awkward sometimes, though. Another way you can do it is to pass in the vector as a parameter, and then modify it that way.

Code:

void functionWhichReturnsAVector(vector<int> &v) {
// ...
}

Just food for thought . . . .

Also, instead of using 48, you can use the literal '0' directly. This is because C++ has much looser type coersion rules; you can take a char number and have it automatically converted to an int; you can use an int in an if statement:

Code:

int x = 3;
if(x) {
// ...
}
// equivalent to
// if(x != 0)

You can initialize doubles to ints and have the int automatically converted, etc. It's very convenient in some ways, but if you start playing around with that, definitely enable compiler warnings (yes, you typically have to enable them in C++). In fact, do that anyway.

dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell