Unit 2 C++ Programming Basics
Unit 2 C++ Programming Basics
Introduction to C++
Danish computer scientist Bjarne Stroustrup began developing C++ at Bell Laboratories in
1979. It was named C++ in 1983. The first book of C++ programming language was published
in 1985 and it was commercially available.
1. C++ is one of the most popular programming language. It is used all around the
world for developing applications, browsers, video games, etc. Most of the operating
systems including Windows, Mac OS and Linux are built in C++.
2. It is a superset of C. The main difference between C and C++ is that C++ supports
object-oriented programming.
3. It is an intermediate level language, meaning it has features of both high level
language and low-level language for programming of computer hardware.
4. It is easy to learn and use. It is close to C# and Java.
Prior to IDEs, programmers wrote their programs using text editors. They wrote and saved
an application in the text editor before running the compiler. If errors were found, they had
to go back to the text editor to revise the code.
Translators
A translator is a program that converts source code into machine code. A program written in
a high level language or assembly language is called source code. There are three types of
translators; compilers, interpreters and assemblers.
#include <iostream>
using namespace std;
int main()
{
int a, b, sum;
cout<<”This is my first C++ program.\n”;
cout<<”Enter the value of a:”;
cin>>a;
cout<<”Enter the value of b:”;
cin>>b;
sum=a+b;
cout<<”Sum of 2 numbers=”<<sum<<endl;
return 0;
}
This program has only one function called main(). The parentheses follow the function
name. Without the parenthesis the compiler would think that it is a variable name. A
program may consist of many functions but on startup, the control always goes to main().
All the programs must have this function.
The word int before the function name indicates that this function has a return value of type
int. This and return statement will be discussed later.
The body of a function is surrounded by braces (curly brackets). In this program there are 9
statements in the function body. All of these end with semicolon (;)
Input/Output Statements
The cout Statement
It is used to output text or values on the screen. The << is called insertion or put to
operator.
The ‘\n’ character at the end of the string constant is an example of escape sequence. It
causes the next output to be displayed on a new line. The endl manipulator used at the end
of the last output statement has the same effect as ‘\n’ escape sequence.
The following program demonstrates the use of ‘\n’ and endl manipulator.
#include<iostream>
using namespace std;
int main()
{
cout<<”\nI am a student.”<<endl;
cout<<”I live in Islamabad.”;
}
The output of the program will be:
I am a student.
I live in Islamabad.
The following single statements can also be used to get the same output.
cout<<”\nI am a student.”<<endl<<”I live in Islamabad.”;
cout<<”\nI am a student.\nI live in Islamabad.”;
cout<<”\nI am a student.”<<”\n”<<”I live in Islamabad”;
The cin Statement
It is used to input data from the keyboard and assign it to one or more variables. The >> is
called extraction or get from operator.
Syntax
cin>>variable;
This is demonstrated with the following statement in the program.
cin>>a;
To assign three values to variables a, b and c. The statement would be:
cin>>a>>b>>c;
The data should be separated with space while entering from the keyboard.
Comments
Comments are explanatory statements that help the reader in understanding the source
code. Comments can be entered at any location in the program. Comments are ignored
during program execution. There are two types of comments in C++. These are single-line
comments and multiple-line comments.
Single-line comments (//)
The single-line comments start with // and continue till the end of the line as demonstrated
in the following program.
// This program displays a message on the screen.
// It displays the message “Hello World” on the screen.
#include <iostream>
using namespace std;
int main()
{
cout<<”Hello World!”; // It prints a message on the screen.
}
Multiple-line Comments (/* and */)
It is used for entering multiple line comments in a program. The /* is used at the beginning
of the comments and */ at the end.
Constants
In computer programming, a constant is a value that does not change during program
execution. It can be a number, a character or a character string. Some examples of
constants are 17, 8.25, ‘b’ and “Information Technology”. A single character constant is
written within single quotes and a string constant within double quotes.
Variables
A variable is a name of memory location where computer stores values of different data
types. The data stored in a variable may change during program execution.
Rules for Specifying Variable Names
i. Must start with an alphabet or underscore (_)
ii. The following characters are allowed in a variable name
a. Upper-case letters (A to Z)
b. Lower-case letters (a to z)
c. Digits (0 to 9)
d. Underscore
e. Upper-case letter is different from a lower-case letter. For example, the
variable avg is different from AVG and Avg.
iii. Blank space, comma or special symbols such as &, %, $, etc. are not allowed.
iv. Reserved word such as do, while, if, then, etc. have special meaning in C++.
Therefore, these cannot be used as variable names.
The data types used in C++ are integer, floating-point and character.
Integer
It is a data type used to defines numeric variables to store whole numbers such as 233, -15,
3890, etc. Numbers having fractional part, such as, 2.67 cannot be stored in an integer
variable.
The following table shows the integer types, the number of bytes in takes in memory to
store the value and the range of numbers it can store. It is for 32-bit word.
The statement:
int a, b, c=10, sum;
declares three variables a, b, c and sum as integer variables and initializes the value of c to
10.
Floating-point
It is a data type used to define variables that can store numbers that have fractional part
such as 4.38, 23.2, -7.99, etc. Floating-point numbers are also called real numbers.
The following table shows the floating-point types, the number of bytes it takes in memory
to store the value and the range of real numbers it can store.
The statement:
float x, y, weight=63.28;
declares three variables x, y and weight as floating-point variables and initializes the variable
weight to 63.28.
It is a data type used to define variables that can store only a single character such as ‘a’,
‘&’, ‘%’, etc. Characters are written within single quotation marks. One byte of memory is
set aside in memory to store a single character.
The statement:
char ch1, ch2=’a’;
declares two variables ch1 and ch2 and initializes ch2 to ‘a’.
Escape Sequences
An escape sequences are used inside the cout statement. It begins with a backslash followed
by a character such as ‘\n’, ‘\t’, ‘\r’, etc. Escape sequence are special characters used to
control the output look on screen or printer.
Commonly used escape sequences are \a, \n, \t, \r, \\, \’ \” and \f.
Arithmetic Operators
The arithmetic operators +, -, * and / for addition, subtraction, multiplication and division
work on both integers and floating-point. They are used much the same way as in algebra.
The Remainder Operator (%)
It works only with integers. The remainder operator is represented by the percent symbol
(%). It finds the remainder when one number is divided by another.
6 % 8 will return 6
7 % 8 will return 7
8 % 8 will return 0
9 % 8 will return 1
10 % 8 will return 2
19 % 5 will return 4