Lecture1 PDF
Lecture1 PDF
Carl Gwilliam
[email protected]
http://hep.ph.liv.ac.uk/~gwilliam/cppcourse
Course Prerequisites
What you should already know about C++
NOTHING!!
I have assumed:
You have never encountered C++ before.
You have limited to no programming experience in any
language.
Be aware!
This is not the comprehensive course for C++!
Some advanced topics will not be covered in this course.
Project specific C++ courses are worth attending if you have
time.
You are learning a sophisticated language! It will take some time
and a fair amount of hands-on experience to become familiar with
C++.
Course format
Lecture 1: Getting Started
Statements, variables, types, operators, I/O, conditional
statements
Lecture 3: Functions
Library and user functions, declarations, arguments, overloading
Course Format
Lecture 6: Classes in Practice
Constructors and destructors, constant functions, memory
management
Hello World!
The first complete C++ program in this course:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Every C++ program must contain one (and only one)
main function.
When the program is executed "Hello World" is
displayed on the terminal screen.
A simple example
int a; float b; float c;
float d = 10.2;
float result;
// get two numbers
cout << "Enter two numbers" << endl;
cin >> a; cin >> b;
c = 2.1;
result = b * a + c;
result = (result + 2)/d;
// display the answer
cout << "Result of calculation: " << result << endl;
Statements
Statements are used to control the sequence of
execution, evaluate an expression (or even do nothing).
ALL statements are terminated with a semi colon ;
Comments
Comments are useful for you and other developers!
// get two numbers
// display the answer
Variables
A variable is a name associated with a location in
computer memory used to store a value.
In order to use a variable in C++, we must first declare it
by specifying which data type we want it to be.
int a;
float d = 10.2;
Variable a has a type integer associated with some
object of type integer.
Variable d has a type float associated with some object
of type float with a value of 10.2
Variable Types
What does it mean to say a variable has a type?
The C++ built-in types are:
int a;
//integer
float b;
//real
double c;
//real
bool d;
//boolean (o or 1)
char e;
Size (bytes)
Range
bool
true/false
char
256 chars
int
2 or 4
(see below)
short int
32,768
long int
2.1*109
float
3.4*1038
double
1.8*10308
1 byte = 8 bits = 28
possible combos.
Size determined
by the compiler.
Range can be
doubled by using
unsigned int
Initialisation
When declaring a variable, its value is by default
undetermined.
We can give the variable a value at the same moment it is
declared
There are two ways to do this in C++.
The more used c-like method:
Assignment
A variable can be assigned (or reassigned) to a particular
value with the assignment operator (=).
c = 2.1;
//assignment operator
result = b * a + c;
Do not mistake the above statements for algebra!
Value of the expression to the right of the assignment
operator (rvalue) is assigned to the variable on the left
(the lvalue). Remember assignment is right-to-left:
Arithmetic Operators
There are five primary mathematical operators:
Addition
Subtraction
Multiplication
Division
Modulus
+
*
/
%
int a = 8; int b = 5;
float c = a / b;
// c is 1
c = (float)a / (float)b; // c is 1.6
Casting an integer
to a float (c-style)
float a = 6 * 4 + 4 * 2;
// answer is 32
float b = 6 * (4+4) * 2;
// answer is 96
// expression is incomprehensible
float c = b+c * 2/d + 3 + b/c + d - b *c/d;
// still unclear!
float d = b + ((c*2)/(d+3)+(b/(c+d))-(b*c/d));
*
/
%
+
-
PRESEDENCE
New line
\n
Horizontal tab
\t
Backspace
\b
Alert
\a
Backslash
\\
This is a string
Value of a: 13.3
a: 13
a=
13.3
Equality example
float a, b, c = 0;
cout << "Enter two numbers << endl;
cin >> a; cin >> b;
c = a - b;
if (a == b) cout << "Two numbers are equal\n";
if (!c) {
cout << "Two numbers are equal\n"; return 0;
}
if (a > b) {
cout << "The first number is greater\n";
} else {
cout << "The second number is greater\n";
}
return 0;
Conditional Statements
if (expression) statement;
Conditional Statements
A statement will be included in the program flow if the
condition is true.
The greater than symbol > and the equivalence symbol
== are relational operators.
if (a > b) statement;
if (a == b) statement;
Remember the difference between the assignment (=)
and equivalence (==) operators!
Condition
is always
true
int x = 4; int y = 3;
if (x = 4) cout << x equals 4\n;
if (y = 4) cout << y equals 4\n;
x equals 4
y equals 4
Relational Operators
Relational operators are used to evaluate if an
expression is true or false.
x<y
Less than
x <= y
x>y
Greater than
x >= y
x == y
equal
x != y
Not equal
Boolean Logic
if (!c)
x equals 2
y equals 0
Compound Statements
Compound statements (or blocks) act as a single
statement.
They enable multiple statements to be attached to one
condition.
Blocks are started and ended with braces { }
A ; is not placed at the end of a block
{
statement 1;
:
statement n;
}
If-else Statements
A true condition in the if expression
will result in block a being executed
otherwise block b will be executed.
if (expression) {
block a;
} else {
block b;
}