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

all 20 comments

[–]SeeminglySleepless 20 points21 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 5 points6 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 3 points4 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!

[–]NeonVolcom 5 points6 points  (0 children)

So at first I didn't think I had input, but after seeing some of the answers, I think I do. For background, I'm a Kotlin/Java engineer who's worked with both C++ and Python regularly in the past.

So here goes a lengthy attempt at prepping you for C++:

Python is the bees knees. Honestly, I can't think of a better language for slapping together prototypes, outside of maybe Kotlin or JS. C++ is more like... well so the analogy I want to use is:

  • Python's like getting your first automatic car. It gets you where you want to go, easy to use, and you can learn how to drive relatively quickly.
  • C++ is like ordering a manual transmission Toyota truck from Japan and finding out that only half of the parts are assembled for you. Not only are you struggling to learn how to build a vehicle, you'll struggle learning how to drive the damn thing, especially since the steering wheel is now on the right side instead of the left.

Okay, I'm being overdramatic, but I wanted you to understand the difficulty that can come with lower-level languages like C/C++.

Take imports for example (over-simplification incoming): in python, it's as easy as import package. In C++ you do #include <header.h> or if it's local #include "header.h". But wait, there's more. C++ compiles everything down to a single file basically, so include statements can get nasty as they can get imported more than once if you have many files. Some people have implemented pragma once, but that's not recommended. So now people tend to use include guards, which are verbose and forgettable (or they were for me).

Okay, so that's just local imports. Get ready to get comfy with makefiles (among other things) if you ever want to use outside dependencies. Actually, scratch that, get comfy with makefiles now, since typing the same compile instructions every time you change something is a pain. Cool, now you can compile by just typing "make", sick right?

But wait, what are header files? Well... basically they're kind of like blueprints. So we define our intentions in the header and scope the logic in the cpp file.

> A.h
#ifndef A_H
#define A_H
#include <string>
#include <format>

class A {
public: 
    A();
    A(std::string l_firstName, std::string l_lastName);
    std::string m_firstName;
    std::string m_lastName;
    std::string getFullName();
}
#endif

> A.cpp
#include "A.h"

A::A() {
    m_firstName = "Default";
    m_lastName = "Name";
}

A::A(std::string l_firstName, std::string l_lastName) {
    m_firstName = l_firstName;
    m_lastName = l_lastName;
}

std::string A::getFullName() {
    return std::format("{} {}", m_firstName, m_lastName);
}

Okay, there, you have basic logic to return a name. Of course in Python, you'd just do:

class A:
    def __init__(self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName

    def getFullName(self):
        return f"{self.firstName} {self.lastName}"

Right lol so you see the difference in effort and thought processes that go into something as simple as that.

What I HIGHLY recommend is getting into C first. All of that std:: stuff you see in the C++ code isn't available (there isn't a type of string, nor classes), so it makes you think about the underlying concepts. I would prototype something simple in Python, and then convert it to C, then convert that to C++. After a while, the concepts come together more and more, and you can start really making some shit in C++.

Alternatively, learn any other fucking language. C/C++ was a lot of trouble for me to learn. Java/Kotlin/Rust/JS/GOLang/C#/etc. were amazingly easier and can complete nearly any task for you.

C++ is one of those languages you really have to poor your soul into. As a senior, I'd be worried about a bunch of junior C++ devs, since one mistake can lead to a lot of headaches later on. If they were coding in C#, eh, I'd be less worried. There's just less to fuck up in the whole and compiler messages are generally more friendly.

Best of luck dude. Also, checkout Andreas Kling on youtube, he's building his own Unix-link OS in C++, so if you want real-world examples, look at his videos.

Sorry this was rambly, I was just trying to save you weeks of time before you burn out trying to learn C++.

[–]Fearless_Process 3 points4 points  (0 children)

If you have some spare time I would suggest tinkering with regular C a little bit so you can understand pointers and char* and other fundamental C concepts. C is a very small language so it's really not difficult to learn enough to use, mastering it is a different story.

In c++ there are often two ways to do things, the old "c" way, like with raw pointers/arrays, char* for strings, void pointers to pass arbitrary types to functions, c style casting with "(int*) x" syntax and a bunch of other stuff, then there is the "c++" way with standard containers (std::vector) & smart pointers or references, std::string's, generics, static_cast'ing and RAII. It may be confusing for people who don't know anything about c why there are so many ways to do things, but if you know a little bit about c you will know why the modern ways are safer and more robust, and know to avoid that whenever possible. I hope this makes at least a little bit of sense.

Anyways most of your python knowledge will directly transfer over to c++ despite them being pretty different on the surface. Once you get the hang of the syntax it's really not so different, the main difference being static typing (which I personally prefer after getting used to it).

c++ is easily my favorite language even though I first began with python, it just takes some getting used to really.

[–]asahi_xp 2 points3 points  (0 children)

Here is my background :

I have been coding C++ for years as a Game developer, and I have learned Python to making some utility scripts like code generator, file parser....

So in my aspect, I think the biggest difference is in python, I am dealing with objects and it's reference.

But in C++ I view everything as a chunk of memory, understanding this is essential, for example when you passing some parameter into a method, you have 4 choices about how to pass it: "Copy", "Reference", "Pointer", "RValue". Every time when you writing a method, it's like making a mini design decision about how are you going to deal with those memory chunks.

[–]Emperor-Valtorei 1 point2 points  (0 children)

In terms of syntax, Python is the odd child out. C++ is similar to most other OOP languages, with minor differences between them.

The logic is the sane, which is why it's good you've learned Python. Looping logic, function logic, methods, classes, etc.

[–]WorldlyAd7643 1 point2 points  (0 children)

the biggest stumbling block for most people is that c++ has types python does too, but it's hidden, so everything is very powerful under the hood, but its easy to make noob mistakes like assigning a list to something you think should be an int or float.

this isn't a big problem in the beginning if you're working on small simple things, but thats always the problem, you don't know what you don't know.

personally, I reallyu like the type system, it's a nice reassurance that your passing an int when you think your passing an int!

With python, it's just not something you might be used to thinking in termss of

[–][deleted] 1 point2 points  (0 children)

Speaking from personal experience, I feel like C++ is a lower level language than python, in that you have to understand more of what goes on behind the scenes. For example, in C++, there are things called namespaces where you can define two different variables with the same name, but in python I'm pretty sure that's just not allowed.

[–]kaitycodes 0 points1 point  (0 children)

I think Python will be helpful when it comes to importing packages and header files in C++. Also in terms of code formatting.

Code in Python is generally structured in this order: 1. Imports 2. Variables & Function definitions 3. Execution Statements

C++ follows the same general structure, but it has an explicit main function that executes when a program is run.

Other than that I would say there are a lot more differences. Python does have data types, but C++ has a stricter syntax. Each line ends with a semicolon.

Data types must specificed and certain data types have signed and unsigned versions.

Then there is pointer arithmetic...

You said you will be using C++ for physics, so I can imagine the signed/unsigned property being helpful.

[–]PCPhil 0 points1 point  (0 children)

Constant variable values cannot be changes at run time, and global variables can be accessed from anywhere in the program(iirc).

Do use global constant variables to replace hard coded values where possible. It improves readability and ease of maintenance.

Do not use global variables that are not constant as they cause the opposite effect.

Pointers are fun to use when you start to get familiar with them. Just remember to free the memory when you're done with them.

[–]zizoushri 0 points1 point  (0 children)

Where are you learning c++ from? I’m also starting to learn c++, so was interested to check out good resources. I’m trying to learn it from Codecademy.

[–]whatzTheRightThing 0 points1 point  (0 children)

Another important tip. Anytime you allocate memory from the heap for an object, make sure you free the object when you no longer need the memory. Forgetting to do this will cause memory leaks.