Unit 2 T 4 Introduction To C++
Unit 2 T 4 Introduction To C++
Introduction to Algorithms
1. Complexities and Flowchart.
2. Introduction to Programming, Categories of Programming Languages, ,
Programming Paradigms
3. Procedure Oriented Programming VS object oriented Programming.
4. Characteristics or Concepts of OOP.
5. Introduction to C++: Character Set, Tokens, Program Structure, Data
Types, Variables, Operators, Expressions, Statements and control structures,
I/O operations, Array, Functions,
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Introduction to C++:
C++ history
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of
AT&T (American Telephone & Telegraph), located in U.S.A.
Bjarne Stroustrup is known as the founder of C++ language.
It was develop for adding a feature of OOP (Object Oriented Programming) in C without
significantly changing the C component.
C++ programming is "relative" (called a superset) of C, it means any valid C program is also a valid
C++ program.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
C++ is a middle-level language it the advantage of programming low-level (drivers, kernels) and even
higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C
and C++ are the same.
Some of the features & key-points to note about the programming language are as follows:
Simple: It is a simple language in the sense that programs can be broken down into logical units and parts,
has a rich library support and a variety of data-types.
Machine Independent but Platform Dependent: A C++ executable is platform-dependent (compiled
programs on Linux won’t run on Windows), however they are machine independent.
Mid-level language: It is a mid-level language as we can do both systems-programming (drivers,
kernels, networking etc.) and build large-scale user applications (Media Players, Photoshop, Game
Engines etc.)
Structured programming language: C++ is a structured programming language in the sense that we can
break the program into parts using functions. So, it is easy to understand and modify.
Rich library support: Has a rich library support (Both standard ~ built-in data structures, algorithms
etc.) as well 3rd party libraries (e.g. Boost libraries) for fast and rapid development.
Pointer and direct Memory-Access: C++ provides pointer support which aids users to directly
manipulate storage address. This helps in doing low-level programming (where one might need to have
explicit control on the storage of variables).
Memory Management: It supports the feature of dynamic memory allocation. In C++ language, we can
free the allocated memory at any time by calling the free () function.
Speed
The compilation and execution time of C++ language is fast.
Pointer
C++ provides the feature of pointers. We can directly interact with the memory by using the pointers. We
can use pointers for memory, structures, functions, array etc.
Recursion
In C++, we can call the function within the function. It provides code reusability for every function.
Extensible
C++ language is extensible because it can easily adopt new features.
Object-Oriented: One of the strongest points of the language which sets it apart from C. Object-Oriented
support helps C++ to make maintainable and extensible programs. i.e. Large-scale applications can be
built.
Compiled Language: C++ is a compiled language, contributing to its speed.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Applications of C++:
C++ finds varied usage in applications such as:
Operating Systems & Systems Programming. e.g. Linux-based OS (Ubuntu etc.)
Browsers (Chrome & Firefox)
Graphics & Game engines (Photoshop, Blender, Unreal-Engine)
Database Engines (MySQL, MongoDB, Redis etc.)
Cloud/Distributed Systems.
There are many compilers available for C++. You need to download any one. Here, we are going to
use Turbo C++. It will work for both C and C++. To install the Turbo C++ software, you need to follow
following steps.
C++ Program
Before starting the abcd of C++ language, you need to learn how to write, compile and run the first C++
program.
To write the first C++ program, open the C++ console and write the following code:
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout << "Welcome to C++ Programming.";
getch();
}
#include<iostream.h> : includes the standard input output library functions. It
provides cin and cout methods for reading from input and writing to output respectively.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
#include <conio.h> includes the console input output library functions. The getch() function is defined in
conio.h file.
void main() The main() function is the entry point of every program in C++ language. The void keyword
specifies that it returns no value.
cout << "Welcome to C++ Programming." is used to print the data "Welcome to C++
Programming." on the console.
getch() The getch() function asks for a single character. Until you press any key, it blocks (holds) the
screen.
1. Character Set: Character set is the combination of English language (Alphabets and White
spaces) and math‟s symbols (Digits and Special symbols). Character Set means that the characters and
symbols that a C++ Program can understand and accept. These are grouped to form the commands,
expressions, words, c-statements and other tokens for C++ Language.
Character Set is the combination of alphabets or characters, digits, special symbols and white spaces
same as learning English is to first learns the alphabets, then learn to combine these alphabets to form
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
words, which in turn are combined to form sentences and sentences are combined to form
paragraphs. More about a C++ program we can say that it is a sequence of characters. These
characters from the character set play the different role in different way in the C++ compiler. The
special characters are listed in Table
In addition to these characters, C++ also uses a combination of characters to represent special
conditions. For example. Character combinations such as „\nt, „\b‟ and „\t‟ are used to represent
newline, backspace and horizontal tab respectively.
When we have to learn English language we start from beginning as given below:
There are mainly four categories of the character
1. Alphabets
Alphabets are represented by A-Z or a-z. C/C++- Language is case sensitive so it takes different
meaning for small and upper case letters. By using this character set C/C++ statements and character
constants can be written very easily. There are total 26 letters used in programming.
2. Digits
Digits are represented by 0-9 or by combination of these digits. By using the digits numeric constant
can be written easily. Also numeric data can be assigned to the C/C++-tokens. There are total 10 digits
used in the C/C++-programming.
3. Special Symbols
All the keyboard keys except alphabet, digits and white spaces are the special symbols. These are
some punctuation marks and some special symbols used for special purpose.
There are total 30 special symbols used in the C/C++-programming. Special symbols are used for
C/C++-statements like to create an arithmetic statement +, -, * etc. to create relational statement <,
>, <=, >=, == etc. , to create assignment statement =, to create logical statement && etc. are
required.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
4. White Spaces
White spaces has blank space, new line return, Horizontal tab space, carriage ctrl, Form feed etc. are
all used for special purpose. Also note that Turbo-C/C++ Compiler always ignores these white space
characters in both high level and low level programming.
2. Tokens: As we know that Software is a Program. And a Program is that which Contains set of
instructions, and an Instruction contains Some Tokens. So Tokens are used for Writing the
Programs the various Types of Tokens those are contained by the Programs.
As in the English language, in a paragraph all the words, punctuation mark and the blank spaces
are called Tokens. Similarly in a C++ program all the C++ statements having Keywords, Identifiers,
Constants, Strings, Operators and the Special Symbols are called C++ Tokens. C++ Tokens are the
essential part of a C++ compiler and so are very useful in the C++ programming. A Token is an
individual entity of a C++ program.
For example, some C++ Tokens used in a C++ program are:
Reserve: words long, do if, else etc.
Identifiers: Pay, salary etc.
Constant: 470.6,16,49 etc.
Strings: “Dinesh”, “2013-01” etc.
Operator: +, *, <, >=, &&,11, etc
Special symbols: 0, {}, #, @, %, etc.
3. Program Structure: The overall form of a program, with particular emphasis on the individual
components of the program and the interrelationships between these components. Programs are
frequently referred to as either well structured or poorly structured. With a well-structured program
the division into components follows some recognized principle such as information hiding, and the
interfaces between components are explicit and simple. By contrast, with a poorly structured
program the division into components is largely arbitrary (or even nonexistent), and interfaces are
implicit and complex.
4. Data Types: A data type specifies the type of data that a variable can store such as integer, floating,
character etc.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
An enumeration is a data type that consists of a set of named values that represent integral
constants, known as enumeration constants.
Let's see the basic data types. It size is given according to 32 bit OS.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
5. Variables: A variable is a name of memory location. It is used to store data. Its value can be
changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
type variable_list;
The example of declaring variable is given below:
int x;
float y;
char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
int x=5,b=10; //declaring 2 variable of integer type
float f=30.8;
char c='A';
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
6. Operators:
An operator is simply a symbol that is used to perform operations. There can be many types of operations like
arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operator
Unary operator
Ternary or Conditional Operator
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
7. Expressions:
C++ expression consists of operators, constants, and variables which are arranged according to the
rules of the language. It can also contain function calls which return values. An expression can
consist of one or more operands, zero or more operators to compute a value. Every expression
produces some value which is assigned to the variable with the help of an assignment operator.
Examples of C++ expression:
1. (a+b) - c
2. (x/y) -z
3. 4a2 - 5b +c
4. (a+b) * (x+y)
An expression can be of following
types: Logical expressions
Constant expressions
Integral expressions
Float expressions
Pointer expressions
Relational expressions
Bitwise expressions
Special assignment expressions
Logical expressions
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
3) Selection statements; Selection statements choose between one of several flows of control.
1) if statement;
2) if statement with an else clause;
3) switch statement.
1) break statement;
2) continue statement;
3) return statement with an optional expression;
4) return statement using list initialization;
5) goto statement.
Control structures
Control Structures are just a way to specify flow of control in programs. Any algorithm or program
can be clearer and understood if they use self-contained modules called as logic or control
structures. It basically analyzes and chooses in which direction a program flows based on certain
parameters or conditions.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Sequential: default mode. Sequential execution of code statements (one line after another) -- like following
a recipe
Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In C++, these
are the types of selection statements:
if
if/else
switch
Repetition: used for looping, i.e. repeating a piece of code multiple times in a row. In C++, there are
three types of loops:
while
do/while
for
The function construct, itself, forms another way to affect flow of control through a whole program.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
3. A switch statement is often convenient for occasions in which there are multiple cases to
choose from.
The syntax format is:
switch (expression)
{
case constant:
statements
case constant:
statements
9. I/O operations,
What is input?
Information given to the program, is called input
In input data is flow inside the program
It required a source(for reading )
stadin - - > standard input used as scanf function.
stdout ---> standard input used as printf function taken by monitor(gives destination)
What is output?
Information given by the program is called output
In output data is flow outside the program
In c++ I/O operations are alone using streams
A stream is class is provide set of functions to perform input and output operations
A stream represents input source (reading) and output destination (writing)
C++ provides 2 stream classes
1. ostream
2. istream
Istream The istream consists of input functions to read streams of characters from the keyboard.
Ostream The ostream consists of output functions to write a character onto the screen.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
10. Array :
Like other programming languages, array in C++ is a group of similar types of elements that have
contiguous memory location.
In C++ std::array is a container that encapsulates fixed size arrays. In C++, array index starts from 0.
We can store only fixed set of elements in C++ array.
Random Access
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Arrays can be single or multidimensional. The number of subscript or index determines the
dimensions of the array. An array of one dimension is known as a one-dimensional array or 1-D
array, while an array of two dimensions is known as a two-dimensional array or 2-D array.
One-dimensional array
Conceptually you can think of a one-dimensional array as a row, where elements are stored one after
another.
1 int num[100];
2 float temp[20];
3 char ch[50];
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
num is an array of type int, which can only store 100 elements of type int.
temp is an array of type float, which can only store 20 elements of type float.
ch is an array of type char, which can only store 50 elements of type char.
Two-dimensional Array
The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare and
access elements of a 2-D array we use 2 subscripts instead of 1.
Syntax: datatype array_name[ROW][COL];
The total number of elements in a 2-D array is ROW*COL.
Let’s take an example.
int arr[2][3];
This array can store 2*3=6 elements. You can visualize this 2-D array as a matrix of 2 rows and 3
columns.
The individual elements of the above array can be accessed by using two subscript instead of one. The
first subscript denotes row number and second denotes column number. As we can see in the above
image both rows and columns are indexed from 0. So the first element of this array is at arr[0][0] and
the last element is at arr[1][2].
Here is how you can access all the other elements:
arr[0][0] - refers to the first element
arr[0][1] - refers to the second element
arr[0][2] - refers to the third element
arr[1][0] - refers to the fourth element
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
11. Functions
The function in C++ language is also known as procedure or subroutine in other programming
languages.
To perform any task, we can create function. A function can be called many times. It provides
modularity and code reusability
A function is a block of code which only runs when it is called. You can pass data, known as
parameters, into a function. Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
Create a Function
C++ provides some pre-defined functions, such as main(), which is used to execute code. But you can also
create your own functions to perform certain actions.
To create (often referred to as declare) a function, specify the name of the function, followed by
parentheses ():
Syntax
void myFunction() {
// code to be executed
}
Example Explained
myFunction() is the name of the function
void means that the function does not have a return value.
inside the function (the body), add code that defines what the function should do.
Advantage of functions
There are many advantages of functions.
1) Code Reusability
By creating functions in C++, you can call it many times. So we don't need to write the same code again
and again.
2) Code optimization
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
It makes the code optimized; we don't need to write much code. Suppose, you have to check 3 numbers (531,
883 and 781) whether it is prime number or not. Without using function, you need to write the prime
number logic 3 times. So, there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it several times.
Types of Functions
There are two types of functions in C++ programming:
1. Library Functions: are the functions which are declared in the C++ header files such as ceil(x), cos(x),
exp(x), etc.
2. User-defined functions: are the functions which are created by the C++ programmer, so that he/she can
use it many times. It reduces complexity of a big program and optimizes the code.
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering
Adina Institute Of Science And Technology Sagar Subject: - Basic Computer Engineering Code: - BT-205
Prepared By:- Mr. Anurag Jain Assit. Prof. Department of Computer Science & Engineering