Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Introduction to C++

C++ is a programming language created by Bjarne Stroustrup and his team


at Bell Laboratories in 1979. Forty years later, it is one of the most widely
used languages in the world; we can find C++ applications everywhere,
from the bottom of the oceans to the surface of Mars.

As the name implies, C++ was derived from the C language; Bjarne’s goal


was to add object-oriented programming into C, a language well-respected
for its portability and low-level functionality.

So why learn C++? Among many other things:

 It is fast and flexible.


 It is well-supported.
 It forces you to think in new and creative ways.

In this lesson, we’ll start learning some basic concepts, and you’ll write your
very first C++ program.

Note: Don’t worry if some words don’t make much sense right now. We’ll
learn about them in a bit!

Hello World!
Take a look at the hello.cpp file in the code editor that is placed in the
middle of the screen. It’s a C++ program!

In our code editor, the file name is displayed at the top:

C++ programs are stored in files which usually have the file extension .cpp,
which simply stands for “C Plus Plus”.

The code inside our C++ file is a classic first step all new programmers take
— they greet the world through the terminal!

The terminal is the black panel on the right. It should be blank right now.
The code in the text editor will print text out onto the terminal. More
specifically, it will print the phrase Hello World!.
Before we explain what all that mumbo jumbo is, let’s run the program to
see what happens.

Instructions

1.
Press Run to see this program in action.

What message appeared in the terminal?

Output
High five! We just got your first program to run.

C++, like most programming languages, runs line by line, from top to
bottom. Here is the structure of a C++ program:

In between the curly braces is what we are going to focus on for now.

std::cout << "Hello World!\n";

 std::cout is
the “character output stream”. It is pronounced “see-out”.
 << is an operator that comes right after it.
 "Hello World!\n" is what’s being outputted here. You need double quotes
around text. The \n is a special character that indicates a new line.
 ; isa punctuation that tells the computer that you are at the end of a
statement. It is similar to a period in a sentence.

Instructions

1.
Let’s write the whole std::cout statement from scratch.

Inside the curly braces, type the following and press Run:

std::cout << "Codecademy\n";


What do you think this program will output?
Checkpoint 2 Passed

Hint
Don’t forget the semicolon ; at the end!

The \n (backward slash and the letter n) is a special character that indicates


a new line.

Your program should look like:

#include <iostream>

int main() {

  std::cout << "Codecademy\n";

}
The terminal should look like this after you click Run:

Pattern
We learned how to output a line of text with the following code:

std::cout << "🚙💨\n";


It will output:

🚙💨
We can also output multiple lines by adding more std::cout statements:

std::cout << "Hello\n";


std::cout << "Goodbye\n";
This will output:
Hello
Goodbye
Now let’s use what we learned so far to complete a coding challenge!

Instructions

1.
Instead of displaying those two lines in the output, edit the code so that we
output the following pattern in the terminal:

       1
     2 3
   4 5 6
7 8 9 10
Exactly how it is.

Remember you can use the hint if you need help.


Hint
This checkpoint requires some trial and error.

Things to remember:

 We can use multiple std::cout statements to output multiple lines.


 Code runs line by line (from top to bottom).
 Note the spacing between the numbers.

Answer:

#include <iostream>

int main() {

  std::cout << "       1\n";


  std::cout << "     2 3\n";
  std::cout << "   4 5 6\n";
  std::cout << "7 8 9 10\n";

Review
Woohoo! You have written a few C++ programs. 🙌

In this lesson, you have learned:


 C++ is a general-purpose coding language.
 C++ runs line by line, from top to bottom.
 std::cout is how you output to the terminal:

std::cout << "Good luck!\n";

Instructions

1.
Before we move on, let’s write a letter to your future self.

In letter.cpp, let’s add the following:

 Goal(s) for yourself.


 Name and date.

Press Run to mail the letter! 📬

P.S. This letter will be returned when you complete the course.
Checkpoint 2 Passed

Hint
Programming may seem tough and intimidating, but like everything, all you
need is a little patience and resilience.

Here, you are writing a letter to your future programming self.

There is no right answer!

Example:

#include <iostream>

int main() {

  std::cout << "Dear Self,\n";


  std::cout << "Build a VR game.\n";
  std::cout << "1/1/2020, New York\n";

}
And here’s a letter to you.

Dear Learner,
I embarked on my programming journey in 2009 when I enrolled in a C/C++
course in college, not knowing what it meant nor where it could possibly take
me.

In the last ten years, programming has brought tremendous joy to my life
and I met so many wonderful people through it. I am incredibly jealous of
where you are right now because I would love to experience this all over
again.

Thank you for learning C++ with us. I can’t wait to see what you create with
it.

Sonny @ Codecademy

You might also like