Programming Fundamentals
CSC102
Lecture 3: Introduction to C++ Programming
1
What is C/C++?
A language written by Brian Kernighan and
Dennis Ritchie. This was to be the language
that UNIX was written in to become the first
"portable" language
In 1972 Dennis Ritchie at Bell Labs writes C and in
1978 the publication of The C Programming
Language by Kernighan & Ritchie caused a
revolution in the computing world
C++ was developed by Bjarne Stroustrup at Bell Labs
since 1979, as an extension of the C language
In recent years C/C++ has been used as a general-purpose
language because of its popularity with programmers.
2
Why use C/C++?
Mainlybecause it produces code that runs nearly as fast as
code written in assembly language. Some examples of the use
of C might be:
◦ Operating Systems
◦ Language Compilers
◦ Device Drivers
◦ Database
◦ Application Programs
◦ Utilities
3
C++ Compilation Process
4
Development with C++
Six stages
Editing: Writing the source code by using some IDE
or editor
Preprocessing or libraries: Already available
routines
compiling: translates or converts source to object
code for a specific platform source code -> object
code
linking: resolves external references and produces
the executable module
loading: loads program for execution
execution: execution or running of program
5
Example Program: A Program to
find sum of two numbers
Requirements Specification: Develop a program
that does the following:
1. Prints on the monitor screen a brief description of the
program’s purpose.
2. Prompts the user to enter two numbers using the
terminal keyboard.
3. Reads the values for two numbers
4. Computes the sum of two numbers
5. Prints the result, i.e. sum of two numbers.
6
Analysis
Input: Two decimal numbers (a, b)
Output: Sum of two numbers (sum)
Formula: The sum is calculated using
the formula:
sum = a + b
7
Program Design
Step 1: print “A Program that finds sum of
two numbers”
Step 2: print “Enter first number:”
Step 3: read the value of a
Step 4: print “Enter second number:”
Step 5: read the value of b
Step 6: compute sum = a + b
Step 7: print the value of sum
8
Source Code
9
Execution - Output
10
Algorithm
An algorithm is defined as a well-defined
sequence of steps that provides a solution
for a given problem.
Algorithms are generally written in a
natural language or plain English
language
11
Simple C++ Program
12
Simple C++ Program
Line 1: // A first C++ Program
The // denotes the comments
Comment is an explanation or description of the source
code of the program.
Comments are ignored by the compiler
13
Simple C++ Program
Line 2: #include <iostream>
As part of compilation, the C++ compiler runs a
program called the C++ preprocessor. The
preprocessor is able to add and remove code from your
source file.
In this case, the directive #include tells the
preprocessor to include code from the file iostream
This file contains declarations for functions that the
program needs to use. A declaration for the cout / cin
function is in this file.
14
Simple C++ Program
Line 3: using namespace std;
The keyword using introduces a name into the current
declarative region (such as a block), thus avoiding the
need to qualify the name.
A namespace is used as additional information to
differentiate similar functions, classes, variables etc.
with the same name available in different libraries.
All the entities (variables, types, constants, and
functions) of the standard C++ library are declared
within the std namespace.
15
Simple C++ Program
Line 5: int main()
This statement declares the main function.
C++ program can contain many functions but must
always have one main function.
A function is a self-contained module of code that can
accomplish some task.
Functions are examined later.
The “int" specifies the return type of main. In this case,
integer value is returned to the operating system.
16
Simple C++ Program
Line 6: {
This opening bracket denotes the start of
the program.
17
Simple C++ Program
Line 7: cout<<“Welcome to FURC\n";
cout is a function from a standard C++ library that is
used to print/display strings to the standard output,
normally your screen.
The compiler links code from these standard libraries to
the code you have written to produce the final
executable.
18
Simple C++ Program
Line 8: }
This closing bracket denotes the end of
the program.
19
Escape Sequence
‘\’is an escape character: causes an escape from the
normal interpretation of string, the next character
recognized as having a special meaning.
\n new line
\t tab
\b backspace
\r carriage return
\’ single quote
\” double quote
\\ backslash
20