Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

C++ question: Just learning the language coming from Python... I'm trying to implement a Grid class but am a little unsure how to do it (still learning syntax...) How do I initiate it? Grid gridGenerator(10, 10) ? int gridGenerator = Grid(10,10)? I see the notation Grid name(someNum, someNum) and the 'Grid' part confuses me. Also, do I always need to initiate variables like this: int x; x = 5 or can I do: int x = 5 ? Thanks!

OpenStudy (anonymous):

sorry about the crappy punctuation... I lost some 'returns'.

OpenStudy (anonymous):

about the second part (declaring and defining variables): in C++ (and maybe in C), yes it is possible to declare variables and assign a value to them in the same line. So int x = 5; is legal :-D

OpenStudy (anonymous):

Yes, it is legal in C. Not only that, but Valgrind shows uninitialized variables as errors, and you can set up flags for gcc to do the same, I believe. At least, you can set it to raise warnings.

OpenStudy (anonymous):

Thanks for that. What is Valgrind? Compiler? I'm using Visual Studio... Anyone familiar with setting up/initiating Grids?

OpenStudy (anonymous):

My answer was more for agdgdgdgwngo. valgrind is a tool to check for memory leaks, heap usage, and some other stuff in C programs. I don't think you can use with sepples, but I never tried.

OpenStudy (shadowfiend):

The <int> part is what is called a template. They're a pretty complex beast, but in this case they're basically being used to say “I want a grid of ints”; this as opposed to a grid of, say, floats (which can store decimal numbers), or a grid of UI elements, etc. Because of the way C++ works, this: Grid<int> gridGenerator(10, 10); Is actually equivalent to saying: Grid<int> gridGenerator = Grid<int>(10,10); In fact, it may be the only way to say the above. So it's the equivalent of saying int x = 5.

OpenStudy (anonymous):

shadowfiend, thanks. That's exacly what I was after and it turns out the reason I had some trouble is due to not importing the library I needed. Thought the assignment files took care of it but they did not. I'm beginning to find that C++ is a much trickier beast than Python. Is a good technique to build a little at a time and test with compiling? It's a lot harder to debug without the translator as in Python, no? Any tricks? Thanks again.

OpenStudy (anonymous):

You're right; one reason Python was so much easier to learn/understand was because of the REPL/interpreter thing that came with it. If you want to debug your C++ programs, you should learn to use a debugger like gdb. Also, you can use the assert() macro from #include <cassert> or #include <assert.h>, which works similar to in Python http://www.cplusplus.com/reference/clibrary/cassert/assert/

OpenStudy (shadowfiend):

Absolutely would go for incremental work + compilation. In a perfect world, your IDE (Visual Studio) warns you of issues beforehand, but sometimes the world isn't perfect :) The upside of a compiler is that you get rid of certain types of errors before you run your program. The downside is it's a different workflow from just having an interpreter and REPL.

OpenStudy (anonymous):

I agree. Thus far I've found the compiler errors and incremental work are solving most of my problems. The hardest part is tracking down exactly what the compiler errors mean...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!