5614_L1_slides
5614_L1_slides
Overview of C++
History
Standards
Types
Initialising variables.
I have uploaded the latest draft version of the C++23 standard to Blackboard.
2120 pages.
C final draft was 534 pages.
2023?? C++23
The C++ standard consists of two parts - the core language and the Standard
Library which is a collection of tools and functionality that the user can re-use and
leverage off in their own code.
The first standard published was C++98. A large part of Standard Library,
especially related to containers and algorithms for working with these containers,
was based on the Standard Template Library(STL) which was first introduced
around 1993. There were several different foundation libraries available prior to
C++98 and were not all exactly compatible.
Similarly, since the early 2000’s a lot of work was done on Boost libraries1 . Some
of these libraries and features were more or less incorporated into the official
standard with C++11 (and more with C++17 and C++20).
1
You will probably come across Boost if taking the Financial Applications of HPC course
R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming
Some Important C++ Concepts
Over the next couple of weeks, the concepts below will become familiar.
Interface vs. Implementation
Classes and objects
Abstraction
Encapsulation
Inheritance
Polymorphism
Overloading
Resource Acquisition Is Initialization (RAII).
Generic Programming
C++ is almost a superset of C. However there is some valid C code that is not valid C.
Apart from the first item listed below, most differences stem from C++’s greater
emphasis on type checking.
C++ has additional keywords that are not keywords in C.
No “implicit int” rule (Technically dropped from C11 but not enforced).
void f(); in C is a function declaration for a function with an unspecified
number of parameters. In C++ it is a function declaration for a function with
exactly zero parameters (cf. void f(void); for C)
C allows implicit casting of a void pointer to any other pointer type. This is not
allowed in C++.
(Plus some other, less common differences)
There are some things that are common in C but which are considered bad practice in
C++.
R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming
Examples C vs. C++
c vs c++.c
Output
1$ g c c c v s c ++.c
When we try to compile
2$
3c
g++ c v s c ++.c
v s c ++.c : I n f u n c t i o n ’ i n t main ( i n t , c h a r ∗∗) ’
this, we see that it fails
v s c ++.c : 4 : 5 : e r r o r : e x p e c t e d p r i m a r y −e x p r e s s i o n b e f o r e ’ i n t ’
4c
5 i n t c l a s s = 5614;
using C++ compiler
6
7c
ˆ
v s c ++.c : 5 : 5 2 : e r r o r : e x p e c t e d p r i m a r y −e x p r e s s i o n b e f o r e ’ c l a s s ’
because class is a keyword
8
9
p r i n t f ( ”The c o d e f o r HPC c++ programming i s %d\n” , c l a s s ) ;
ˆ
under C++.
c vs c++ malloc.c
minimal.cc
1#i n c l u d e <i o s t r e a m >
Output
2
3 i n t main ( ) 1 $ g++ m i n i m a l . c c
4{ 2 $ . / a . out
5 s t d : : c o u t << ” H e l l o World\n” ; 3 H e l l o World
6 return 0;
7}
The minimal C++ program is int main(){} . Every C++ program must have
exactly one global function named main(). Curly braces {} express grouping (&
scope) in C++.
#include <iostream> is the preprocessor directive which takes the
contents if the file iostream and “dumps” them at the top of the file. This file
contains information so that the compiler can make sense of the cout line.
std::cout << "Hello World\n"; prints the phrase “Hello World” to the
screen. The \n prints a newline.
R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming
Types and declarations
Every name (identifier) in a C++ program has a type associated with it. The type
determines what operations can be applied to the name and how such operations are
interpreted.
1 float x; // x i s a f l o a t i n g −p o i n t v a r i a b l e
2 int y = 7; // y i s an i n t e g e r v a r i a b l e w i t h t h e initial value 7
3
4 float f ( int ); // f i s a f u n c t i o n t a k i n g an argument o f t y p e i n t
5 // and r e t u r n i n g a f l o a t i n g −p o i n t number
Once we know the types, then the compiler knows how to handle expressions such
as x = y + f(2);
Similar to C all identifiers used in C++ must be declared before use.
C++ is more strongly typed, with better type safety, than C.
We also have integer literals which can be expressed in decimal, octal (starting with 0)
or hexadecimal (starting with 0x) form.
E.g 63 (dec) = 077 (oct) = 0x3f (hex).
Suffixes can be added to explicitly specify the type.
100U is an unsigned int. 100L is a long int. 100LLU is a long long unsigned
We can also have floating point literals. By default, these will have type double. To
specify a string literal of different type you can explicitly add the suffix “f” or “F” for
float, or “l” or “L” for long double. E.g. 1.23F or 1.23l etc.
Output
auto.cc
1 $ g++ a u t o . c c −−s t d=c++11
1#i n c l u d e <i o s t r e a m > // Needed f o r c o u t 2 $ . / a . out
2#i n c l u d e <t y p e i n f o > // Needed f o r t y p e i d ( ) 3 v1 i s t y p e i
3 i n t main ( ) { 4 v2 i s t y p e d
4 a u t o v1 = 1 ; // v1 i s an i n t e g e r 5 v3 i s t y p e f
5 a u t o v2 = 2 . 0 ; // v2 i s a d o u b l e 6 v4 i s t y p e l
6 a u t o v3 = 3 . 0 f ; // v3 i s a f l o a t 7 v5 i s t y p e e
7 a u t o v4 = 1L ; // v4 i s a l o n g 8
8 a u t o v5 = 1 . 0 L ; // v5 i s a l o n g d o u b l e 9 Iteration 0
9 10 I t e r a t i o n 1
10 s t d : : c o u t << ” v1 i s t y p e ” << t y p e i d ( v1 ) . name ( ) << ”\n” 11 I t e r a t i o n 2
11 << ” v2 i s t y p e ” << t y p e i d ( v2 ) . name ( ) << ”\n” 12 I t e r a t i o n 3
12 << ” v3 i s t y p e ” << t y p e i d ( v3 ) . name ( ) << ”\n” 13 I t e r a t i o n 4
13 << ” v4 i s t y p e ” << t y p e i d ( v4 ) . name ( ) << ”\n”
14 << ” v5 i s t y p e ” << t y p e i d ( v5 ) . name ( ) << ”\n” ;
15
16 // a u t o commonly u s e d i n l o o p s C++11 introduced (actually
17 f o r ( a u t o i = 0 ; i < 5 ; ++i ) {
18 s t d : : c o u t << ” I t e r a t i o n ” << i << s t d : : e n d l ; reused/redefined2 ) the auto
19 }
20 return 0; keyword. It can be used if
21 }
initialising a variable when
declaring it.
2
auto was a (redundant) keyword in C which explicitly declared a variable as an automatic variable,
i.e. created and stored onR.the stack.
Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming
auto type deduction.
General advice (for now) is to use auto whenever it is clear what type the variable is.
Although in assignments etc. I might tell you to use explicit types just so you can get
used to them.
Note that there is another type of type deduction using the decltype specifier. You use
auto when initialising a variable.
(We have template type deduction which has some slightly different rules too. More on
this later.)
The first form is usually recommended and is the most general. Although you will not
see this in older code. It is called uniform intialisation (or “braced initialisation”). The
third form is the “traditional” form which you will see most often.
Uniform initialisation does not allow narrowing. In other words, it does not allow
conversion from one type to another if the new variable cannot fully store the original
type. So, trying to initialise a long int with an int is fine, but not the reverse!
narrowing.cc
1 i n t A {1};
2 l o n g B{2}; Using the initialiser list could
3
4 l o n g C {A} ; // f i n e potentially help you catch unintended
5 i n t D {B} ; // Not a l l o w e d
6 narrowing i.e. a hard to detect bug!
7 i n t E = B; // R e s u l t s i n n a r r o w i n g .
Note that, pre C++17, when using auto type deduction, if you do auto A {1};,
the compiler deduces that A is of type initialiser-list rather than int!. This is
“fixed” now..
Some authors will advise that when using auto you can use the old C style ‘‘=’’
- auto var = 1.23; - because there can not be any narrowing anyway.
We looked at:
History of C++. Standard library.
Some differences between C and C++.
Fundamental types in C++
auto type deduction.
Different ways to initialise variables.