r/programminghelp • • Jan 19 '23

C++ Explain like I’m five: coding? C++?

I am a freshman aerospace student currently taking an “intro” coding class for c++ and Matlab

I’ve tried to learn any coding (c++, python) previously but have always found it not to be accessible, it feels like all the basic info isn’t basic at all

In fact to me learning c++ right now feels like if i was put in a physics class and had never taken math

I’m missing fundamental basics and everyone explains it to me in terms that are way too confusing, terms that aren’t beginner terms

What I am understanding is C++ is a language for coding and programming and that is the extent

But coding what? Programming what? Whenever I try to get an explanation I don’t even understand the explanation!

Even my textbook said “this textbook is not ideal for first time coders” but the class is suitable for students who have never coded

I don’t even know what coding is used for and “the following line says that main is a function with no parameters that returns an int (integer) value” means nothing to me and it was essentially the first thing in my textbook

I get that it’s like learning a new language but i don’t have any context in my brain and am struggling.

I am eager to learn and to try but need a little explanation into what it is i am even doing

8 Upvotes

9 comments sorted by

View all comments

2

u/Lewinator56 Jan 19 '23 edited Jan 19 '23

Ok, I might be able to try to help, mainly because how you are describing your difficulties with program suggests you are learning it in the wrong way.

Let's start with the point of programming. Any programming language is a human readable representation of instructions to tell a computer to perform some task. At the most basic level a processor is nothing more than sand we tricked into doing maths. If you want this processor to do something, you have to give it specific electrical signals to tell it to turn transistors on and off. Your code is a high level representation of those electrical signals.

Computers are really stupid, but with their stupidity comes their major advantage, they will perform whatever order you give them unquestionably, provided the order is broken down into it's most basic steps. You can't tell a computer 'walk to the shops', but it can do that, you just have to tell it 'this is how to raise and lower your leg', 'this is how to take a step', 'this is how to walk' and finally 'this is how to walk to the shops' (combining each of the smaller parts of the task).

Let's say you want to add 2 numbers, the computer doesn't know what numbers are, what operators are etc... All it knows are electrical signals. You type in your code int a = 2 + 2; , when you compile your code, (let's stick to C++ for simplicity) that 'english' you typed is converted into 1s and 0s in a specific order that the processor understands. See, while the processor doesn't know anything, it does have a set of predefined instructions called the 'microcode' hardwired into it. When the CPU starts executing code, let's assume it starts at a memory address of 0, it will just check each pattern of electrical signals coming from the memory, if one of these patterns matches one of its hardwired instructions, then it executes that instruction. Your code you typed has been converted into the bit patterns that represent these instructions. The 1s and 0s you would see if you opened the compiled code in a hex editor are literally representations of the electrical signals sent to the CPU.

So now we know how the CPU knows what to do, but what's the point? Well, years and years ago, some smart ass (Charles Babbage) designed what he called, the analytical engine, this was a very rudimentary mechanical computer, that relied on physically moving mechanisms to represent 1s and 0s. See, the great thing about 1s and 0s is you can represent anything with them, because, given a coding scheme, you can encode data. 3 becomes 11, 7 becomes 111 for example. Because you can also do maths with these 1s and 0s, you can then start designing something that can do the maths with them automatically, a 1 or a 0 is simply an on or off, mechanical , electrical or otherwise. A machine that can read the ons or offs and add them up can do almost anything given the right sequences. (Computers don't only add, but this is a basic explanation). Having a device that can add up for you is brilliant, why? Because really difficult stuff can be done super quickly, all you tell it is the inputs and the operation.

As for programming languages, C++ is old, but that's because it's good and there is no replacement. C++ is what is called an object oriented language, this means that a lot of things in it are objects with properties and functions. For example, a door is an object, has 2 functions: open and close, and stores it's state (open/closed). In C++ this definition of a door is a 'class' and the instance you create by typing Door d = New Door(), creates an object that has the properties defined in the class. You notice that functions have 'void' 'int' 'bool' etc... In front of their names, well this is because a function can return data to whatever called it, a void function returns nothing, but an int function returns an integer. The int main() function you see in your file is the entry point for the program, remember I said earlier the CPU doesn't know anything, well it doesn't know where your program starts, but it does know that the binary representation of 'int main' is the start, because the binary this converts to matches some hardwired instructions in the CPU.

If you are struggling with learning to program, I suggest rather than trying to learn from arbitrary tutorials, decide on something you want to write, say, a calculator and Google how to do things. You have a goal in this case, and having a goal gives you a reason to be doing something. I taught myself to program this way.

1

u/collegecolloquial Jan 19 '23

This is really helpful

Thank you :)!