Lec01 Intro
Lec01 Intro
Introduction
to Computing
Using C++
Introduction
• Overview of C++
2
Timeline of Computer History
• 1100 BC: Abacus • 1947: Transistors
• 1622: Slide rule • Late 1950s: Integrated circuits (ICs)
• 1642: Pascal’s Adder • 1951: UNIVAC
• 1673: Leibniz’s calculator • 1954: Bell Labs TRADIC (transistor-
based)
• 1804: Jacquard loom
• 1958, IBM 7090 (transistor-based)
• 1830s: Babbage’s computers
• 1964: IBM System/360 (based on ICs)
• 1847: Boolean logic
• 1971: Microprocessor
• 1880: Hollerith’s electric tabulator
• 1975: Altair 8880
• 1930s: Analog computer
• 1976-77: Apple I & II
• 1936-41: Zuse’s computers Z1 – Z4
• 1981: IBM PC
• 1944: Harvard Mark I
• 1980s, Most computers (VLSI)
• 1945: ENIAC (decimal)
• 1993, Intel Pentium PC
• 1946-49: EDVAC (binary) Digital
computers
7
What is a Computer?
• A computer is a machine that manipulates data
according to a list of instructions.
• e.g. desktop PCs, laptop PCs, smartphones, tablets,
game consoles like Xbox and Nintendo Switch, …
Output Unit
ALU
Memory Unit Storage Unit
CU Secondary storage devices
(hard drives, optical drives,
USB drives, memory cards …)
Puts the data processed by the
Directs the operations of the Output Unit computer on output devices
other units by providing (monitor, printer, speaker, haptic
timing and control signals. feedback, network, …)
Von Neumann Architecture
Flow of data
13
Levels of Machines
• There are several levels in
a computer (the exact
number is open to
debate), from the user
level down to the
transistor level.
• Progressing from the top
level downward, the
levels become less
abstract as more of the
internal structure of the Source: Principles of Computer Architecture by M. Murdocca and V. Heuring
computer becomes
visible.
14
Memory Hierarchy
• Registers: • Main memory
• Small, fast storage buffers within • Generally made up of DRAM
the CPU (dynamic random access memory)
• Compiler is responsible for • Relatively large storage capacity
managing their use, deciding compared with caches
which values should be kept in the • Relatively larger access time
available registers at each point in compared with SRAMs.
the program.
• Storing currently needed program
• Cache: instructions and data only
• Small, high speed temporary • Secondary Stores
storage
• Long access latency (SSDs, hard
• Generally made up of SRAM disks, tapes, etc.)
(static random access memory)
• Store data and programs that are
• Located on the CPU (Level 1/2/3 not actively needed
cache)
• Most-commonly-used data copied
to cache for faster access
• Small amount of cache is
sufficient for boosting
performance (temporal locality)
15
Memory Hierarchy Implication to a programmer:
When we write a program, try to exploit
temporal and spatial locality for good
execution performance.
But how?
DRAM
Disk
https://linux2me.wordpress.com/2017/09/15/linux-introduction-to-memory-management/
16
Programming Languages
• A computer is driven by software.
• Computer programs (software) are written in
programming languages.
• A programming language defines a set of
instructions in specific format that can be
understood by a computer.
• Two important issues on writing programs:
• Program syntax – Is the grammar of the instructions
correct?
• Program logic – Is the program
able to solve the problem?
17
High-Level Languages Language Inventors
• Procedural or Structural
• C, COBOL, Ada, Pascal, Basic
• Object-Oriented (OO)
• C++, Java, C#, VB.NET
COBOL: Pascal: C Language:
• Scripting Grace Murray
Hopper
Niklaus Wirth Dennis Ritchie
18
Compilers vs. Interpreters
• A compiler is a software program (or a set of
programs), which translates source code written in
high-level language into a low-level object code
(binary code) in machine language.
• The job is done offline. We say it’s done at compile time.
• This process is called compilation.
• The high-level language is called a compiled language.
Compilation
Machine language
High-level language
(Object code)
(Source code)
19
Compilers vs. Interpreters
• An interpreter actually does the same job as a
compiler except that the job is done at the moment
when the program is run.
• We say it translates code at run time.
• An interpreter works in a line-by-line manner.
• Convert one program statement to machine code, and execute
it immediately; then convert and execute the next statement.
• The high-level language that works in this way is called
an interpreted language.
Question:
Compiled languages vs. interpreted languages –
which runs faster in general?
20
Compilers vs. Interpreters
21
Object-Oriented Languages
• Simula The first OOP language (developed in 1960s)
• Ruby
• Python
• Objective-C
• VB.NET (Visual Basic)
22
How Many Programming
Languages
• How many programming languages are there?
• The most accepted answer seems to be Wikipedia’s
list of 700.
• However, there are only around 50 most popular
languages that are in common use today (according
to the Tiobe Index).
Source: https://careerkarma.com/blog/how-many-coding-languages-are-there/
23
Tiobe Index (as of July 2020)
• C++ is now ranked 4th in popularity worldwide.
• Ranked 3th in Aug 2018. Dethroned by Python in 2019.
Source: https://www.tiobe.com/tiobe-index/
24
A Funny but Truthful Programming
Language Comic A warning here!
C++ isn't easy to learn.
26
A Brief History of C++
• C++ can be said a sequel or extension of the C
programming language.
• Quiz time: Then who invented the C language?
• Answer: By Dennis Ritchie between 1969 and
1973 at Bell Labs, and used to re-implement
the Unix operating system.
• Btw, who was the main inventor of Unix?
• Answer: Ken Thompson
27
A Brief History of C++
• Ken Thompson and Dennis Ritchie
• 1983 Turing Award
• "For their development of generic operating systems
theory and specifically for the implementation of the
UNIX operating system."
• 1999 US National Medal of
Technology
• "For their invention of the
UNIX operating system
and the C programming
language."
28
A Brief History of C++
• In 1979, Bjarne Stroustrup (aged 71 already),
a Danish computer scientist of AT&T Bell Labs,
began work on extending the C language
with classes, and the new language was
called "C with Classes". Bjarne Stroustrup – C++ Language
30
Stroustrup@HK
Bjarne Stroustrup
Prof. Francis Lau (My PhD examiner)
31
Stroustrup @ HK
• My daughter loves this song (and code?) …
(Victoria, 2016)
32
Java vs. C++
• Java: by James Gosling at Sun Microsystems since 1995
• C++: by Bjarne Stroustrup at Bell Labs since 1979
VS
33
C++: Pros vs. Cons
Pros Cons
• General purpose • Inherit drawbacks of C
• (Highly) portable • Not secure / unsafe
• High performance • Because of pointers,
friend functions, global
• Good for writing device variables …
drivers or low-level • Complicated (to learn)
system programs
• Platform dependent 😵
• Widely used
• “Mix and match”
• Support object-oriented • OOP and procedural code
programming mixed together
34
Evolution of C++ Standards
• C++ is standardized by an ISO working group known
as JTC1/SC22/WG21. So far, it has published six
revisions of the C++ standard and is currently
working on the next revision, C++23.
Year C++ Standard Informal Name Remarks
1998 ISO/IEC 14882:1998 C++98
2003 ISO/IEC 14882:2003 C++03 Bug fixes
2011 ISO/IEC 14882:2011 C++11, C++0x Major revision
2014 ISO/IEC 14882:2014 C++14, C++1y Bug fixes
2017 ISO/IEC 14882:2017 C++17, C++1z Major revision
2020 ISO/IEC 14882:2020 C++20, C++2a
35
Phases of Making a C++ Program
Source Compile Object Link Executable
files files program
(.cpp files) (.obj files) (.exe files)
36
The Full Picture Program is created in
Editor Disk the editor and stored
on disk.
5.Load Loader
(legacy) 38
Your First C++ Program
• “Hello, World” is usually the first example when
learning nearly every programming language.
Hello, world!
• Comments
• provide program documentation /* This is a
• improve program readability multi-line comment */
• are ignored by the compiler
• Single-line comments begin with two forward slashes: //
• Multi-line comments are enclosed inside /* and */
41
1 // The first C++ program
2 #include <iostream>
3 using namespace std;
4 The main function
5 int main()
6 {
7 cout << "Hello, World! ";
Statement
8 cout << "Welcome to CSCI1120\n";
9 cout << "Introduction to Computing Using C++!\n";
10
11 return 0;
12 } // End of function "main"
42
1 // The first C++ program
2 #include <iostream>
3 using namespace std;
We need these in order
4 to use “cout” to perform
5 int main() output in the program
6 {
7 cout << "Hello, World! ";
8 cout << "Welcome to CSCI1120\n";
9 cout << "Introduction to Computing Using C++!\n";
10
11 return 0;
12 } // End of function "main"
• Performing simple output
• cout is an object that knows how to send output to the
screen
• "Hello, World!" are the data being passed to cout
• << (called the insertion operator) indicates that you want
to pass the data on the right of the operator to cout
43
1 // The first C++ program
2 #include <iostream>
3 using namespace std;
Do not confuse
4 backslash character (\)
5 int main()
String literal with forward slash (/)
6 {
7 cout << "Hello, World! ";
8 cout << "Welcome to CSCI1120\n";
9 cout << "Introduction to Computing Using C++!\n";
10
11 return 0;
12 } // End of function "main"
Hello! Welcome to CSCI1120
Introduction to Computing Using C++!
• String literals: enclosed by a pair of double quotes (")
• Special characters in a string literal
• \n newline (enter key)
• \\ backslash (\)
• \" double quote (")
• \t tab key
44
1 // The first C++ program
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 Cout << "Hello! ";
8 Cout << "Welcome to CSCI1120\n";
9 Cout << "Introduction to Computing Using C++!\n";
10
11 return 0;
12 } // End of function "main"
• Every programming language has its own syntax, which
is very strict and must be obeyed.
• A program would have compilation error if it fails to
follow the syntax
45
Summary
• Basic components of a computer
• Basic programming concepts:
• Programming in high-level language (human readable)
• Compiled and linked as a program executable
• Computers run the machine code
• Overview of C++: pros and cons
• First look at a C++ program
46