Learn to Code via Tutorials on Repl.it

← Back to all posts
C++: An Introduction
ipastrano (46)

Welcome to my c++ introduction! I hope that I do a good job explaining this.

EDIT: I have a new lesson for all to see on Repl talk!

I will post something every Sunday I will try to post something every Sunday.

EDIT: Thanks so much for the cycles(upvotes). It means a lot to me and I will definitely post on Saturday!

Fellow c++ers: If something is incorrect or can be explained better, tell me!

People reading this: Questions? Comment on this post and I will get back to you as soon as possible.

OK, let's get started!

Including files using preprocessor directives

  • If you've learned another language before, you probably know that you need to include files.

  • If you haven't, picture this: You are in a library, and you need to borrow a book. You use it in your book report.

  • To include a file, use this:
#include <file>
  • Your first file (credit to @DynamicSquid for making this more clear):
#include <iostream> // used for input and output to the screen. This is a 
//basic header file.
  • Comments(credit to @Highwayman for asking me to add this)
  • Comments are used to document what the code does. They won't change the program
    Small offshoot
  • Person: Why use comments if they don't change the program?
  • Me: OK, imagine this: You are working for a programming company and you send your giant program(let's say 100,000 lines) to the big boss for review. If you didn't add comments, they would have to figure out all 100,000 lines by themselves!
  • Person: Fine, fine, but I want to just program for fun. No one will look at my work, just me. So do I have to put comments then?
  • Me: Of course. Say you have a program you've worked on for a looong time(the same 100,000 lines of code). It finally works! Now, you leave it for 2 months or so. Then, you realize that there's something more you want to add to it, so you go back. Now, you have to retrace all your code again! (Real life experience: I ended up deleting some code I had been working on because I didn't understand it after coming back from a vacation. Please don't make this mistake!)
    end of offshoot
  • Ok, so anyway, let's look at the difference between single and multi-line comments.
//This is a single-line comment.
//For each new line you make,
//You need a new pair of forward slashes(//) to make it
//a comment.
/* This is a multi line comment.
You can have
as
many
lines as you want, as long as it is between the /* and the
thing you will see right now. */

That's all folks! Tune in next week to learn about the main function!

(insert jazzy music here)

Commentshotnewtop
Highwayman (672)

Uh, quick thing: to fix your use of the markdown, just put a space in between the hashes and the actual thing you want to be shown as a header. Also do the same for the pluses in your list. When making a code black, you can actually have it have coding highlighting by putting the name of the language next to the top beginning block thing.
~~~c++

~~~
or
```c++

```

and
#### header text

Another thing, simply stating that an entire line is a comment can be a tad confusing for a beginner who’s never seen code or comments before, so for those of you who are still a little bit confused: what the OP is talking about and showing you is called a single line comment. comments don’t affect you code. To begin a single line comment in c++, simply place 2 forward slashes(//) in front of you comment like the OP demonstrated.

#include <iostream>
// fhdheu blah blah blah. Notice how this 'comment' will not stop until you
// make a new line, so be careful to always put it at the end of a line of
// code, or on a line by itself. 
ipastrano (46)

@Highwayman Thanks for telling me, I will add this info and fix the Markdown. I'm new to Markdown, so tips like these are appreciated :D

Highwayman (672)

@ipastrano Awesome! Actually if you ever forget stuff about the markdown, there’s a link to a guide in the bottom right of every post that’s made (I use it constantly lol). Also thank’s for making this series, there’s practically no other tutorials on c++ here,

ipastrano (46)

@Highwayman Yeah, I looked at the link but forgot some stuff! Also, yeah, I haven't really seen a tutorial on c++, even though a new version may come out this year.

DynamicSquid (22)

@ipastrano Yes! New version, can't wait! I'm also looking forward on getting C++ Primer 6th edition coming out in November. It's said to be a really good book

ipastrano (46)

@DynamicSquid Oh cool, and I hope repl will support this new version soon.

Highwayman (672)

@ipastrano I honestly don’t know what to think. I’m excited that it coming, but I am also kinda depressed, because I am SO. FAR. BEHIND. I haven’t even finished even figuring out what there was in c++11 and under 😞. But besides that I am ready for that sh** lol.

Highwayman (672)

@ipastrano didn’t clang and gcc start supporting most of c++20’s features a while ago though or something? Maybe you can use c++20 if you pass the right flag..

ipastrano (46)

@Highwayman hehehe me too, for example, I keep trying to use nullptr in regular c++ even though it came in c++ 11 lol.

Highwayman (672)

@ipastrano yeah lol, so much stuff to learn.. XD

ipastrano (46)

@Highwayman yep. so much stuff to learn

Highwayman (672)

@ipastrano it supports c++20! Yes! Happy 😊

Highwayman (672)

@ipastrano I thought you said that it did sorry. Does it? Wait I have to look this up now ahhhh oh boy.

ipastrano (46)

@Highwayman yeah, i can't find it. It's ok, easy mistake. :D

If you do find it, great!

Highwayman (672)

@ipastrano lol, yeah I guess that was just wishful thinking on my part..

ipastrano (46)

@Highwayman yeah, i used command line and looked at repl's list of languages, sorry. Hopefully they support it soon tho

Highwayman (672)

@ipastrano dam. Welp same sentiments then, just waiting for support to come to repl.it. :)

ipastrano (46)

@student_SaiSai /* Hello! I like your comment style :)*/

DynamicSquid (22)

Don't bother showing beginners the other header files. It just confuses them more. Just show them the input/output stream, and explain what that does.

The other headers will come naturally as they progress.

ipastrano (46)

@DynamicSquid OK, I'll take them out. Thanks!

ipastrano (46)

This is the OP. I have changed the lesson date to Sunday, because that date works better for me!

ThePianist (0)

/ Can't wait
For next weeks
Lessons!
/

ipastrano (46)

@ThePianist Thanks! I have the following lesson up, you can go take a look.
If you want the asterisks to appear(asterisks around make italics, use \*
I play piano too!
Thank you so much for the support!

ipastrano (46)

Hey! I made a new post today(one day late) if you want to see it.

DynamicSquid (22)

Oh, and btw, can't wait for next week!

ipastrano (46)

@DynamicSquid :D Thank you! If I can't do it next week, I will comment here on Friday.

DynamicSquid (22)

@ipastrano K, looking forward to it!