Skip to content
← Back to Community
C++ Tutorial: Day 1
Profile icon
HahaYes

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

Voters
Profile icon
hermes77
Profile icon
PrajwolBasyal
Profile icon
AVAJAMES
Profile icon
eskioglou
Profile icon
JacobRoper
Profile icon
RamnSnchez
Profile icon
ConradBallman
Profile icon
WILLIAMBAEWER
Profile icon
multicoder2021
Profile icon
ia13ru
Comments
hotnewtop
Profile icon
HahaYes
Profile icon
CodeLongAndPros

@HahaYes Was the mention required? I was pinged by the post.

Profile icon
HahaYes

@CodeLongAndPros kinda but not really

Profile icon
CodeLongAndPros

@HahaYes Also, is there going to be a website?

Profile icon
HahaYes

@CodeLongAndPros yeah, I sent you a link

Profile icon
CodingCactus

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

Profile icon
HahaYes

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

Profile icon
HahaYes

@CodingCactus sorry I meant output

Profile icon
CodingCactus
Profile icon
HahaYes

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

Profile icon
CodingCactus

@HahaYes lol it's easier i guess?

Profile icon
HahaYes

@CodingCactus well duh

Profile icon
HahaYes

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

Profile icon
Jakman

@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.

Profile icon
HahaYes

@Jakman yeah, python is basically machine english

Profile icon
kinneticslammer

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

Profile icon
HahaYes

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

Profile icon
kinneticslammer

@HahaYes coding

Profile icon
CodeLongAndPros

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:

Profile icon
HahaYes

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

Profile icon
programmeruser

The #include 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

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 , and
. 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!

Profile icon
programmeruser

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.

Profile icon
HahaYes

@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

Profile icon
BenFord

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

Profile icon
HahaYes

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

Profile icon
DungeonMaster00

@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.

Profile icon
HahaYes

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

Profile icon
JustAWalrus

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::!

Profile icon
HahaYes

@Wuru oh ok thanks

Profile icon
HahaYes

@Wuru feel free to upvote

Profile icon
JustAWalrus

@HahaYes Will do.

Profile icon
HahaYes

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

Profile icon
DevJoshua

thank you for helping me!

Profile icon
HahaYes

@DevJoshua lol you are welcome

Profile icon
Highwayman

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

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

Profile icon
HahaYes

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

Profile icon
TheSummit3145

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

Profile icon
HahaYes

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

Profile icon
TheSummit3145

@HahaYes upvoted xD

Profile icon
HahaYes

@TheSummit3145 lol thanks, feel free to share this!

Profile icon
HahaYes

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

Profile icon
TheSummit3145

@hahayes nothing yet, ill get back to you

Profile icon
HahaYes
Profile icon
awesome10

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

Profile icon
HahaYes

@awesome10 lol thanks for the like

Profile icon
HahaYes

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

Profile icon
awesome10
Profile icon
awesome10

@HahaYes awesome thx!

Profile icon
Bookie0

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

Profile icon
HahaYes

@Bookie0 How do you make repl classroom?

Profile icon
Ganesha1

@HahaYes
image

Select Teacher to be able to create a classroom.

Profile icon
HahaYes

@Ganesha1 ummmm yes.... wrong post?

Profile icon
Ganesha1

@HahaYes
image

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

Profile icon
HahaYes

@Ganesha1 I made a post......

Profile icon
Ganesha1

@HahaYes But I replied to the comment...

Profile icon
HahaYes

@Ganesha1 ummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm-

Profile icon
HahaYes

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