





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






gy
At its core, a computer is just a processor with some memory, capable of running tiny instructions like āstore 5 in memory location 23459.ā Why would we express a program as a text file in a programming language, instead of writing processor instructions?
The advantages:
C++ is a high-level language: when you write a program in it, the shorthands are sufficiently expressive that you donāt need to worry about the details of processor instructions. C++ does give access to some lower-level functionality than other languages (e.g. memory addresses).
A program goes from text files (or source files) to processor instructions as follows:
Source File Compiler Object File
Source File Compiler Object File
Linker Executable Libraries
O S Program in Memory
Object files are intermediate files that represent an incomplete copy of the program: each source file only expresses a piece of the program, so when it is compiled into an object file, the object file has some markers indicating which missing pieces it depends on. The linker
takes those object files and the compiled libraries of predefined code that they rely on, fills in all the gaps, and spits out the final program, which can then be run by the operating system (OS).
The compiler and linker are just regular programs. The step in the compilation process in which the compiler reads the file is called parsing.
In C++, all these steps are performed ahead of time, before you start running a program. In some languages, they are done during the execution process, which takes time. This is one of the reasons C++ code runs far faster than code in many more recent languages. C++ actually adds an extra step to the compilation process: the code is run through a preprocessor, which applies some modifications to the source code, before being fed to the compiler. Thus, the modified diagram is:
Source File Preprocessor^ Processed Code Compiler Object File
Source File Preprocessor^ Processed Code Compiler Object File
Linker Executable Libraries
O S (^) Program in Memory
C++ is immensely popular, particularly for applications that require speed and/or access to some low-level features. It was created in 1979 by Bjarne Stroustrup, at first as a set of extensions to the C programming language. C++ extends C; our first few lectures will basically be on the C parts of the language. Though you can write graphical programs in C++, it is much hairier and less portable than text-based (console) programs. We will be sticking to console programs in this course. Everything in C++ is case sensitive: someName is not the same as SomeName.
In the tradition of programmers everywhere, weāll use a āHello, world!ā program as an entry point into the basic features of C++.
// A Hello World program
This line tells the compiler that it should look in the std namespace for any identifier we havenāt defined. If we do this, we can omit the std:: prefix when writing cout. This is the recommended practice.
Escape Sequence Represented Character \a System bell (beep sound) \b Backspace \f Formfeed (page break) \n Newline (line break) \r āCarriage returnā (returns cursor to start of line) \t Tab \ Backslash \ā Single quote character " Double quote character \some integer x The character represented by x
Note that every statement ends with a semicolon (except preprocessor commands and blocks using {}). Forgetting these semicolons is a common mistake among new C++ programmers.
So far our program doesnāt do very much. Letās tweak it in various ways to demonstrate some more interesting constructs.
First, a few definitions:
Not every statement is an expression. It makes no sense to talk about the value of an #include statement, for instance.
We can perform arithmetic calculations with operators. Operators act on expressions to form a new expression. For example, we could replace "Hello, world!\n" with (4 + 2) / 3 , which would cause the program to print the number 2. In this case, the + operator acts on the expressions 4 and 2 (its operands).
Operator types:
Every expression has a type ā a formal description of what kind of data its value is. For instance, 0 is an integer, 3.142 is a floating-point (decimal) number, and "Hello, world!\n"
We might want to give a value a name so we can refer to it later. We do this using variables. A variable is a named location in memory.
For example, say we wanted to use the value 4 + 2 multiple times. We might call it x and use it as follows: 1 # include < iostream > 2 using namespace std ; 3 4 int main () { 5 int x ; 6 x = 4 + 2; 7 cout << x / 3 << ā ā << x * 2; 8 9 return 0; 10 }
(Note how we can print a sequence of values by āchainingā the << symbol.) The name of a variable is an identifier token. Identifiers may contain numbers, letters, and underscores ( ), and may not start with a number. Line 4 is the declaration of the variable x. We must tell the compiler what type x will be so that it knows how much memory to reserve for it and what kinds of operations may be performed on it.
Line 5 is the initialization of x, where we specify an initial value for it. This introduces a new operator: =, the assignment operator. We can also change the value of x later on in the code using this operator. We could replace lines 4 and 5 with a single statement that does both declaration and initialization: int x = 4 + 2;
This form of declaration/initialization is cleaner, so it is to be preferred.
Now that we know how to give names to values, we can have the user of the program input values. This is demonstrated in line 6 below:
1 # include < iostream > 2 using namespace std ; 3 4 int main () { 5 int x ; 6 cin >> x ; 7 8 cout << x / 3 << ā ā << x * 2; 9 10 return 0; 11 }
Just as cout << is the syntax for outputting values, cin >> (line 6) is the syntax for inputting values.
Memory trick: if you have trouble remembering which way the angle brackets go for cout and cin, think of them as arrows pointing in the direction of data flow. cin represents the terminal, with data flowing from it to your variables; cout likewise represents the terminal, and your data flows to it.
There are two kinds of errors youāll run into when writing C++ programs: compilation errors and runtime errors. Compilation errors are problems raised by the compiler, generally resulting from violations of the syntax rules or misuse of types. These are often caused by typos and the like. Runtime errors are problems that you only spot when you run the program: you did specify a legal program, but it doesnāt do what you wanted it to. These are usually more tricky to catch, since the compiler wonāt tell you about them.