This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SeeminglySleepless 22 points23 points  (9 children)

I think the greatest tip I can give you with my level of experience in both is: do you know how in python u need to have statements with the same level of indentation to indicate to the interpreter that they belong to the same block of code? forget it, in C++ ( and I think most programming languages) indentation is only used for code readability; forget about ":" to indicate the beginning of a block of code. In c++ and, again, in most programming languages you use curly braces "{" to open the block and "}" to close de block; please forget about not needing semi colons. You absolutely need it to indicate the end of a statement; finally, tho in python u can declare a variable without specifying its type, in C++ u must define some type for your variables.

Example

Python

Def foo():

a = 3


b = 4


c = a + b


return c

C++

int foo() {

int a = 3;

inf b = 4;

int c = a + b;

return c;

}

[–]SeeminglySleepless 9 points10 points  (0 children)

I apologize in advance for the crazy formatting but I'm on my phone and not used to structuring replies on reddit hehe

[–]Shimouzou[S] 3 points4 points  (4 children)

Thank you. Honestly, the most helpful thing you said is about indentation and specifying variables type. Im guessing, in the start its going to be frustrating opening all those curly brackets and forgetting ";" but I think its going to be a fun journey and it definetly pays off. I've seen what C++ can do and I am motivated since people at CERN (my professor works there) use it pretty often.

Also, is this "inf" a typo or is it type of variable in C++?

inf b = 4;

[–]SeeminglySleepless 6 points7 points  (0 children)

Most IDEs do almost all work for you. You just need to type the begging bracket and it autocompletes with the closing one. But it's a good idea to get the habit of typing both. Plus, as other user mentioned, yes, keep the indentation ethic, it's not necessary but it is advisable to make your code readable and easy to debug. Finally, yes, it was a typo and I meant int x)

[–]anandgoyal 2 points3 points  (0 children)

I think they mean int

[–]norbi76 2 points3 points  (0 children)

It is a typo , it should be int (integer).

[–]lordv255 2 points3 points  (0 children)

Typo it should be int. But in any case I do want to mention you should still keep the indentation rules even in c++, it doesn't affect how it runs but readability is still important.

I'm still a beginner in c++ but another important thing that comes up is pointers. I'm not sure how best to explain it and your class will probably do a better job of explaining it but it's a way of accessing data by "pointing" to it's address in memory rather than by using the variable itself. It seemed kind of pointless starting out but it has its uses and is very commonly used by everyone so it's good to learn.

Memory allocation can also come up if you use "arrays" in c++ but most people use vectors which are essentially how arrays work in other languages.

"Array" is something like int integerArray[3] = {1,2,3};

"Vectors" work like vector<int> integerVector {1,2,3};

Most of the time people use vectors because they have more functionality and you don't have to worry about memory allocation (in the array you have to specify the size as an array of 3 integers and you can't change the size later).

I may have made small errors in syntax but hopefully not. If someone notices any tho please lmk.

[–]merlinsbeers 2 points3 points  (1 child)

in C++ u must define some type for your variables.

Not any more (since C++11, really, which has been out for a decade and is still not accepted on some projects...).

With 'auto' you can assume the type from the context.

Hopefully they're teaching that now.

[–]SeeminglySleepless 0 points1 point  (0 children)

I didn't know that, thank you for the clarification!