0% found this document useful (0 votes)
35 views28 pages

02 Preparation

Uploaded by

yatoabesta
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)
35 views28 pages

02 Preparation

Uploaded by

yatoabesta
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/ 28

Modern C++

Programming
2. Preparation

Federico Busato
2024-04-10
Table of Contents

1 Books and References


2 Slide Legend

3 What Editor/ IDE/Compiler Should I Use?

4 How to compile?
5 Hello World
I/O Stream

1/22
Books and
References
Suggested Books

Programming and Principles Professional C++ (5th) Absolute C++ (6th)


using C++ (2nd) S. J. Kleper, N. A. Solter, 2021 W. Savitch, 2015
B. Stroustrup, 2014

2/22
More Advanced Books

Effective Modern C++ Embracing Modern C++ Beautiful C++: 30 Core


S. Meyer, 2014 Safely Guidelines for Writing Clean,
J. Lakos, V. Romeo, R. Safe, and Fast Code
Khlebnikov, A. Meredith, 2021 J. G. Davidson, K. Gregory, 2021
3/22
References 1/3

(Un)official C++ reference:*


• en.cppreference.com
• C++ Standard Draft

Tutorials:
• Learn C++
• Tutorials Point C++
• en.wikibooks.org/wiki/C++
• yet another insignificant...programming notes

Other resources:
• stackoverflow.com/questions/tagged/c++

4/22
* The full C++ standard draft can be found at eel.is/c++draft/full (32 MB!)
References 3/3

News:
• isocpp.org (Standard C++ Foundation)
• Reddit C++
• cpp.libhunt.com/newsletter/archive
• MeetingCpp Blogroll

Main conferences:
• cppcon.org (slides), (search engine)
• meetingcpp.com (slides)
• isocpp.com conference list

Coding exercises:
• HackerRank C++
• leetcode.com/problemset/algorithms
• open.kattis.com 5/22
Slide Legend
Slide Legend 1/2


Advanced Concepts. In general, they are not fundamental. They can be
related to very specific aspects of the language or provide a deeper
exploration of C++ features.
A beginner reader should skip these sections/slides

⇝ See next. C++ concepts are closely linked, and it is almost impossible to
find a way to explain them without referring to future topics. These slides
should be revisited after reading the suggested topic

™ Homework. The slide contains questions/exercises for the reader

6/22
Slide Legend 2/2

this is a code section


This is a language keyword/token and not a program symbol (variable, functions,
etc.). Future references to the token could use a standard code section for better
readability

7/22
What Editor/
IDE/Compiler
Should I Use?
What Compiler Should I Use?

Most popular compilers:


• Microsoft Visual Code (MSVC) is the compiler offered by Microsoft
• The GNU Compiler Collection (GCC) contains the most popular C++ Linux
compiler
• Clang is a C++ compiler based on LLVM Infrastructure available for
Linux/Windows/Apple (default) platforms

Suggested compiler on Linux for beginner: Clang


• Comparable performance with GCC/MSVC and low memory usage
• Expressive diagnostics (examples and propose corrections)
• Strict C++ compliance. GCC/MSVC compatibility (inverse direction is not ensured)
• Includes very useful tools: memory sanitizer, static code analyzer, automatic formatting,
8/22
linter, etc.
Install the Compiler on Linux

Install the last gcc/g++ (v11) (v12 on Ubuntu 22.04)


$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt update
$ sudo apt install gcc-12 g++-12
$ gcc-12 --version

Install the last clang/clang++ (v17)


$ bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
$ wget https://apt.llvm.org/llvm.sh
$ chmod +x llvm.sh
$ sudo ./llvm.sh 17
$ clang++ --version

9/22
Install the Compiler on Windows

Microsoft Visual Studio


• Direct Installer: Visual Studio Community 2022

Clang on Windows
Two ways:
• Windows Subsystem for Linux (WSL)
• Run → optionalfeatures
• Select Windows Subsystem for Linux , Hyper-V ,
Virtual Machine Platform
• Run → ms-windows-store: → Search and install Ubuntu 22.04 LTS
• Clang + MSVC Build Tools
• Download Build Tools per Visual Studio
• Install Desktop development with C++ 10/22
What Editor/IDE/Compiler Should I Use? 1/3

Popular C++ IDE (Integrated Development Environment):


• Microsoft Visual Studio (MSVC) (link). Most popular IDE for Windows
• Clion (link). (free for student). Powerful IDE with a lot of options
• QT-Creator (link). Fast (written in C++), simple
• XCode. Default on Mac OS
• Cevelop (Eclipse) (link)

Standalone GUI-based coding editors:


• Microsoft Visual Studio Code (VSCode) (link)
• Sublime (link)
• Lapce (link)
• Zed (link)
11/22
What Editor/IDE/Compiler Should I Use? 2/3

Standalone text-based coding editors (powerful, but needs expertise):

• Vim
• Emacs
• NeoVim (link)
• Helix (link)

Not suggested: Notepad, Gedit, and other similar editors (lack of support for
programming)

12/22
What Editor/IDE/Compiler Should I Use? 3/3

StackOverflow Developer Survey 2022 13/22


How to compile?
How to Compile?

Compile C++11, C++14, C++17, C++20, C++23, C++26 programs:


g++ -std=c++11 <program.cpp> -o program
g++ -std=c++14 <program.cpp> -o program
g++ -std=c++17 <program.cpp> -o program
g++ -std=c++20 <program.cpp> -o program
g++ -std=c++23 <program.cpp> -o program
g++ -std=c++26 <program.cpp> -o program

Any C++ standard is backward compatible*


C++ is also backward compatible with C in most case, except if it contains C++
keywords (new, template, class, typename, etc.)
We can potentially compile a pure C program in C++26

14/22
*except for very minor deprecated features
C++ Standard 1/2

C++11 C++14 C++17 C++20


Compiler
Core Library Core Library Core Library Core Library

g++ 4.8.1 5.1 5.1 5.1 7.1 9.0 11+ 11+


clang++ 3.3 3.3 3.4 3.5 5.0 11.0 16+ 16+
MSVC 19.0 19.0 19.10 19.0 19.15 19.15 19.29+ 19.29

15/22
en.cppreference.com/w/cpp/compiler support
C++ Standard 2/2

16/22
Hello World
Hello World 1/2

C code with printf : C++ code with streams :

#include <stdio.h> #include <iostream>

int main() { int main() {


printf("Hello World!\n"); std::cout << "Hello World!\n";
} }

printf cout
prints on standard output represents the standard output stream

17/22
Hello World 2/2

The previous example can be written with the global std namespace:
#include <iostream>

using namespace std;


int main() {
cout << "Hello World!\n";
}

Note: For sake of space and for improving the readability, we intentionally omit the
std namespace in most slides

18/22
I/O Stream (std::cout) 1/3

std::cout is an example of output stream. Data is redirected to a destination, in


this case the destination is the standard output

C:
#include <stdio.h>
int main() {
int a = 4;
double b = 3.0;
char c[] = "hello";
printf("%d %f %s\n", a, b, c);
}

C++: #include <iostream>


int main() {
int a = 4;
double b = 3.0;
char c[] = "hello";
std::cout << a << " " << b << " " << c << "\n"; 19/22
}
I/O Stream (Why should we prefer I/O stream?) 2/3

• Type-safe: The type of object provided to the I/O stream is known statically by the
compiler. In contrast, printf uses % fields to figure out the types dynamically

• Less error prone: With I/O Stream, there are no redundant % tokens that have to
be consistent with the actual objects passed to I/O stream. Removing redundancy
removes a class of errors

• Extensible: The C++ I/O Stream mechanism allows new user-defined types to be
passed to I/O stream without breaking existing code

• Comparable performance: If used correctly may be faster than C I/O ( printf ,


scanf , etc.) .
20/22
I/O Stream (Common C errors) 3/3

• Forget the number of parameters:


printf("long phrase %d long phrase %d", 3);

• Use the wrong format:


int a = 3;
...many lines of code...
printf(" %f", a);

• The %c conversion specifier does not automatically skip any leading white space:
scanf("%d", &var1);
scanf(" %c", &var2);

21/22
std::print

C++23 introduces an improved version of printf function std::print based on


formatter strings that provides all benefits of C++ stream and is less verbose
#include <print>

int main() {
std::print("Hello World! {}, {}, {}\n", 3, 4ll, "aa");
// print "Hello World! 3 4 aa"
}

This will be the default way to print when the C++23 standard is widely adopted

22/22

You might also like