Multiplying Two-Digit Numbers Using Grid Method

Programming in C++
David Bedn
árek
2022/2023
undefined
2
NPRG041 Programming in C++ - 2021/2022 David Bednárek
History and Literature
templates
exceptions
1991
classes
messages
classes
virtual
methods
function
prototypes
STL
=
containers
+
algorithms
a
l
l
o
c
a
v
a
r
i
a
b
l
e
-
s
i
z
e
d
 
a
r
r
a
y
s
Ancient h
istor
y of
 
C and 
C++
B
(Bell Labs. 1969)
BCPL
(Cambridge 1966)
C with classes
(
Stroustrup, Bell Labs
 
1979
)
The C++ programming language
(
1st edition - Stroustrup
 
1985
)
C
+
+
9
8
(
ISO/IEC 14882 1998
)
C
(Bell Labs. 1971)
The C programming language
(
Kernigan & Ritchie
 197
8
)
Objective-C
(
Cox & Love 1981
)
Object-Oriented Programing
(
Cox
 
1986
)
A
N
S
I
 
C
(
ANSI X3J11
 
1989
)
C
9
9
(
ISO/IEC 9899 1999
)
almost superset
inspired
superset
significant change
Java
(Sun 1995)
L
i
n
u
x
1
9
9
1
U
n
i
x
1
9
7
3
W
i
n
d
o
w
s
 
N
T
1
9
9
3
O
S
-
X
2
0
0
0
M
a
c
O
S
1
9
8
4
Simula
(196
7
)
gcc
1987
MSVC
1993
language
book
=
de-facto
standard
standard
OS
notable
compiler
Turbo C IDE
1987
P
C
-
D
O
S
1
9
8
1
Watcom C
1988
SW
DOOM
1993
m
o
d
u
l
e
s
c
o
n
c
e
p
t
s
r
a
n
g
e
s
coroutines
fold-expressions
deduction guides
optional, variant
filesystem
make_unique
variable templates
i
m
p
o
r
t
 
s
t
d
;
mdspan
auto
range-for 
m
o
v
e
-
s
e
m
a
n
t
i
c
s
s
m
a
r
t
 
p
o
i
n
t
e
r
s
type templates
v
a
r
i
a
d
i
c
 
t
e
m
p
l
a
t
e
s
l
a
m
b
d
a
p
a
r
a
l
l
e
l
i
s
m
p
a
r
a
l
l
e
l
i
s
m
Modern h
istor
y of
 C++
 and related languages
C
+
+
9
8
(
ISO/IEC 14882 1998
)
C
+
+
0
3
(
2003
)
C
+
+
0
x
 
=
 
C
+
+
1
1
(
2011
)
C
+
+
1
4
(
2
0
1
4
)
Objective-C
(
Cox & Love 1981
)
Objective-
C
 2.0
(
Apple
 
2006
)
Objective-C++
(
Apple 2010
)
C
1
1
(
2011
)
C
9
9
(
ISO/IEC 9899 1999
)
C
#
(
Microsoft
 
2002
)
C
++/CLI
(
Microsoft
 
2005
)
C
+
+
1
7
(
2
0
1
7
)
C
+
+
2
0
(
2
0
2
0
)
C
1
8
(2018)
C
+
+
2
3
(
2
0
2
3
)
clang
2007
language
very important standard
compiler
extinct language
important standard
defect-correcting standard
Books
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Be sure that you have (at least) the 
C++11 versions 
of the books
Introduction to programming (using C++)
Stanley B. Lippman, Josée Lajoie, Barbara E. Moo: C++ Primer (
5th Edition
)
Addison-Wesley 2012 (976 pages)
Bjarne Stroustrup: Programming: Principles and Practice Using C++ (
2nd Edition
)
Addison-Wesley 2014 (1312 pages)
Introduction to C++
Bjarne Stroustrup: A Tour of C++ (
2nd Edition
)
Addison-Wesley 2018 (256 pages)
Reference
Bjarne Stroustrup: The C++ Programming Language - 
4
th Edition
Addison-Wesley 2013
Nicolai M. Josuttis: The C++ Standard Library: A Tutorial and Reference (
2nd
Edition
)
Addison-Wesley 2012
Books
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Be sure that you have the 
C++11 versions 
of the books
Best practices
Scott Meyers: Effective 
Modern
 C++
O'Reilly 2014 (334 pages)
Advanced [not in this course]
David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor:
C++ Templates: The Complete Guide (
2nd Edition
)
Addison-Wesley 2017 (832 pages)
Anthony Williams: C++ Concurrency in Action: Practical Multithreading
Manning Publications 2012 (528 pages)
On-line materials
Bjarne Stroustrup, Herb Sutter: C++ Core Guidelines
github.com/isocpp/CppCoreGuidelines
Nate Kohl et al.: C++ reference [C++98, C++03, C++11, C++14, C++17, C++20]
cppreference.com
undefined
7
NPRG041 Programming in C++ - 2021/2022 David Bednárek
The 
C++
 Programming Language
C++
C/C++ can live alone
No need for an interpreter or JIT compiler at run-time
Run-time support library contains only the parts really required
Restricted environments may run with less-than-standard support
Dynamic allocation and/or exceptions may be stripped off
Code may work with no run-time support at all
Compilers allow injection of system/other instructions within C/C++ code
Inline assembler or intrinsic functions
Code may be mixed with/imported to other languages
There is no other major language capable of this
All current major OS kernels are implemented in C
C was designed for this role as part of the second implementation of Unix
C++ would be safer but it did not exist
Almost all run-time libraries of other languages are implemented in C/C++
If C/C++ dies, all the other languages will die too
NPRG041 Programming in C++ - 2021/2022 David Bednárek
8
C++
C/C++ is fast
Only FORTRAN can currently match C/C++
C++ is exactly as fast as C
But programming practices in C++ often trade speed for safety
Why?
The effort spent by FORTRAN/C/C++ compiler teams on optimization
40 years of development
Strongly typed language with minimum high-level features
No garbage-collection, reflexion, introspection, …
The language does not enforce any particular programming paradigm
C++ is not necessarily object-oriented
The programmer controls the placement and lifetime of objects
If necessary, the code may be almost as low-level as assembly language
High-Performance Computing (HPC) is done in FORTRAN and C/C++
python/R/matlab may also work in HPC well…
…but only if most work is done inside library functions (implemented in C)
NPRG041 Programming in C++ - 2021/2022 David Bednárek
9
undefined
10
NPRG041 Programming in C++ - 2021/2022 David Bednárek
Major features specific for C++
(compared to other modern languages)
Major distinguishing features of C++ (for beginners)
Archaic text-based system for publishing module interfaces
Will be (gradually) replaced by true modules defined in C++20
All major compilers (as of 2023) implement the modules in the language
The standard library implementations are not yet ready for the module interface
No 100%-reliable protections
Programmer’s mistakes may result in uncontrolled crashes
Hard crashes (invalid memory accesses) cannot be caught as exceptions
Some compilers can do it in some cases
Preference for value types
Similar to old languages, unlike any modern (imperative) language
Objects are often manipulated by copying/moving instead of sharing references
to them
No implicit requirement for dynamic allocation
No garbage collector
Approximated by smart pointers since C++11
Safety still dependent on programmer's discipline
11
NPRG041
Programmin
g in C++ -
2021/2022
David
Bednárek
Major distinguishing features of C++ (for beginners)
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do it blows your whole leg off.
Bjarne Stroustrup, creator of C++
12
NPRG041
Programmin
g in C++ -
2021/2022
David
Bednárek
Major distinguishing features of C++ (for beginners)
java/C#/...
void f(/*...*/)
{
   T v = new T(/*...*/);
  
// v 
is a reference
   do_it(v);
  
// the reference is passed
}
Do we really need dynamic allocation
here?
Probably not, but...
... what if do_it stores a copy of the
reference somewhere
Programmers don't care
The language enforces the use of
new
Advanced compilers (escape
analysis) may sometimes detect that
dynamic allocation is not needed
The code is then converted into an
equivalent of the C++ value style
modern C++
Value-based approach
void f(/*...*/)
{
   T v(/*...*/);
  
// v 
is the object
   do_it(v);
  
// usually passed by reference
}
do_it shall not store the reference to
v
 anywhere
if it does, the program will probably
crash later
see 
"Shooting in one's foot"
C++ conventions include this:
If an object is passed by reference
to a function, the function must stop
using the reference upon its exit
technically, do_it 
can
 store the
reference (e.g. in a static variable),
but it requires ugly code
NPRG041 Programming in C++ - 2021/2022 David Bednárek
13
Major distinguishing features of C++ (for beginners)
java/C#/...
void f(/*...*/)
{
   T v = new T(/*...*/);
  
// v 
is a reference
   do_it(v);
  
// the reference is passed
}
Do we really need dynamic allocation
here?
Probably not, but...
... what if do_it stores a copy of the
reference somewhere
Programmers don't care
The language enforces the use of
new
Advanced compilers (escape
analysis) may sometimes detect that
dynamic allocation is not needed
The code is then converted into an
equivalent of the C++ value style
modern C++
Smart pointers
void f(/*...*/)
{
 
auto v =
       std::make_unique<T>(/*...*/);
  
// v 
is a smart pointer
   do_it(std::move(v));
  
// ownership of the object
  
// transferred to do_it
}
If we really need to store a reference
to 
v
 forever
Dynamic allocation required
Wrapped into smart-pointers
Passing smart pointers around often
requires special syntax
It acts as a warning to readers
It is far more complex than java etc.
NPRG041 Programming in C++ - 2021/2022 David Bednárek
14
Major distinguishing features of C++ (for beginners)
Value-based approach
Suitable function declaration
void do_it(T 
&
 p);
or
void do_it(
const
 T 
&
 p);
Usage
void f(/*...*/)
{
   T v(/*...*/);
  
// v 
is the object
   do_it(v);
  
// usually passed by reference
}
C++ conventions include this:
If an object is passed by reference
to a function, the function must stop
using the reference upon its exit
This is NOT enforced by the
language itself
technically, do_it 
can
 store the
reference (e.g. in a static variable),
but it requires unusual code
T * g = nullptr;
void do_it(T & p) { g = &p; }
Smart pointers
Suitable function declaration
void do_it(std::
unique_ptr
<T> p);
or
void do_it(std::
unique_ptr
<T> 
&&
 p);
Usage
void f(/*...*/)
{
 
auto v =
       std::make_unique<T>(/*...*/);
  
// v 
is a smart pointer
   do_it(std::
move
(v));
  
// ownership of the object
  
// transferred to do_it
}
Passing smart pointers around often
requires special syntax
std::move(v), &*v, etc.
It acts as a warning to readers
There are other smart pointers
std::shared_ptr<T>
There are 
observer
 pointers
T *
const T *
NPRG041 Programming in C++ - 2021/2022 David Bednárek
15
Major distinguishing features of C++ (for beginners)
java/C#/...
Programmers don't care
about the lifetime of objects
The have no choice anyway
Advanced compilers may optimize
Shouldn't a programmer have an
idea of what will happen to their
object?
modern C++
Programmers must think
about the lifetime of objects
It kills beginners
It helps in large projects
You have to select from a variety
of pointer/reference types
You sometimes have to use some
operators when passing
pointer/references around
This acts as a documentation!
If you adhere to conventions
Details later…
NPRG041 Programming in C++ - 2021/2022 David Bednárek
16
Major distinguishing features of C++ (for advanced programmers)
User-defined operators
Pack sophisticated technologies into symbolic interfaces
C and the standard library of C++ define widely-used conventions
Extremely strong generic-programming mechanisms
Turing-complete compile-time computing environment for meta-programming
No run-time component – zero runtime cost of being generic
C++ is now more complex than any other general programming language
ever created
17
NPRG041
Programmin
g in C++ -
2021/2022
David
Bednárek
undefined
18
NPRG041 Programming in C++ - 2021/2022 David Bednárek
Programming languages and compilers
undefined
Compilers produce binary packages from source code
These packages are also read by the compiler when referenced
All languages created after 1990 use something like import/require clauses
But not in C/C++ before C++20
C++20 has modules and module interfaces, more complex than in java
Compilation in modern languages
19
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.java
module a;
b.java
module b;
requires a;
javac
javac
a.class
b.class
JRE
undefined
Why not in C/C++? There are disadvantages:
When anything inside a.java changes, new timestamp of a.class induces
recompilation of b.java
Even if the change is not in the public interface
How do you handle cyclic references?
Compilation in modern languages
20
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.java
module a;
b.java
module b;
requires a;
javac
javac
a.class
b.class
interface
implementation
interface
interface
implementation
interface
implementation
interface
implementation
undefined
In C, the situation was simple
Interface = function headers
 in „header files“
Typically small
Implementation 
= function bodies in “C files”
Change of a.c does not require recompilation of b.c
Compilation in C
21
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.c
#
i
nclude “a.h”
b.c
#
i
nclude “a.h”
cc
cc
a.o
b.o
interface
implementation
interface
implementation
implementation
implementation
a.h
undefined
In modern C++, the separate compilation is no longer an advantage
Interface (classes etc.) is often larger than implementation (function bodies)
Changes often affect the interface, not (only) the body
The purely textual behavior of #include is anachronism
Compilation in C++ (before C++20 modules)
22
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.cpp
#
i
nclude “a.hpp”
b.cpp
#
i
nclude “a.hpp”
cc++
cc++
a.o
b.o
interface
implementation
interface
implementation
implementation
implementation
a.hpp
undefined
Implementation of generic functions (templates) must be visible where
called
Explanation later…
Generic code often comprises of header files only
Compilation of generic code in C++ (before C++20 modules)
23
NPRG041 Programming in C++ - 2021/2022 David Bednárek
b.cpp
#
i
nclude “a.hpp”
cc++
b.o
interface
implementation
implementation
a.hpp
implementation
undefined
Object files (.o, .obj) contain binary code of target platform
They are incomplete – not executable yet
Linker/loader merges them together with library code
Static/dynamic libraries. Details later…
Compilation in C++ (before C++20 modules)
24
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.cpp
#
i
nclude “a.hpp”
b.cpp
#
i
nclude “a.hpp”
cc++
cc++
a.o
b.o
a.hpp
linker
loader
Execu-
table
lib1.a
lib2.so
Operating
system
Compiler
suite
undefined
The (contents of) a .o [unix] or .obj [windows] file is called a 
module
also applied to the corresponding .c or .cpp file
one module = one independent run of the compiler
if more .cpp files specified at compiler command-line, they are still independent
This is related but 
not the same 
meaning as in C++20 modules
Compilation in C++ (before C++20 modules)
25
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.cpp
#
i
nclude “a.hpp”
b.cpp
#
i
nclude “a.hpp”
cc++
cc++
a.o
b.o
a.hpp
linker
loader
Execu-
table
lib1.a
lib2.so
Operating
system
Compiler
suite
undefined
Problems
The files can no longer be compiled in arbitrary order
New build system required
Module interface files must be compiled before module implementation files
The suffix 
.ixx 
of 
module interface files 
is Microsoft-specific solution of this problem
There may be dependences between different 
module interface files
Compilation with C++20 modules (preview)
26
NPRG041 Programming in C++ - 2021/2022 David Bednárek
a.cpp
module a;
/*implementation*/
b.cpp
module b;
import a;
cc++
cc++
a.o
b.o
a.ixx
export module a;
/*interface*/
cc++
a.???
Slide Note
Embed
Share

Learn how to multiply two-digit numbers using the grid method through step-by-step examples. Understand the concept of partitioning numbers and placing digits effectively in the grid to calculate the product accurately. Practice with multiple exercises to enhance your multiplication skills.

  • Multiplication
  • Grid Method
  • Math
  • Two-Digit Numbers
  • Calculation

Uploaded on Feb 26, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Programming in C++ David Bedn rek 2022/2023

  2. History and Literature NPRG041 Programming in C++ - 2021/2022 David Bedn rek 2

  3. Ancient history of C and C++ classes virtual methods Unix 1973 B C BCPL Simula (1967) (Bell Labs. 1969) (Bell Labs. 1971) (Cambridge 1966) The C programming language (Kernigan & Ritchie 1978) classes messages C with classes (Stroustrup, Bell Labs 1979) Objective-C (Cox & Love 1981) PC-DOS 1981 function prototypes MacOS 1984 The C++ programming language (1st edition - Stroustrup1985) Object-Oriented Programing (Cox 1986) gcc 1987 Turbo C IDE 1987 Watcom C 1988 inspired ANSI C (ANSI X3J11 1989) superset templates exceptions 1991 almost superset significant change Linux 1991 Java (Sun 1995) MSVC 1993 Windows NT 1993 DOOM 1993 STL = + C++98 containers algorithms C99 (ISO/IEC 14882 1998) (ISO/IEC 9899 1999) book = de-facto standard alloca OS-X 2000 variable-sized arrays notable compiler OS SW standard language

  4. Modern history of C++ and related languages C99 C++98 Objective-C (Cox & Love 1981) (ISO/IEC 9899 1999) (ISO/IEC 14882 1998) C# (Microsoft 2002) C++03 (2003) Objective-C 2.0 (Apple 2006) C++/CLI (Microsoft 2005) auto clang 2007 range-for move-semantics smart pointers type templates variadic templates lambda parallelism Objective-C++ (Apple 2010) C11 (2011) parallelism C++0x = C++11 (2011) make_unique variable templates C++14 (2014) fold-expressions deduction guides optional, variant filesystem language C++17 (2017) C18 (2018) extinct language modules concepts ranges coroutines very important standard C++20 (2020) important standard defect-correcting standard import std; mdspan C++23 (2023) compiler

  5. Books http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Be sure that you have (at least) the C++11 versions of the books Introduction to programming (using C++) Stanley B. Lippman, Jos e Lajoie, Barbara E. Moo: C++ Primer (5th Edition) Addison-Wesley 2012 (976 pages) Bjarne Stroustrup: Programming: Principles and Practice Using C++ (2nd Edition) Addison-Wesley 2014 (1312 pages) Introduction to C++ Bjarne Stroustrup: A Tour of C++ (2nd Edition) Addison-Wesley 2018 (256 pages) Reference Bjarne Stroustrup: The C++ Programming Language - 4th Edition Addison-Wesley 2013 Nicolai M. Josuttis: The C++ Standard Library: A Tutorial and Reference (2nd Edition) Addison-Wesley 2012

  6. Books http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Be sure that you have the C++11 versions of the books Best practices Scott Meyers: Effective Modern C++ O'Reilly 2014 (334 pages) Advanced [not in this course] David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor: C++ Templates: The Complete Guide (2nd Edition) Addison-Wesley 2017 (832 pages) Anthony Williams: C++ Concurrency in Action: Practical Multithreading Manning Publications 2012 (528 pages) On-line materials Bjarne Stroustrup, Herb Sutter: C++ Core Guidelines github.com/isocpp/CppCoreGuidelines Nate Kohl et al.: C++ reference [C++98, C++03, C++11, C++14, C++17, C++20] cppreference.com

  7. The C++ Programming Language NPRG041 Programming in C++ - 2021/2022 David Bedn rek 7

  8. C++ C/C++ can live alone No need for an interpreter or JIT compiler at run-time Run-time support library contains only the parts really required Restricted environments may run with less-than-standard support Dynamic allocation and/or exceptions may be stripped off Code may work with no run-time support at all Compilers allow injection of system/other instructions within C/C++ code Inline assembler or intrinsic functions Code may be mixed with/imported to other languages There is no other major language capable of this All current major OS kernels are implemented in C C was designed for this role as part of the second implementation of Unix C++ would be safer but it did not exist Almost all run-time libraries of other languages are implemented in C/C++ If C/C++ dies, all the other languages will die too NPRG041 Programming in C++ - 2021/2022 David Bedn rek 8

  9. C++ C/C++ is fast Only FORTRAN can currently match C/C++ C++ is exactly as fast as C But programming practices in C++ often trade speed for safety Why? The effort spent by FORTRAN/C/C++ compiler teams on optimization 40 years of development Strongly typed language with minimum high-level features No garbage-collection, reflexion, introspection, The language does not enforce any particular programming paradigm C++ is not necessarily object-oriented The programmer controls the placement and lifetime of objects If necessary, the code may be almost as low-level as assembly language High-Performance Computing (HPC) is done in FORTRAN and C/C++ python/R/matlab may also work in HPC well but only if most work is done inside library functions (implemented in C) NPRG041 Programming in C++ - 2021/2022 David Bedn rek 9

  10. Major features specific for C++ (compared to other modern languages) NPRG041 Programming in C++ - 2021/2022 David Bedn rek 10

  11. Major distinguishing features of C++ (for beginners) Archaic text-based system for publishing module interfaces Will be (gradually) replaced by true modules defined in C++20 All major compilers (as of 2023) implement the modules in the language The standard library implementations are not yet ready for the module interface No 100%-reliable protections Programmer s mistakes may result in uncontrolled crashes Hard crashes (invalid memory accesses) cannot be caught as exceptions Some compilers can do it in some cases Preference for value types Similar to old languages, unlike any modern (imperative) language Objects are often manipulated by copying/moving instead of sharing references to them No implicit requirement for dynamic allocation No garbage collector Approximated by smart pointers since C++11 Safety still dependent on programmer's discipline NPRG041 Programmin g in C++ - 2021/2022 11 David

  12. Major distinguishing features of C++ (for beginners) C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off. Bjarne Stroustrup, creator of C++ NPRG041 Programmin g in C++ - 2021/2022 12 David

  13. Major distinguishing features of C++ (for beginners) java/C#/... modern C++ Value-based approach void f(/*...*/) { T v(/*...*/); // v is the object void f(/*...*/) { T v = new T(/*...*/); // v is a reference do_it(v); } do_it(v); } // usually passed by reference // the reference is passed do_it shall not store the reference to v anywhere if it does, the program will probably crash later see "Shooting in one's foot" C++ conventions include this: If an object is passed by reference to a function, the function must stop using the reference upon its exit technically, do_it can store the reference (e.g. in a static variable), but it requires ugly code Do we really need dynamic allocation here? Probably not, but... ... what if do_it stores a copy of the reference somewhere Programmers don't care The language enforces the use of new Advanced compilers (escape analysis) may sometimes detect that dynamic allocation is not needed The code is then converted into an equivalent of the C++ value style NPRG041 Programming in C++ - 2021/2022 David Bedn rek 13

  14. Major distinguishing features of C++ (for beginners) java/C#/... modern C++ Smart pointers void f(/*...*/) { auto v = std::make_unique<T>(/*...*/); // v is a smart pointer void f(/*...*/) { T v = new T(/*...*/); // v is a reference do_it(v); } do_it(std::move(v)); // ownership of the object // transferred to do_it } // the reference is passed Do we really need dynamic allocation here? Probably not, but... ... what if do_it stores a copy of the reference somewhere Programmers don't care The language enforces the use of new Advanced compilers (escape analysis) may sometimes detect that dynamic allocation is not needed The code is then converted into an equivalent of the C++ value style If we really need to store a reference to v forever Dynamic allocation required Wrapped into smart-pointers Passing smart pointers around often requires special syntax It acts as a warning to readers It is far more complex than java etc. NPRG041 Programming in C++ - 2021/2022 David Bedn rek 14

  15. Major distinguishing features of C++ (for beginners) Value-based approach Suitable function declaration void do_it(T & p); or void do_it(const T & p); Smart pointers Suitable function declaration void do_it(std::unique_ptr<T> p); or void do_it(std::unique_ptr<T> && p); Usage Usage void f(/*...*/) { T v(/*...*/); // v is the object void f(/*...*/) { auto v = std::make_unique<T>(/*...*/); // v is a smart pointer do_it(v); } C++ conventions include this: If an object is passed by reference to a function, the function must stop using the reference upon its exit This is NOT enforced by the language itself technically, do_it can store the reference (e.g. in a static variable), but it requires unusual code T * g = nullptr; void do_it(T & p) { g = &p; } // usually passed by reference do_it(std::move(v)); // ownership of the object // transferred to do_it } Passing smart pointers around often requires special syntax std::move(v), &*v, etc. It acts as a warning to readers There are other smart pointers std::shared_ptr<T> There are observer pointers T * const T * NPRG041 Programming in C++ - 2021/2022 David Bedn rek 15

  16. Major distinguishing features of C++ (for beginners) java/C#/... Programmers don't care about the lifetime of objects The have no choice anyway Advanced compilers may optimize modern C++ Programmers must think about the lifetime of objects It kills beginners It helps in large projects Shouldn't a programmer have an idea of what will happen to their object? You have to select from a variety of pointer/reference types You sometimes have to use some operators when passing pointer/references around This acts as a documentation! If you adhere to conventions Details later NPRG041 Programming in C++ - 2021/2022 David Bedn rek 16

  17. Major distinguishing features of C++ (for advanced programmers) User-defined operators Pack sophisticated technologies into symbolic interfaces C and the standard library of C++ define widely-used conventions Extremely strong generic-programming mechanisms Turing-complete compile-time computing environment for meta-programming No run-time component zero runtime cost of being generic C++ is now more complex than any other general programming language ever created NPRG041 Programmin g in C++ - 2021/2022 17 David

  18. Programming languages and compilers NPRG041 Programming in C++ - 2021/2022 David Bedn rek 18

  19. Compilation in modern languages a.java javac a.class module a; JRE b.java javac b.class module b; requires a; Compilers produce binary packages from source code These packages are also read by the compiler when referenced All languages created after 1990 use something like import/require clauses But not in C/C++ before C++20 C++20 has modules and module interfaces, more complex than in java NPRG041 Programming in C++ - 2021/2022 David Bedn rek 19

  20. Compilation in modern languages a.java interface interface javac a.class implementation implementation module a; b.java interface interface javac b.class module b; requires a; implementation implementation Why not in C/C++? There are disadvantages: When anything inside a.java changes, new timestamp of a.class induces recompilation of b.java Even if the change is not in the public interface How do you handle cyclic references? NPRG041 Programming in C++ - 2021/2022 David Bedn rek 20

  21. Compilation in C a.c implementation cc a.o implementation #include a.h a.h b.c cc b.o implementation implementation #include a.h In C, the situation was simple Interface = function headers in header files Typically small Implementation = function bodies in C files Change of a.c does not require recompilation of b.c NPRG041 Programming in C++ - 2021/2022 David Bedn rek 21

  22. Compilation in C++ (before C++20 modules) a.cpp implementation cc++ a.o implementation #include a.hpp a.hpp b.cpp cc++ b.o implementation implementation #include a.hpp In modern C++, the separate compilation is no longer an advantage Interface (classes etc.) is often larger than implementation (function bodies) Changes often affect the interface, not (only) the body The purely textual behavior of #include is anachronism NPRG041 Programming in C++ - 2021/2022 David Bedn rek 22

  23. Compilation of generic code in C++ (before C++20 modules) a.hpp b.cpp cc++ b.o implementation implementation #include a.hpp Implementation of generic functions (templates) must be visible where called Explanation later Generic code often comprises of header files only NPRG041 Programming in C++ - 2021/2022 David Bedn rek 23

  24. Compilation in C++ (before C++20 modules) Compiler suite Operating system a.cpp cc++ a.o #include a.hpp Execu- table a.hpp linker loader b.cpp cc++ b.o #include a.hpp lib1.a lib2.so Object files (.o, .obj) contain binary code of target platform They are incomplete not executable yet Linker/loader merges them together with library code Static/dynamic libraries. Details later NPRG041 Programming in C++ - 2021/2022 David Bedn rek 24

  25. Compilation in C++ (before C++20 modules) Compiler suite Operating system a.cpp cc++ a.o #include a.hpp Execu- table a.hpp linker loader b.cpp cc++ b.o #include a.hpp lib1.a lib2.so The (contents of) a .o [unix] or .obj [windows] file is called a module also applied to the corresponding .c or .cpp file one module = one independent run of the compiler if more .cpp files specified at compiler command-line, they are still independent This is related but not the same meaning as in C++20 modules NPRG041 Programming in C++ - 2021/2022 David Bedn rek 25

  26. Compilation with C++20 modules (preview) a.cpp module a; /*implementation*/ cc++ a.o a.ixx export module a; /*interface*/ cc++ a.??? b.cpp module b; import a; cc++ b.o Problems The files can no longer be compiled in arbitrary order New build system required Module interface files must be compiled before module implementation files The suffix .ixx of module interface files is Microsoft-specific solution of this problem There may be dependences between different module interface files NPRG041 Programming in C++ - 2021/2022 David Bedn rek 26

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#