C++ - Introduction

Nov. 20, 2020 pexels-pixabay-35888.jpg Vuong Huynh

C++ Certificate

Credential

Introduction to C++

C++ is a programming language created by Bjarne Stroustrup and his team at Bell Laboratories in 1979. 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.


First program
#include <iostream>

int main() {
  
  std::cout << "Hello, World!\n";
  return 0;

}

Output
:

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

#include <iostream> → include libraries

int main() → the main() function

{ → beginning the function

anything inside the curly braces → the content of the function or whatever the program does inside the curly braces

return 0; The return statement is used to end a function. Returning 0 indicates the code executed successfully.

} → end of the function

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 the text. The \n is a special character that indicates a new line.
  • ; is punctuation that tells the computer that you are at the end of a statement. It is similar to a period in a sentence.


Compile and Execute

Code → Save → Compile → Execute

C++ is a compiled language. It needs a compiler to execute.

Install GCC compiler for C++ for Windows (or other OS)

4 phases during development:

  1. Code — writing the program
  2. Save — saving the program
  3. Compile — compiling via the terminal
  4. Execute — executing via the terminal

Compile: To compile a file, you need to type g++ followed by the file name in the terminal. Of course, we have to have our machine installed c++ compiler.

g++ hello.cpp


Execute: To execute the new machine code file, all you need to do is type ./

./a.out

We can name the output file:

g++ hello.cpp -o hello

And execute from that name:

./hello


Comments

There are two types of code comments in C++:

  • A single line comment will comment out a single line and is denoted with two forward slashes // preceding it:
// Prints "hi!" to the terminal
std::cout << "hi!";

You can also use a single line comment after a line of code:

std::cout << "hi!";  // Prints "hi!"
  • A multi-line comment will comment out multiple lines and is denoted with /* to begin the comment, and */ to end the comment:
/* This is all commented.
std::cout << "hi!";
None of this is going to run! */


Project: Block Letters

Write a C++ program called initials.cpp that displays the initials of your name in block letters

// My initial name is V H
#include <iostream>
 
int main() {
 
  std::cout << "V           V    H           H\n";
  std::cout << " V         V     H           H\n";
  std::cout << "  V       V      H           H\n";
  std::cout << "   V     V       H H H H H H H\n";
  std::cout << "    V   V        H           H\n";
  std::cout << "     V V         H           H\n";
  std::cout << "      V          H           H\n";
 
}
g++ initials.cpp -o initials
./initials