Chapter 1 Notes

Please read chapter 1 of the textbook.

Basic Computer Structure

see diagram on p. 3

see diagram on p. 5

Running a Program

see diagram on p. 10/11

Creating a Program

especially for large programs, it is useful to follow a systematic process when writing programs

see diagram on p. 16

Hello, World in C++

// hello_world.cpp

#include <iostream>
#include "cmpt_error.h"

int main() {
    std::cout << "Hello, world!\n";
}

we call this source code, or just code for short

programming is all about getting a computer to do things

we will do this mainly by reading and writing C++ source code

the // characters indicate the beginning of a source code comment (or just comment) meant for humans to read

<iostream> is a file in the C++ standard library, and is where cout resides

the file cmpt_error.h is special to this course, and contains a function called cmpt::error that we will often use; it must be in the same folder as hello_world.cpp

almost all the programs we write in this course will “include” these two files

all C++ programs must have a main function

when a C++ program runs, the main function is always called first

Hello, World in C++

std::cout is how we will get C++ to print text on the console

you can think of cout as the name of the console where text will be printed

std refers to the standard namespace, which are a group of functions, variables, classes, etc. pre-defined in the C++ standard library

in the statement std::cout << "Hello, world!\n";, the << is like an arrow indicating that the string "Hello, world!\n" should be sent to std::cout

the \n in the string is an escape character called newline that represents a line-break

when used with std::cout, it causes the cursor to go to the next line

even though we write \n using two symbols, \ and n, it represents a single C++ character

Hello, World in C++

notice the ; at the end of the statement

; is a semi-colon, and it is used to mark the end of a statement in C++

also note the matching { and } in main

these kinds of brackets are called curly braces, or just braces

braces are used to make the begin and end of a block of code in C++

Compiling and Running Hello, World

$ g++ hello_world.cpp
$ ./a.out
Hello, world!

$ is the Linux command prompt

g++ is the particular C++ compiler we are using in this course

the command g++ hello_world.cpp compiles hello_world.cpp

compiling is the process of converting source code (which is in text format) to an executable format (which is binary format) that we can run

by default, g++ names the executable file a.out

to run a.out, Linux requires you put ./ in front of the name:

$ ./a.out

Compiling and Running Hello, World

$ g++ -o hello hello_world.cpp
$ ./hello
Hello, world!

the -o option lets you give the executable a friendlier name

Using a makefile

it turns out that there are other options that are useful to use when compiling with g++, e.g.

  • turning on all possible warnings (to help catch source code errors)
  • include debugging info (in case you want to use a debugger)
  • stop compiling after the first error
  • etc.
$ g++ -o hello -std=c++14 -Wall -Wextra -Werror -Wfatal-errors
      -Wno-sign-compare -Wnon-virtual-dtor -g hello_world.cpp
$ ./hello
Hello, world!

Using a makefile

instead of typing all these options each time, we store them in file named makefile and then use the make command to compile the file

$ make hello_world
g++  -std=c++14 -Wall -Wextra -Werror -Wfatal-errors -Wno-sign-compare -Wnon-virtual-dtor -g   hello_world.cpp   -o hello_world
$ ./hello_world
Hello, world!

assuming the course makefile is in the same directory, we just need to type make hello_world and make automatically runs g++ with the correct options

the executable file name will be the same as the source file name (without the .cpp)

Using a makefile

use the makefile!

it might seem like more work at first, but overall it is a great time-saver and all those special options will help you catch and prevent errors

make is an entire language, and it is often used with big multi-file programs; it is a very popular tool

among other things, make is smart enough to avoid re-compiling files that have not changed since their last compilation

$ make hello_world
make: `hello_world' is up to date.

large C++ programs typically consist of 100s, or even 1000s, of files, and so this feature can save a huge amount of compiling time

if you want to force re-compiling, use the -B option:

$ make -B hello_world
g++  -std=c++14 -Wall -Wextra -Werror -Wfatal-errors -Wno-sign-compare -Wnon-virtual-dtor -g   hello_world.cpp   -o hello_world

A Bigger Program

// hello_name.cpp

#include "cmpt_error.h"
#include <iostream>
#include <string>

int main() {
    std::string name;

    std::cout << "What is your name? ";
    std::cin >> name;

    int age = 0;
    std::cout << "How old are you? ";
    std::cin >> age;

    int birth_year = 2017 - age;
    std::cout << "Hi " << name << "!\n"
              << "Since you are " << age << " years old "
              << "I'm guessing you were "
              << "born in " << birth_year << ".\n";
}

to compile and run this program type

$ make hello_name
g++  -std=c++14 -Wall -Wextra -Werror -Wfatal-errors -Wno-sign-compare -Wnon-virtual-dtor -g   hello_name.cpp   -o hello_name
$ ./hello_name

make sure cmpt_error.h and makefile are in the same folder!!

The using Command

we had to write std:: a lot in the program

for the kinds of small programs we are writing in this course, std:: everywhere makes the source code harder to read

so we will usually add this line to the top of our programs

using namespace std;

this tells C++ to “use” all the names defined in the std name space in our program so we don’t need to keep writing std::

// hello_name.cpp

#include "cmpt_error.h"
#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;

    cout << "What is your name? ";
    cin >> name;

    int age = 0;
    cout << "How old are you? ";
    cin >> age;

    int birth_year = 2017 - age;
    cout << "Hi " << name << "!\n"
         << "Since you are " << age << " years old "
         << "I'm guessing you were "
         << "born in " << birth_year << ".\n";
}

Things to Note

C++ is very picky about the details of your source code

one wrong character may cause errors

case matters, e.g. int is not the same as Int

C++ does not usually care about spacing and indentation, but human readers do

thus we will always write our programs in a way that uses indentation and blank lines to make our code’s structure and purpose clearer

Variable Declarations

this programs declares three different variables

string name;
// ...

int age = 0;
// ...

int birth_year = 2017 - age;
// ...

these are variable declaration statements

every C++ variable has a name, and a type (which describes the kind of value in the variable)

we could have declared age like this without the = 0

int age;  // bad: initial value of age could be anything

the variable age has an unknown value!

it could be any int

this sort of uncertainty can be the source of hard-to-detect errors

so it is better to always explicitly initialize variables:

int age = 0;  // good: initial value of age is 0

Variable Declarations

string variables are different

string name; // good: name is initialized to the empty string, ""

by default, when you declare a string variable it is initialized to the empty string

you could also be explicit and declare name like this

string name = ""; // good: name is initialized to the empty string, ""

the = "" is not necessary, but it is good because it makes it a little clearer exactly what the initial value of name is