Lecture 07: C++ CS 101: Introduction to Computing
C++
Shahab Haider
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction2to Computing
History of C and C++
• C++ evolved from C
– C evolved from two other programming languages, BCPL(Basic
Combined Programming Language) and B
• ANSI C
– Established worldwide standards for C programming
• C++
– Provides capabilities for object-oriented programming
• Objects are reusable software components that model
things in the real world
• Object-oriented programs are easy to understand, correct
and modify
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction3to Computing
C++ Standard Library
• C++ programs
– Built from pieces called classes and functions
• C++ standard library
– Provides rich collections of existing classes and functions for all
programmers to use
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction4to Computing
Other High-level Languages
• Other high-level languages
– FORTRAN
• Used in scientific and engineering applications
– COBOL
• Used to manipulate large amounts of data
– Pascal
• Used to teach structured programming
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction5to Computing
Structured Programming
• Structured programming
– Disciplined approach to writing programs
– Clear, easy to test and debug, and easy to modify
• Multitasking
– Many activities to run in parallel
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction6to Computing
Basics of a Typical C++ Environment Program is created in
Editor Disk the editor and stored
Phases of C++ Programs: on disk.
1. Edit Preprocessor Disk
Preprocessor program
processes the code.
2. Preprocess Compiler Disk
Compiler creates
object code and stores
it on disk.
3. Compile Linker links the object
code with the libraries,
Linker
4. Link
Disk creates a.out and
stores it on disk
Primary
5. Load Loader
Memory
6. Execute Loader puts program
in memory.
Disk ..
..
..
Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
.. executes.
..
..
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction7to Computing
Introduction to C++ Programming
• C++ language
– Facilitates a structured and disciplined approach to computer program
design
• Following are several examples
– The examples illustrate many important features of C++
– Each example is analyzed one statement at a time.
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++ Comments
Written between /* and */ or following a //.
3 #include <iostream>
Improve program readability and do not cause
4 the computer to perform any action.
5 int main()
6 { preprocessor directive
7 std::cout << "Welcome to Message C++!\n"; to the C++ preprocessor.
Lines beginning with # are preprocessor
8 directives.
9 return 0; // indicate that program #include <iostream> tells the preprocessor
ended successfully
to include the contents of the file <iostream>,
10} C++
whichprograms
includescontain one oroperations
input/output more functions,
(such as
one of which must be
printing to the screen). main
Parenthesis are used to indicate a function
Welcome to C++! int means that main "returns" an integer value.
Prints the string of characters contained
More in Chapter 3. between
the quotation marks.
return is a way to exit a function
from a function. A leftstd::cout,
brace { begins
The entire line, including thethe
<< body of every
return 0, in this case, means function andto a right brace and
} ends it.
operator, the string "Welcome C++!\n"
that the program terminated
the semicolon (;), is called a statement.
normally.
All statements must end with a semicolon.
Lecture 07: C++ CS 101: Introduction9to Computing
A Simple Program:
Printing a Line of Text
• std::cout
– Standard output stream object
– “Connected” to the screen
– std:: specifies the "namespace" which cout belongs to
• std:: can be removed through the use of using
statements
• <<
– Stream insertion operator
– Value to the right of the operator (right operand) inserted into output
stream (which is connected to the screen)
– std::cout << “Welcome to C++!\n”;
• \
– Escape character
– Indicates that a “special” character is to be output
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction10
to Computing
Simple Program:
Printing a Line of Text
Escape Sequence Description
\n Newline. Position the screen cursor to the
beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.
• There are multiple ways to print text
– Following are more examples
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 07: C++ CS 101: Introduction to Computing
References
Dietal and Dietal : How to Program C++
3rd Edition
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi