feat/cleaned up formatting, added clarity, removed unnecessary parts (#35519)
* feat/cleaned up formatting, added clarity, removed unnecessary parts * feat: removed chart image
This commit is contained in:
committed by
Tom
parent
d636f21aa3
commit
a3c4d5f3c1
@ -1,102 +1,78 @@
|
||||
---
|
||||
title: C++
|
||||
---
|
||||
# Hello World! - Your First C++ Program
|
||||
# C++
|
||||
|
||||
## What is C++?
|
||||
|
||||
* C++ is a general purpose programming [Object-Oriented](https://en.wikipedia.org/wiki/Object-oriented_programming) language which has been used since the 1990's
|
||||
* It was designed as a thesis paper by [Bjarne Stroustrup](https://en.wikipedia.org/wiki/Bjarne_Stroustrup) under the name "C with Classes", which later became C++.
|
||||
* It is a version of C that includes Object-Oriented elements, including classes and functions.
|
||||
- C++ is a general purpose programming [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) (OOP) language which has been used since the 1990's
|
||||
- It was designed as a thesis paper by [Bjarne Stroustrup](https://en.wikipedia.org/wiki/Bjarne_Stroustrup) under the name "C with classes", which later became C++.
|
||||
- It is a version of C that includes OOP elements, including classes and functions.
|
||||
|
||||
* It is considered one of the biggest programming languages, as you can see in the following image:
|
||||

|
||||
_source: Github_
|
||||
|
||||
### Your First Program in C++
|
||||
## A Simple C++ Program
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
cout << "Hello World!" << endl;
|
||||
cout << "Hello, World!" << endl;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
#### The Output of this program will be:
|
||||
**Output:**
|
||||
|
||||
```
|
||||
Hello World!
|
||||
Hello, World!
|
||||
```
|
||||
|
||||
Now, let's break down the code:
|
||||
|
||||
|
||||
#### Lines 1 and 2
|
||||
### Code Breakdown
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
```
|
||||
|
||||
- The first line tells the compiler to use the `iostream` header file for this specific program. A header file is a separate file with pre-written C++ code. There are many other header files which are required for a specific program to run properly. Some examples are the `math`, `vector` and `string` header files. Header files are generally represented by a `.h` extension, but when including standard library header files you do not need to include the `.h` extension.
|
||||
- The `iostream` header contains the public interface for the input-output stream from the standard library. The `iostream` file contains code for allowing the computer to take input and generate an output, using the C++ language.
|
||||
|
||||
```cpp
|
||||
using namespace std;
|
||||
```
|
||||
|
||||
* The first line tells the computer to use the `iostream` header file for this specific program. A header file is a separate file with pre-written C++ code. There are many other header files which are required for a specific program to run properly. For example, `math`, `vector`, `string`, etc. Header files are generally represented by a `.h` extension, when including standard library header files you don't include the `.h` extension.
|
||||
* The `iostream` header contains the public interface for the input-output stream from the standard library. The `iostream` file contains code for allowing the computer to take input and generate an output, using the C++ language.
|
||||
* The second line tells the computer to use the standard namespace, which includes features of standard C++. You could write this program without this line, but you'd have to use `std::cout` instead of `cout` and `std::endl` instead of `endl` on line 4. It makes the code more readable and our lives as programmers easier.
|
||||
* While not including `std::` seems easier, it is not good practice to omit the namsespace. The benefit of putting the namespace in front is to ensure that functions with the same names but from different libraries can still run normally.
|
||||
|
||||
#### Line 3 and 4
|
||||
- This line tells the compiler to use the standard namespace, which includes features of standard C++. You could write this program without this line, but you'd have to use `std::cout` instead of `cout` and `std::endl` instead of `endl` on line four.
|
||||
- Adding the namespace makes the code more readable, but it is not necessarily good practice to omit the namespace as putting the namespace in front helps ensure that functions with the same name but from different libraries can still together without conflict.
|
||||
|
||||
```cpp
|
||||
int main()
|
||||
{
|
||||
/*
|
||||
Code Body
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
* C++ starts execution of a program from the -main function- `int main()`. During execution, the computer starts running the code from every line from `{`(opening bracket) till `}`(closing bracket)
|
||||
**NOTE: Every function starts with an opening curly brace "{" and ends with a closing curly brace "}".**
|
||||
* Line 4 indicates the start of the main() function.
|
||||
|
||||
#### Lines 5, 6 & 7
|
||||
- A C++ program starts execution at the `main` function `int main()`. During execution, the computer will run each line of code from the `{` opening curly brace until the `}` closing curly brace.
|
||||
- **NOTE**: Every function starts with an opening curly brace ( `{` ) and ends with a closing curly brace ( `}` ).
|
||||
|
||||
```cpp
|
||||
cout << "Hello World" << endl;
|
||||
{
|
||||
cout << "Hello, World!" << endl;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
* The word `cout` in C++ is used to stream data to standard output.
|
||||
* It is followed by `<<` , the _insertion operator_.
|
||||
* Whatever is in the double quotes `""` is printed. Certain special characters have a different syntax for print statements
|
||||
* Now to print any other kind of data, you have to add `<<`.
|
||||
- `cout` (console output) in C++ is used to stream data to standard output.
|
||||
- It is followed by `<<` (the _insertion operator_) which is used to stream the following text in quotes `"Hello, World!"` to `cout`
|
||||
- Certain special characters may require escaping by preceding the character with a backslash character ( `\` ).
|
||||
- `endl` (end line) is then streamed to `cout` to insert a newline character, which tells `cout` to move to the next line in the output.
|
||||
- `return 0;` safely terminates the current function (e.g., `main()`). A `0` return value tells the execution environment that the program exited without error.
|
||||
|
||||
***Challenge: Change Hello World to another sentence. What will be the output?***
|
||||
Programmers use a _Hello World_ program like this one as a common learning tool when using a new programming language. Successful execution indicates a basic understanding of the syntax and allows the programmer to move on to more complicated features of the language.
|
||||
|
||||
* `endl` is another symbol from the iostream library which means to **end this line and go to the next line during output**. - _cout stands for "console output"_
|
||||
* Finally, finish the statement with a semicolon `;`.
|
||||
## Additional Resources
|
||||
|
||||
**NOTE : Every statement except the main function definition and the #include directive needs to be ended by the semicolon. Without a ";", you may encounter a compiler error.**
|
||||
|
||||
* `return 0;` safely terminates the current function i.e. `main()` in this case and since no function follows after 'main()' the program is terminated.
|
||||
* Don't forget to tell the computer that this is end of the `main()` function. To do this, you add the closing curly brace `}`. You will encounter compiler error before program execution if you do not include the `}`.
|
||||
|
||||
### The code should look something like this:
|
||||
|
||||

|
||||
|
||||
Programmers use a Hello World program (like this one) as a ritual on using a new programming language. It is a symbol of good luck.
|
||||
_You have finished coding your first C++ program and have understood most of the code you have written/typed. CONGRATULATIONS!_
|
||||
|
||||
**Good Luck to all of you and happy coding! :)**
|
||||
|
||||
**Happy Coding ! :)**
|
||||
|
||||
**Feel free to ask any questions on FreeCodeCamp's GitHub page or [FreeCodeCamp's Forum.](https://forum.freecodecamp.org/)**
|
||||
|
||||
[Try it yourself ! :)](https://repl.it/L4k3)
|
||||
|
||||
**You need software to write and execute C++ code, download from one of the links below:**
|
||||
|
||||
[Codeblocks](http://www.codeblocks.org/downloads/26)
|
||||
[Xcode](https://developer.apple.com/xcode/)
|
||||
[Eclipse](http://www.eclipse.org/downloads/)
|
||||
### IDE Software that Support C++
|
||||
- [Codeblocks](http://www.codeblocks.org/downloads/26)
|
||||
- [Xcode](https://developer.apple.com/xcode/)
|
||||
- [Eclipse](http://www.eclipse.org/downloads/)
|
||||
|
Reference in New Issue
Block a user