C++ Tutorial: Day 1
h
HahaYes (1869)

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

You are viewing a single comment. View All
programmeruser (613)

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!

programmeruser (613)

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 (1869)

@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