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

C++ Programming: Namiq Sultan

This document is a chapter from a book on C++ programming by Namiq Sultan from the University of Duhok. It provides an introduction to computers and programming. It discusses why we program, computer hardware and software components, what a program is made of including variables, the programming process, and high-level programming languages like C++. It includes examples of flowcharts, pseudocode, and a simple C++ program to calculate pay.

Uploaded by

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

C++ Programming: Namiq Sultan

This document is a chapter from a book on C++ programming by Namiq Sultan from the University of Duhok. It provides an introduction to computers and programming. It discusses why we program, computer hardware and software components, what a program is made of including variables, the programming process, and high-level programming languages like C++. It includes examples of flowcharts, pseudocode, and a simple C++ program to calculate pay.

Uploaded by

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

C++ Programming

Namiq Sultan
University of Duhok
Department of Electrical and Computer Engineering
www.sites.google.com/site/namiqsultan

Reference: Starting Out with C++, Tony Gaddis, 2nd Ed.

2011-2012
1
Chapter 1. Introduction to Computers and
Programming

1.1 Why Program?


1.2 Computer Systems: Hardware and Software
1.3 Programs and Programming Languages
1.4 What is a Program Made of?
1.5 Input, Processing, and Output
1.6 The Programming Process
1.7 Procedural and Object-Oriented Programming

C++ Programming, Namiq Sultan 2


1.1 Why Program?

• Computers can do many different jobs because they are


programmable.

C++ Programming, Namiq Sultan 3


1.2 Computer Systems: Hardware and Software

• All computer systems consist of similar hardware devices


and software components. This section provides an
overview of standard computer hardware and software
organization.

C++ Programming, Namiq Sultan 4


Hardware
1. The CPU
2. Main Memory
3. Secondary Storage
4. Input Devices
5. Output Devices

C++ Programming, Namiq Sultan 5


Figure 1.1 The organization of computer system

Central Processing
Input
Unit (CPU)
Device

Output
Device

Main
Memory

Secondary
Storage
Device

C++ Programming, Namiq Sultan 6


Figure 1.2 Internal organization of the CPU

Instruction
(Input) Result
Arithmetic and (Output)
Logic Unit

Control
Unit

C++ Programming, Namiq Sultan 7


Software

– Operating Systems: An operating system is a set of


programs that manages the computer’s hardware
devices and controls their processes, e.g. DOS,
Windows, UNIX.
– Application Software: Programs that solve specific
problems or perform general operations that satisfy the
needs of the user, e.g. word processors, spreadsheet,
and database packages.

C++ Programming, Namiq Sultan 8


1.3 Programs and Programming Languages

• What is a program?
– A set of instructions a computer follows in order to
perform a task.
• A programming language is a special language used to
write computer programs.

C++ Programming, Namiq Sultan 9


Program 1-1
// This program calculates the user’s pay.
#include <iostream>
using namespace std;
int main()
{
float hours, rate, pay;
cout << "How many hours did you work? ";
cin >> hours;
cout << "How much do you get per hour? ";
cin >> rate;
pay = hours * rate;
cout << "You have earned $" << pay << endl;
return 0;
}
C++ Programming, Namiq Sultan 10
Program Output

How many hours did you work? 10


How much do you get paid per hour? 15

You have earned $150

C++ Programming, Namiq Sultan 11


Before Programming
• While planning a program, the programmer uses one or
more design tools to create a model of the program.
– flowcharts
– pseudocode

C++ Programming, Namiq Sultan 12


Flowchart
Terminal START

I/O Operation Display message


”How many hours did you work?”

Read hours

Display message
”How much do you get paid per
hour?”

Read rate

Pay = rate * hours


Process

Display pay

END

C++ Programming, Namiq Sultan 13


Pseudocode
1. Display “How many hours did you work?”
2. Input hours
3. Display “How much do you get paid per hour?”
4. Input rate
5. pay = rate * hours
6. Display pay

C++ Programming, Namiq Sultan 14


Programming Languages

• Figure 1-4 The concept of language levels

High level
(Close to Human Language)

Low level
(Machine Language)

C++ Programming, Namiq Sultan 15


Programming Languages

Source Code Compiler Executable


Machine Code

C++ Programming, Namiq Sultan 16


Table 1-1 Few of high-level languages
Language Description
BASIC Beginners All-purpose Symbolic Instruction Code. A general
programming language originally designed to be simple enough
for beginners to learn.
FORTRAN Formula Translator. A language designed for programming
complex mathematical algorithms.
COBOL Common Business-Oriented Language. A language designed for
business applications.
Pascal A structured, general purpose language designed primarily for
teaching programming.
C A structured, general purpose language developed at Bell Labs. C
offers both high-level and low-level features.
C++ Based on the C language, C++ offers object-oriented features not
found in C. Also invented at Bell Laboratories.
Java An object-oriented language invented at Sun Microsystems. Java
may be used to develop programs that run over the Internet, in a
web browser.

C++ Programming, Namiq Sultan 17


1.4 What is a Program Made of?

• There are certain elements that are common to all


programming languages.
– Key Words
– Programmer-Defined Symbols
– Operators
– Punctuation
– Syntax

C++ Programming, Namiq Sultan 18


Language Elements, Table 1-2
Language Description
Element
Key Words Words that have a special meaning. Key words may only be
used for their intended purpose.
Programmer- Words or names defined by the programmer. They are symbolic
Defined names that refer to variables or programming routines.
Symbols
Operators Operators perform operations on one or more operands. An
operand is usually a piece of data, like a number.
Punctuation Punctuation characters that mark the beginning or ending of a
statement, or separate items in a list.
Syntax Rules that must be followed when constructing a program.
Syntax dictates how key words and operators may be used, and
where punctuation symbols must appear.

C++ Programming, Namiq Sultan 19


Lines and Statements

cout << "How many hours did you work?";

C++ Programming, Namiq Sultan 20


Variables

• A storage location in the computer’s memory for holding a


piece of information.
• Symbolic names that represent locations in the computer’s
random-access memory.

C++ Programming, Namiq Sultan 21


Variable Declarations

• Two types of information: numbers and characters


• Numbers may be integers or floating-point numbers
• The statement below creates three variables in memory
named hours, rate, and pay that each can store a floating
point number

float hours, rate, pay;

C++ Programming, Namiq Sultan 22


1.5 Input, Processing, and Output

• Input:
cin >> hours;
• Processing:
pay = hours * rate;
• Output
cout << "You have earned $" << pay;

C++ Programming, Namiq Sultan 23


1.6 The Programming Process

• The programming process consists of several steps, which


include design, creation, testing and debugging activities.

C++ Programming, Namiq Sultan 24

You might also like