ddc2133 c++ topic1
TRANSCRIPT
-
8/13/2019 DDC2133 c++ Topic1
1/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 1
Chapter 1 Introduction to Computers,Programs, and C++
Lecturer: Mrs Rohani Hassan
UTM
-
8/13/2019 DDC2133 c++ Topic1
2/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 2
Objectives
To review computer basics, programs, and operating systems( 1.2-1.4).(Optional) To represent numbers in binary, decimal, andhexadecimal ( 1.5 Optional).To know the history of C++ (1.6).To write a simple C++ program (1.7).To understand the C++ program development cycle (1.8).To develop C++ using C++Builder (1.9).To develop C++ using command line tools on Windows (1.10).To develop C++ using command line tools on Unix (1.11).To develop C++ using Visual C++.Net (1.12).
-
8/13/2019 DDC2133 c++ Topic1
3/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 3
What is a Computer?
A computer consists of a CPU, memory, hard disk, floppy disk,monitor, printer, and communication devices.
CPU
e.g., Disk, CD,and Tape
InputDevices
e.g., Keyboard,Mouse
e.g., Monitor,Printer
CommunicationDevices
e.g., Modem,and NIC
StorageDevices Memory
OutputDevices
Bus
-
8/13/2019 DDC2133 c++ Topic1
4/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 4
CPUThe central processing unit (CPU) is the brain of a computer. Itretrieves instructions from memory and executes them. The CPUspeed is measured in megahertz (MHz), with 1 megahertz equaling 1million pulses per second. The speed of the CPU has been improvedcontinuously. If you buy a PC now, you can get an Intel Pentium 4
Processor at 3 gigahertz (1 gigahertz is 1000 megahertz).
CPU
e.g., Disk, CD,and Tape
InputDevices
e.g., Keyboard,Mouse
e.g., Monitor,Printer
CommunicationDevices
e.g., Modem,and NIC
StorageDevices Memory
Output
Devices
Bus
-
8/13/2019 DDC2133 c++ Topic1
5/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 5
Memory Memory is to store data and program instructions for CPU toexecute. A memory unit is an ordered sequence of bytes, each holdseight bits. A program and its data must be brought to memory beforethey can be executed. A memory byte is never empty, but its initialcontent may be meaningless to your program. The current content of
a memory byte is lost whenever new information is placed in it.
CPU
e.g., Disk, CD,and Tape
Input
Devices
e.g., Keyboard,Mouse
e.g., Monitor,Printer
Communication
Devices e.g., Modem,
and NIC
StorageDevices Memory
Output
Devices
Bus
-
8/13/2019 DDC2133 c++ Topic1
6/31
-
8/13/2019 DDC2133 c++ Topic1
7/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 7
Storage DevicesMemory is volatile, because information is lost when the power isoff. Programs and data are permanently stored on storage devicesand are moved to memory when the computer actually uses them.There are three main types of storage devices:Disk drives (hard disksand floppy disks), CD drives (CD-R and CD-RW), and Tape drives.
CPU
e.g., Disk, CD,and Tape
InputDevices
e.g., Keyboard,Mouse
e.g., Monitor,Printer
CommunicationDevices
e.g., Modem,and NIC
StorageDevices Memory
OutputDevices
Bus
-
8/13/2019 DDC2133 c++ Topic1
8/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 8
Output Devices: MonitorThe monitor displays information (text and graphics). The resolutionand dot pitch determine the quality of the display.
CPU
e.g., Disk, CD,and Tape
InputDevices
e.g., Keyboard,Mouse
e.g., Monitor,Printer
CommunicationDevices
e.g., Modem,and NIC
StorageDevices Memory
OutputDevices
Bus
-
8/13/2019 DDC2133 c++ Topic1
9/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 9
Monitor Resolution and Dot PitchThe resolution specifies the number of pixels per squareinch. Pixels (short for picture elements) are tiny dots thatform an image on the screen. The resolution can be setmanually. The higher the resolution, the sharper and clearerthe image is. However, the image may be very small if you
set high resolution on a small screen monitor. PC monitorsare usually 15-inch, 17-inch, 19-inch, or 21-inch. For a 15-inch monitor, a comfortable resolution setting would be640 480 (307,200 pixels).
resolution
The dot pitch is the amount of space between pixels. Thesmaller the dot pitch, the better the display.
dot pitch
-
8/13/2019 DDC2133 c++ Topic1
10/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 10
Communication DevicesA regular modem uses a phone line and can transfer data in a speed up to
56,000 bps (bits per second). A DSL (digital subscriber line) also uses a phone line and can transfer data in a speed 20 times faster than a regularmodem. A cable modem uses the TV cable line maintained by the cablecompany. A cable modem is as fast as a DSL. Network interface card( NIC ) is a device to connect a computer to a local area network (LAN).
The LAN is commonly used in business, universities, and governmentorganizations. A typical type of NIC, called 10BaseT , can transfer data at10 mbps (million bits per second).
CPU
e.g., Disk, CD,and Tape
InputDevices
e.g., Keyboard,Mouse
e.g., Monitor,Printer
CommunicationDevices
e.g., Modem,and NIC
StorageDevices Memory
OutputDevices
Bus
-
8/13/2019 DDC2133 c++ Topic1
11/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 11
Programs
Computer programs , known as software , are instructions tothe computer.
You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers donot understand human languages, so you need to usecomputer languages to communicate with them.
Programs are written using programming languages.
-
8/13/2019 DDC2133 c++ Topic1
12/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 12
Programming LanguagesMachine Language Assembly Language High-Level Language
Machine language is a set of primitive instructions built into every computer. The instructions are inthe form of binary code, so you have to enter binary
codes for various instructions. Program with nativemachine language is a tedious process. Moreoverthe programs are highly difficult to read andmodify. For example, to add two numbers, youmight write an instruction in binary like this:
1101101010011010
-
8/13/2019 DDC2133 c++ Topic1
13/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 13
Programming LanguagesMachine Language Assembly Language High-Level Language
Assembly languages were developed to make programmingeasy. Since the computer cannot understand assemblylanguage, however, a program called assembler is used toconvert assembly language programs into machine code.For example, to add two numbers, you might write aninstruction in assembly code like this:
ADDF3 R1, R2, R3
ADDF3 R1, R2, R3
Assembly Source File
Assembler 1101101010011010
Machine Code File
-
8/13/2019 DDC2133 c++ Topic1
14/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 14
Programming LanguagesMachine Language Assembly Language High-Level Language
The high-level languages are English-like and easy to learnand program. For example, the following is a high-levellanguage statement that computes the area of a circle with
radius 5:area = 5 * 5 * 3.1415;
-
8/13/2019 DDC2133 c++ Topic1
15/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 15
Popular High-Level LanguagesCOBOL (COmmon Business Oriented Language)FORTRAN (FORmula TRANslation)BASIC (Beginner All-purpose Symbolic Instructional Code)Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)C (whose developer designed B first) Visual Basic (Basic-like visual language developed by Microsoft)Delphi (Pascal-like visual language developed by Borland)C++ (an object-oriented language, based on C)Java (a popular object-oriented language, similar to C++)C# (a Java-like developed my Microsoft)
-
8/13/2019 DDC2133 c++ Topic1
16/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 16
Compiling Source CodeA program written in a high-level language is called asource program . Since a computer cannot understand asource program. Program called a compiler is used totranslate the source program into a machine language
program called an object program . The object program isoften then linked with other supporting library code beforethe object can be executed on the machine.
CompilerSource File Object File Linker Excutable File
-
8/13/2019 DDC2133 c++ Topic1
17/31
-
8/13/2019 DDC2133 c++ Topic1
18/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 18
Number Systems NOTE: You can skip this section and use it as reference when you
have questions regarding binary and hexadecimal numbers.
0, 1
0, 1, 2, 3, 4, 5, 6, 7
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
binary
octal
decimal
hexdecimal
-
8/13/2019 DDC2133 c++ Topic1
19/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 19
Number SystemsComputers use binary numbers internally because storage deviceslike memory and disk are made to store 0s and 1s. A number or atext inside a computer is stored as a sequence of 0s and 1s. Each 0and 1 is called a bit , short for binary digi t . The binary numbersystem has two digits, 0 and 1.
Binary numbers are not intuitive, since we use decimal numbers inour daily life. When you write a number like 20 in a program, it isassumed to be a decimal number. Internally, computer software isused to convert decimal numbers into binary numbers, and viceversa.
-
8/13/2019 DDC2133 c++ Topic1
20/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 20
Number Systems, cont.The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8,
and 9. A decimal number is represented using a sequence of one ormore of these digits. The value that each digit in the sequencerepresents depends on its position. A position in a sequence has avalue that is an integral power of 10. For example, the digits 7, 4, 2,and 3 in decimal number 7423 represent 7000, 400, 20, and 3,respectively, as shown below:
The decimal number system has ten digits and the position values
are integral powers of 10. We say that 10 is the base or radix of thedecimal number system. Similarly, the base of the binary numbersystem is 2 since the binary number system has two digits and the
base of the hex number system is 16 since the hex number systemhas sixteen digits.
103
7 4 2 3
102 10 1 10 0
0123 103102104107
3204007000 7423
-
8/13/2019 DDC2133 c++ Topic1
21/31
-
8/13/2019 DDC2133 c++ Topic1
22/31
-
8/13/2019 DDC2133 c++ Topic1
23/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 23
Decimals => Binary To convert a decimal number d to a binary number is to find the
binary digits.. such that
These numbers can be found by successively dividing d by 2 until the quotient
is 0. The remainders are
For example, the decimal number 123 is 1111011 in binary. The conversion isconducted as follows:
01221 ,,,...,,, bbbbbb nnn
01221 222...222 01221 bbbbbbd nnn nnn
01221 ,,,...,,, bbbbbb nnn
1232
61
122
1
b 0
612
30
60 1
b 1
302
15
30
0
b 2
152
7
14 1
b 3
Remainder
Quotient
72
3
6
1
b 4
32
1
2 1
b 5
12
0
0
1
b 6
-
8/13/2019 DDC2133 c++ Topic1
24/31
-
8/13/2019 DDC2133 c++ Topic1
25/31
-
8/13/2019 DDC2133 c++ Topic1
26/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 26
Decimals => Hexadecimal
To convert a decimal number d to a hexadecimal number is to findthe hexadecimal digits hn, hn-1, hn-2, ... such that
01221 161616...161616 01221 hhhhhhd nnn nnn
These numbers can be found bysuccessively dividing d by 16 until thequotient is 0. The remainders are
For example, the decimal number 123 is7B in hexadecimal. The conversion isconducted as follows:
01221 ,,,...,,, hhhhhh nnn
nnn hhhhhh ,,,...,,, 12210
12316
7
112
11
h 0
716
0
0 7
h 1
Remainder
Quotient
-
8/13/2019 DDC2133 c++ Topic1
27/31
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.013225445X 27
Hexadecimal Binary
0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 81001 9 91010 A 101011 B 111100 C 121101 D 131110 E 141111 F 15
Binary Hex DecimalTo convert a hexadecimal number to a binary
number, simply convert each digit in thehexadecimal number into a four-digit binarynumber.
To convert a binary number to a hexadecimal,
convert every four binary digits from right to leftin the binary number into a hexadecimal number.For example,
1 1 1 0 0 0 1 1 0 1
D83
-
8/13/2019 DDC2133 c++ Topic1
28/31
-
8/13/2019 DDC2133 c++ Topic1
29/31
-
8/13/2019 DDC2133 c++ Topic1
30/31
-
8/13/2019 DDC2133 c++ Topic1
31/31
Liang Introduction to C++ Programming (c) 2007 Pearson Education Inc All rights reserved
Creating,Compiling, and
RunningPrograms Source Code
Create/Modify Source Code
Compiler
Executable Code
Run Executable Codee.g., Welcome
Result
If compilation errors
If runtime errors or incorrect result
#include
int main(){
// Display Welcome to C++ to the consolestd::cout