0% found this document useful (0 votes)
12 views

Lec01 Intro

Uploaded by

wongtosang8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lec01 Intro

Uploaded by

wongtosang8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

CSCI1120

Introduction
to Computing
Using C++

Introduction

Class B, Fall 2023


By Dr. Lam King Tin
Outline
• Introduction to Computing and Programming

• 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, …

• What do computers have in common?


11
Computer Organization
Input Unit
CPU John von Neumann
in the 1940s
Arithmetic
& Logic Unit Memory Unit Storage Unit
Control
Unit

Output Unit

Von Neumann Architecture


Flow of data
12
Computer Organization
Obtains data from input devices
Performs arithmetic and (keyboard, mouse, microphone,
logic operations Input Unit touch panel, network, …)

CPU Refers to main / primary memory


or Random Access Memory (RAM)

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?

SRAM is fast but


expensive while DRAM is (usually 1 CPU cycle)
slower (often by a factor of
10) but less expensive (by
a factor of 20). SRAM is
usually used as a hardware SRAM
cache.

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

• JavaScript, VBScript, PHP, Perl, Language Inventors


Python, Ruby, Shell scripts
• Functional
• Haskell, Lisp, Scala
There is no overarching classification scheme for programming
languages; but Wikipedia keeps a long list classified by “type”. Java C++
James Gosling Bjarne Stroustrup
https://en.wikipedia.org/wiki/List_of_programming_languages_by_type

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

Compiled languages: Interpreted languages:


• C • Perl
• C++ • Python
• Visual Basic • PHP
• Java (to bytecode) • JavaScript
• C# (to bytecode) • VBScript
• Objective-C • Ruby
• Swift • Unix shell scripts
• Go • PowerShell

21
Object-Oriented Languages
• Simula The first OOP language (developed in 1960s)

• Smalltalk Developed by Alan


Kay; released in 1972
• C++
• C# (C sharp)
• Java Ole-Johan Dahl and Kristen Nygaard

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

An indicator of the popularity


of programming languages.

Source: https://www.tiobe.com/tiobe-index/
24
A Funny but Truthful Programming
Language Comic A warning here!
C++ isn't easy to learn.

Comic source: http://quantlabs.net/blog/2016/02/truthful-programming-language-comic/


25
Overview of C++
• History of C and C++

• C++: pros and cons

• Phases of developing a C++ program

• First view of a C++ program

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

One of my most productive days was throwing


Dennis Ritchie – C Language
away 1,000 lines of code.
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

• C++, the successor of "C with classes", was designed by


Stroustrup in 1986, and version 2.0 was introduced in 1989.
• The design of C++ was guided by the following THREE
principles:
• The use of classes would not result in programs executing any more
slowly than programs not using classes.
• C programs should run as a subset of C++ programs.
• No run-time inefficiency should be added to the language.
How do you pronounce "Bjarne Stroustrup?"
http://www.stroustrup.com/bs_faq.html#pronounce
29
Stroustrup@HK
• Bjarne came to Hong
Kong in October
2017!
• He presented at HKU.

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

They look so similar but different!

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)

Other object files and library files


You edit these files
(.obj, .lib, .dll, …)
• Compile: Translate C++ codes into equivalent machine
(or binary) codes
• Each object file is like a “part” of a program

• Link: Put all the binary “parts” together to form an


executable program

• In VS/Xcode, Compile + Link = “Build”

36
The Full Picture Program is created in
Editor Disk the editor and stored
on disk.

• Typical phases: Preprocessor Disk Preprocessor program


processes the code.
1.Edit Compiler creates
Compiler Disk object code and stores
2.Preprocess it on disk.
Linker links the object
3.Compile Linker Disk code with the libraries,
creates an executable file
4.Link Primary and stores it on disk
Memory

5.Load Loader

6.Execute Loader puts program


in memory.
Disk
• 2-3 are compile-time
.
.
.

• 5-6 are run-time


Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
..
storing new data
.. values as the program
..
executes.
37
C++ Compilers
• The list of C++ compilers is just too long.
• Wikipedia: https://en.wikipedia.org/wiki/List_of_compilers#C++_compilers
• Stroustrup's list: http://www.stroustrup.com/compilers.html But both lists are
still incomplete.
• A few notable examples:
• GCC (GNU Project): open-source; it compiles C and C++;
the command for compiling a C++ program is called g++.
• Clang (an LLVM project): relatively new; very active
development; adopted in XCode on Mac.
• Intel C++ Compiler
• Microsoft Visual C++ Compiler

(legacy) 38
Your First C++ Program
• “Hello, World” is usually the first example when
learning nearly every programming language.

Hello, world!

(Victoria's newborn picture) 39


Your First C++ Program
• A C++ program that outputs a few lines of text
1 // Your first C++ program
2 #include <iostream>
3 using namespace std;
4
5 int main()
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" Output to console screen
Hello, World! Welcome to CSCI1120.
Introduction to Computing Using C++!
40
1 // The first C++ program
2 #include <iostream>
3 using namespace std;
Single-line comments
4
5 int main()
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"

• 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"

• Every C++/C program has exactly one function


named “main”.
• The function body contains the statements that
make up your program.
• A statement is terminated by a semi-colon (;)

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

Next: C++ Basics (focus on syntax)

46

You might also like