[go: up one dir, main page]

0% found this document useful (0 votes)
4 views23 pages

5614_L1_slides

The document outlines the MAP55614 C++ Programming course for the M.Sc. in High Performance Computing, detailing the course structure, important concepts, and references. It covers the evolution of C++, differences between C and C++, and fundamental types in C++. The course aims to familiarize students with key programming concepts and the C++ language standards, including C++23.

Uploaded by

yashhpc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

5614_L1_slides

The document outlines the MAP55614 C++ Programming course for the M.Sc. in High Performance Computing, detailing the course structure, important concepts, and references. It covers the evolution of C++, differences between C and C++, and fundamental types in C++. The course aims to familiarize students with key programming concepts and the C++ language standards, including C++23.

Uploaded by

yashhpc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

MAP55614 - C++ Programming

M.Sc. in High Performance Computing


School of Mathematics

Richie Morrin - rmorrin@maths.tcd.ie

Hilary Term 2023


This Lecture

Overview of C++
History
Standards
Types
Initialising variables.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


References for this course

Some good references for this course are:


The C++ Programming Language. 4th Ed. Bjarne Stroustrup. I will be mainly
using this.
cppreference.com (better) or cplusplus.com. isocpp.org.
Programming: Principles and Practice Using C++. Bjarne Stroustrup. A more
introductory treatment of how to program.
A Tour of C++. 2nd Edition. Bjarne Stroustrup. Gives an overview. More useful
for people who have programmed in C++ before, but who want to get an idea of
new features.
Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and
C++14. Scott Meyers. More advanced and not standalone.
If you want to buy one to have as a reference, the first one is probably the most useful.
There are other many online resources and forums such as stackoverflow.com too.
Even youtube has good videos (e.g cppcon videos . . . 2021 HPC Student
https://www.youtube.com/watch?v=8Cs_uI-O51s ).
R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming
C++23 Draft

I have uploaded the latest draft version of the C++23 standard to Blackboard.
2120 pages.
C final draft was 534 pages.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Evolution of C++
1967 Simula BCPL
Created by Bjarne Stroustrup at Bell Labs.
B
Took C’s efficiency and flexibility for systems
1978 K&R C programming and merged with Simula’s
Classic C facilities for program organization to get “C
1980 C with classes
with classes”.
Supports various techniques:
1985 Early C++
C89 Object-oriented Programming
1989 ARM C++ Procedural Programming
1998 C++98 C99
Data abstraction
(C++03) Generic Programming
2011 C++11 (C++0x) C11
(C++14)
2017 C++17 (C18)

2020 C++20 (C++2a)

2023?? C++23

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Evolution of C++

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

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


C vs. C++

Technically, MAP55613 is not an official pre-requisite for this course. However


most of you will have taken that course.
So I will do a brief overview of some differences between C and C++.
For anyone who has not taken the C course, the next few slides may not make
much sense to you!
However, after these slides, I will quickly try to cover the basics from the start where
necessary. If there is anything that does not make sense, make sure to ask me.
Is it an advantage to learn C before C++?
Positive: The syntax will be familiar
Negative: You can get stuck in doing things the C way in C++

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Filename extensions and compilers
You will commonly see both .cc and .cpp file extensions in use. (And sometimes even
others like .cxx or .C).
.cc would be traditionally more common on *nix environments → we will use this
for 5614.
You would see .cpp on Windows environments.
For header files, .h is a common extension, although .hpp can also be seen. However
you can also see header files with no extension. (C++20 brings modules but we will
ignore those for now)
When compiling with GNU compilers we can either run gcc or g++.
gcc will compile .c/.cc files as C and C++ respectively
g++ will compile .c/.cc files as C++ files.
Also, when you run g++ it automatically links to the std C++ libraries. g++ is
equivalent to running: gcc -xc++ -lstdc++ -shared-libgcc .

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


C vs. C++

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

1#i n c l u d e <s t d i o . h>


2 i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
3{
4 i n t c l a s s = 5614;
5 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 ) ;
6 return 0;
7}

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++.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Examples C vs. C++ 2

c vs c++ malloc.c

1#i n c l u d e <s t d i o . h>


2
3 void ∗malloc ( s i z e t s i z e ) ; /∗ N o r m a l l y i n c l u d e d v i a s t d l i b . h ∗/
4
5 i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] )
6{
7 d o u b l e ∗A = m a l l o c (10∗ s i z e o f ( d o u b l e ) ) ;
8 return 0;
9}

Output Similarly when we try to


1$ g c c c v s c++ m a l l o c . c compile this, we see that it
2$ g++ c v s c++ m a l l o c . c
3c v s c++ m a l l o c . c : I n f u n c t i o n ’ i n t main ( i n t , c h a r ∗∗) ’ fails using C++ compiler, this
4c v s c++ m a l l o c . c : 7 : 2 3 : e r r o r : i n v a l i d c o n v e r s i o n from ’ v o i d ∗ ’ t o
5 ’ d o u b l e ∗ ’ [− f p e r m i s s i v e ] time due to implicit casting of
6 d o u b l e ∗A = m a l l o c (10∗ s i z e o f ( d o u b l e ) ) ;
void *.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Introduction to C++!
Minimal C++ function.

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.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Types in C++
C++ has a number of fundamental types:
A Boolean type (bool)
Character types (such as char and wchar t)
Integer types (such as int and long long)
Floating-point types (such as double and long double)
A type, void, used to signify the absence of information
From these types we can construct other types using declarator operators:
Pointer types (such as int *)
Array types (such as char[])
Reference types (such as double& and vector<int>&&)
The user can also define additional types:
Data structures and classes
Enumeration types for representing specific sets of values (enum and enum class)
R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming
Characters

char. Usually 8 bits. Holds implementation’s character set.


signed char. Guaranteed to be signed
unsigned char. Guaranteed to be unsigned
wchar t. Implementation defined
char16 t. Holds 16-bit charater sets.
char32 t. Holds 32-bit charater sets.
For example:
char ch = ‘a’; store the character “a” in the variable ch.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Integers
Integers come in 4 sizes:
short int. Typically 2 bytes
int. Typically 4 bytes
long int. Typically 8 bytes
long long int. Typically 8 bytes
There are three forms of each integer - int, signed int, unsigned int. Note that the
sizes are somewhat implementation dependent.

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

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Floating point integers

There are three floating point types


float: single-precision. e.g. 4 bytes
double: double-precision. e.g. 8 bytes
long double: extended-precision. e.g. 16 bytes
The exact meaning of single-, double-, and extended-precision is implementation
defined.

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.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


auto type deduction.

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.)

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Initializing variables

Since C++11 there are 4 ways to initialise a basic type in C++


initialize.cc

1 int a1 {1}; // Initialises a1 to value 1


2 int a2 = {2}; // Initialises a2 to value 2
3 int a3 = 3 ; // Initialises a3 to value 3
4 int a4 ( 4 ) ; // Initialises a4 to value 4

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!

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Initializing variables
Using {} to prevent narrowing

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.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming


Summary of L1

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.

R. Morrin. 27/01/2023. Lecture 1. MAP55614 - C++ Programming

You might also like