Learn to Code via Tutorials on Repl.it!

← Back to all posts
C++ Tutorial: Day 1
h
HahaYes (1221)

HahaYes' Very Good C++ Tutorial

So what is C++?

C++ is a general purpose programming language that is basically based off of C

It was created by Bjarne Stroustrup, and C++ is known as "C with Classes"

Quick Notes:

C++ is a statically typed language, which means that you have to declare if a variable is
a float, or a char, or a number. (Will elaborate on Day 2 or 3)

C++ is kinda a hard language, so there will be many chapters of this "series"
HahayEs

C++ is a FAST language, which makes it great for coding games, making software, and sending
stuff to space! (and fast food) yum yum

2nd fastest language (if you don't count machine code)

Each instruction in C++ MUST end with a semicolon


Alright! Day 1 of how many days of C++ Bootcamp!

A simple "Hello World" program in C++

#include <iostream>
// this is a comment
using namespace std; 
int main()
{
	cout << "Hello World" << endl;
	return 0;
}

The #include <iostream> is what we call a header. C++ allows a bunch of headers,
but because I'm lazy, I use #include <bits/stc++.h> because it basically is a header file that
has every standard library, including #include <iostream>

(C++ ignores blank lines btw)

The // symbol: the C++ version of a comment. A comment will not be executed.

The using namespace std; using namespace std means that you are going to use classes or functions (if any) from "std" namespace, so you don't have to explicitly call the namespace to access them.
Some people say that using namespace std; will become a bad habit, but I don't really see anything good or bad about it.

For example, if you don't have using namespace std; this is what you have to do to make a "hello world" prompt.

#include <bits/stdc++.h>
// this is a comment
using namespace std;
int main()
{
	std::cout << "Hello World" << endl;
	return 0;
}

(C++ also ignores tabs and spaces, but tabs and spaces help your code look cleaner)

The int main() is used as the "entry point" of a program. (Someone please help me elaborate on this)

The brackets, {} is where the program will be executed. (Not the French Revolution type, but like computer programming type.)
These brackets indicate the beginning and the end of a function.

What does cout << "" << endl; do?
well, cout basically is how the computer will display the output, and the quotation marks ("") are where you put in your characters. endl; tells the computer to create a new line. But, I prefer to use "\n" because it is a better way to create a new line.

The << is a symbol for output, and >> is the symbol for input.

The return 0; is basically the "exit" of a function, signaling that the program has ended.
(0 can be replaced with -1 or 1, but that is slightly more advanced, and I will cover that at a later date.)

That's it for Day 1!

Please upvote!

I cannot confirm that I can do daily tutorials, but I will try my best to upload one by 12:00 in the morning NYC time.

Any typos that need to be changed or suggestions are very welcome.

Thanks! HahaYes

EDIT: Day 2 COMING SOON

Commentshotnewtop
CodingCactus (3039)

nice, don't know any c++ so this is pretty cool. What does << do?

HahaYes (1221)

@CodingCactus the input sign, I'll put that in the thing

HahaYes (1221)

@CodingCactus I hope this gets a few more upvotes. People seem to like python better though.

HahaYes (1221)

@CodingCactus I guess you are more popular so you have more respect I guess

Jakman (455)

@HahaYes people prefer python mainly because of all of the things done for them in the language. it is dynamically typed and is loved by beginners. I have no problem with it other than it does not require a plan for building things with it usually.
Once you know of a few functions and how oop works, you can just type psuedo code and it requires not alot of work. When I build something with python i really dont feel a high of accomplishment as i would at any other language.

HahaYes (1221)

@Jakman yeah, python is basically machine english

kinneticslammer (1)

very confusing i am new to all coding this is very complicated @HahaYes

HahaYes (1221)

@kinneticslammer hmmm what do you mean by new. New to this community or new to coding?

CodeLongAndPros (1370)

I can explain int main:

What is int main()?

To answer that question, we need to first look at what C(++) is.
(I use C and C++ to refer to the same thing)

C is compiled to machine code.

So is assembly code. Assembly code is pretty much the lowest level your code can be.

In the ELF, it needs an entry point. This can be anything with the entry foo command.

So, when GCC compiles C to machine code, it uses ENTRY main in the assembly code.

This is why you need to have an int main in your code.

Sources:

HahaYes (1221)

@CodeLongAndPros oh lol thanks for the very detailed explanation. I will make sure to give you credits in the next episode of C++ bootcamp

TheProgrammer3 (6)

The #include <iostream> is what we call a header. C++ allows a bunch of headers, but because I'm lazy, I use #include <bits/stc++.h> because it basically is a header file that has every standard library, including #include <iostream>

From Stack Overflow:

Including <bits/stdc++.h appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year.

I imagine the advantages are vaguely given thus:

You only need write one #include line
You do not need to look up which standard header everything is in
Unfortunately, this is a lazy hack, naming a GCC internal header directly >instead of individual standard headers like <string>, <iostream> and
<vector>. It ruins portability and fosters terrible habits.

The disadvantages include:

It will probably only work on that compiler
You have no idea what it'll do when you use it, because its contents are >not set by a standard
Even just upgrading your compiler to its own next version may break your >program
Every single standard header must be parsed and compiled along with your >source code, which is slow and results in a bulky executable under certain >compilation settings
Don't do it!

TheProgrammer3 (6)

Another answer:

Why? Because it is used as if it was supposed to be a C++ standard header, but no standard mentions it. So your code is non-portable by construction. You won't find any documentation for it on cppreference. So it might as well not exist. It's a figment of someone's imagination :)

I have discovered - to my horror and disbelief - that there is a well-known tutorial site where every C++ example seems to include this header. The world is mad. That's the proof.

To anyone writing such "tutorials"

Please stop using this header. Forget about it. Don't propagate this insanity. If you're unwilling to understand why doing this is Wrong, take my word for it. I'm not OK being treated as a figure of authority on anything at all, and I'm probably full of it half the time, but I'll make an exception in this one case only. I claim that I know what I'm talking about here. Take me on my word. I implore you.

P.S. I can well imagine the abominable "teaching standard" where this wicked idea might have taken place, and the circumstances that led to it. Just because there seemed to be a practical need for it doesn't make it acceptable - not even in retrospect.

P.P.S. No, there was no practical need for it. There aren't that many C++ standard headers, and they are well documented. If you teach, you're doing your students a disservice by adding such "magic". Producing programmers with a magical mindset is the last thing we want. If you need to offer students a subset of C++ to make their life easier, just produce a handout with the short list of headers applicable to the course you teach, and with concise documentation for the library constructs you expect the students to use.

HahaYes (1221)

@TheProgrammer3 lmao sure I"ll stop. I just did it because I'm a competitive programmer. #include <bits/stdc++.h> is a nice thing in competitive programming

BenFord (13)

Thanks a lot! A lot of this stuff I somewhat recall but it's always good to brush up on the basics.

HahaYes (1221)

@BenFord yeah no problem. I'll continue this series once school gets back again

DungeonMaster00 (130)

@HahaYes It would be good if you added links to each tutorial (except the one your'e on) in each tutorial. That way, someone who comes across these tutorials in a month can easily navigate between tutorials.

HahaYes (1221)

@nk1rwc yes, thanks for the tip! I will give you a shoutout next episode.

JustAWalrus (1146)

In retrospect 'using namespace std;' is a bad habit. The faults of using it are immense whilst using he regular way is fine. Just use std::!

HahaYes (1221)

@Wuru I gotta rule the tutorials section, day 5 came out

DevJoshua (0)

thank you for helping me!

Highwayman (1358)

2nd fastest language (if you don't count machine code)

oo wait wait 3rd. Look up FPGAs. ;)

HahaYes (1221)

@Highwayman I'm talking about languages people actually use....

oignons (310)

You explained it better than I did :) But I wouldn't recommend doing using namespace std; globally, because what if you need to pull from another namespace in your program? It could cause an error.

HahaYes (1221)

@ipastrano I'm a competitive programmer, competitive programming is about how many bad habits you can do all at once. :)

TheSummit3145 (22)

haha can i help if you look at my profile most of my stuff is in c++

HahaYes (1221)

@TheSummit3145 hmmmmmm I dunno. But you can give this a upvote to know you support this

HahaYes (1221)

@TheSummit3145 lol thanks, feel free to share this!

HahaYes (1221)

@TheSummit3145 Do you have any suggestions or questions? Be happy to answer them

TheSummit3145 (22)

@hahayes nothing yet, ill get back to you

awesome10 (221)

this is great! super helpful because im doing a challenge to 'learn' 3 new languages in a week, so this is awesome!

HahaYes (1221)

@awesome10 lol thanks for the like

HahaYes (1221)

@awesome10 I will try to post day 2 as soon as possible!

Bookie0 (4023)

Cool but you should make Repl classroom So that you can give us like exercises and stuff

HahaYes (1221)

@Bookie0 How do you make repl classroom?

Ganesha1 (33)

@HahaYes

Select Teacher to be able to create a classroom.

HahaYes (1221)

@Ganesha1 ummmm yes.... wrong post?

Ganesha1 (33)

@HahaYes

You asked... Literally right above my comment...

Ganesha1 (33)

@HahaYes But I replied to the comment...

HahaYes (1221)

@Ganesha1 ummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm-

HahaYes (1221)

@Ganesha1 Like this post for more content! HahaYes-bot