Professional Documents
Culture Documents
Introduction
Introduction
3
Procedural Programming
▫ Procedural programming is a programming
paradigm, derived from structured programming
based on the concept of the procedure call.
Procedures (a type of routine or subroutine)
simply contain a series of computational steps to
be carried out.
▫ Any given procedure might be called at any point
during a program's execution, including by other
procedures or itself.
Structure of Procedure Oriented
Programming
Relationship b/w data and function
Object Oriented Programming
8
Features of OOP
▫ 1. Emphasis is on doing rather than procedure.
▫ 2. programs are divided into what are known as objects.
▫ 3. Data structures are designed such that they characterize the
objects.
▫ 4. Functions that operate on the data of an object are tied
together in the data structure.
▫ 5. Data is hidden and can’t be accessed by external functions.
▫ 6. Objects may communicate with each other through
functions.
▫ 7. New data and functions can be easily added.
▫ 8. Follows bottom-up approach in program design.
10
Introduction
• C++ is derived from C programming language – an extension that
added object-oriented (OO) features.
- Lots of C++ syntax similar to that found in C, but there are
differences and new features.
Evolution of C++
• C (1972)
• ANSI Standard C (1989)
• Bjarne Stroustrup adds features of the language Simula (an object-
oriented language designed for carrying out simulations) to C
resulting in …
• C++ (1983)-the name of the language was changed from C with
Classes to C++.
• Many new features were added around this time, the most notable of
which are virtual functions, function overloading, references and many
more.
• In 1990, Turbo C++ was released as a commercial product. Turbo C++
added a lot of additional libraries which have had a considerable impact
on C++'s development.
12
Evolution of C++
• ANSI Standard C++ (1998)-In 1998, the C++ standards committee
published the first international standard for C++ ISO/IEC 14882:1998,
which is informally known as C++98. The Standard Template Library,
which began its conceptual development in 1979, was also included
• ANSI Standard C++ [revised] (2003)
• ANSI Standard C++ (2011)
• The present C++
– A general-purpose language that is in widespread use for
systems and embedded
– The most commonly used language for developing system
software such as databases and operating system.
13
Mary 1001
Jane 1002
name rollNo
name rollNo
Encapsulation
C supports procedural programming paradigm for code C++ supports both procedural and object oriented
development. programming paradigms;
C does not support object oriented programming; Being an object oriented programming language C++
therefore it has no support for polymorphism, supports polymorphism, encapsulation, and
encapsulation, and inheritance. inheritance.
C, being a procedural programming, it is a function While, C++, being an object oriented programming, it is
driven language. an object driven language.
26
Advantages of C++
▫ C++ is a highly portable language and is often the language
of selection for multi-device, multi-platform app
development.
▫ C++ is an object-oriented programming language and
includes concepts like classes, inheritance, polymorphism,
data abstraction, and encapsulation which allow code
reusability and makes programs very maintainable.
▫ C++ use multi-paradigm programming. The Paradigm
means the style of programming .paradigm concerned about
logics, structure, and procedure of the program. C++ is
multi-paradigm means it follows three paradigm Generic,
Imperative, Object Oriented.
▫ It is useful for the low-level programming language and very
efficient for general purpose.
Differences between C++ and Standard
C++
/* A traditional-style C++ program */
#include < iostream.h >
int main( ) {
/* program code */
return 0;
}
/*
A modern-style C++ program that uses
the new-style headers and namespace
*/
#include < iostream>
using namespace std;
int main( ) {
/* program code */
return 0;
}
Differences between C++ and Standard
C++
Old style header files Standard C++ headers
< math.h > < cmath >
< string.h > < cstring >
MY FIRST PROGRAM
#include <iostream>
using namespace std;
int main()
{
declaration(s)
statement(s)
return 0;
}
The statements specify the algorithm for the solution to your problem.
A Simple, Yet Complete, C++ Program
Ends execution
of main() which ends the
program
The cout Output Statement
• Syntax
• Problem Input
miles distance in miles
• Problem Output
kms distance in kilometers
• Algorithm will use Conversion Formula
1 mile = 1.609 kilometers
• Formulate the algorithm that solves the problem.
• Algorithm
1. Get the distance in miles.
2. Convert the distance to kilometers by multiplying by 1.609
3. Display the distance in kilometers.
Syntax
const type name = expression;
The statement must include the reserved word const, which designates
the declaration as a constant declaration.
Examples
const float TAXRATE = 0.0675;
const int NUMSTATES = 50;
▫ TOKENS:
▫ KEYWORDS:
▫ IDENTIFIERS:
Variables
• We can define a variable as a portion of memory to
store a determined value
a = 5; b = 2; a = a + 1; result = a - b;
• Each variable needs an identifier that distinguishes
it from the others.
Identifiers
A valid identifier is a sequence of one or more
letters, digits or underscore characters (_). Neither
spaces nor punctuation marks or symbols can be part
of an identifier. Only letters, digits and single
underscore characters are valid.
• Another rule that you have to consider when
inventing your own identifiers is that they
cannot match any keyword of the C++
Note: The C++ language is a "case sensitive" language. That means that an
identifier written in capital letters is not equivalent to another one with the
same name but written in small letters.
Constants
Data Types
Declaration of variables
• To use a variable in C++, we must first declare it
specifying which data type we want it to be.
Example:
int a;
float mynumber;
Initialization of variables
C++ INPUT/OUTPUT
• cout is used to output (print) values.The cout object,
together with the << operator, is used to output
values/print text.
• cin is a predefined variable that reads data from the
keyboard with the extraction operator
• EXAMPLE:
• int x;
• cout << "Type a number: "; // Type a number and press
enter
• cin >> x; // Get user input from the keyboard
• cout << "Your number is: " << x; // Display the input value
56
C++ Comments
• Single-line comments start with two forward slashes
(//).
• Multi-line comments start with /* and ends with */.
57
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
61
• Arithmetic Operators
C++ OPERATOR
• Relational Operators: These are used for
comparison of the values of two operands. For
example, checking if one operand is equal to the
other operand or not, an operand is greater than
the other operand or not etc. Some of the
relational operators are (==, >= , <= )
63
C++ OPERATOR
• Assignment Operators: Assignment operators are
used to assign value to a variable. The left side
operand of the assignment operator is a variable
and right side operand of the assignment
operator is a value.(=,+=,-=,*=,/=)
• For example:
• a = 10;
• Bitwise Operators: The Bitwise operators is used
to perform bit-level operations on the
operands.(&,|,^,<<,>>)
64
C++ OPERATOR
• Conditional Operator: Conditional operator is of
the form Expression1 ? Expression2 :
Expression3 . Here, Expression1 is the condition
to be evaluated. If the condition(Expression1) is
True then we will execute and return the result of
Expression2 otherwise if the
condition(Expression1) is false then we will
execute and return the result of Expression3. We
may replace the use of if..else statements by
conditional operators.
65
• #include<iostream>
• using namespace std;
•
• int main()
• {
• int i = 20;
•
• if (i > 10)
• {
• cout<<"10 is less than 20";
• }
•
• cout<<"I am Not in if";
• }
68
• if (condition)
• {
• // Executes this block if
• // condition is true
• }
• else
• {
• // Executes this block if
• // condition is false
• }
69
if-else ladder
• if (condition)
• statement;
• else if (condition)
• statement;
• .
• .
• else
• statement;
70
Loops in C++
• In computer programming, a loop is a sequence
of instructions that is repeated until a certain
condition is reached.
• For Loop and While Loop- test condition is
tested before entering the loop body
• Do-While Loop- test condition is tested or
evaluated at the end of loop body.
71
Loops in C++
• A for loop is a repetition control structure which
allows us to write a loop that is executed a
specific number of times. The loop enables us to
perform n number of steps together in one line.
• Syntax:
• for (initialization expr; test expr; update expr)
•{
• // body of the loop
• // statements we want to execute
•}
72
Loops in C++
• initialization expression;
• while (test_expression)
• {
• // statements
•
• update_expression;
• }
73
Loops in C++
• initialization expression;
• do
•{
• // statements
• update_expression;
• } while (test_expression);
Structure of C++ program
User-Defined DataTypes
Functions in C++
• A function is a group of statements that together
perform a task. Every C++ program has at least one
function, which is main().You can divide up your code
into separate function.
• function declaration:
• Calling a Function:
• While creating a C++ function, you give a
definition of what the function has to do. To use a
function, you will have to call or invoke that
function.
• example:
• // calling a function to get max value.
• ret = greater(a, b);
83
• OUTPUT
• Total value is :300
• Total value is :120
86
Pointers
• int *ip; // pointer to an integer
• double *dp; // pointer to a double
• float *fp; // pointer to a float
• char *ch // pointer to character
90
Pointers
• // ptr can point to an address
• // which holds int data
• int *ptr;
• int main () {
• int var = 20; // actual variable declaration.
• int *ptr; // pointer variable
Pointer to pointer
• A pointer to a pointer is a form of multiple
indirection or a chain of pointers. Normally, a
pointer contains the address of a variable. When
we define a pointer to a pointer, the first pointer
contains the address of the second pointer, which
points to the location that contains the actual
value as shown below.
• Example:
• int **var;
93
• int main () {
• int var;
• int *ptr;
• int **pptr;
• var = 3000;
• return 0;
• }
94
References in C++
• References in C++: When a variable is declared
as reference, it becomes an alternative name for
an existing variable. A variable can be declared as
reference by putting ‘&’ in the declaration.
• int i = 20;
• int& r = i;