SlideShare a Scribd company logo
Object Oriented Programming using
C++
• Topics to be covered in the first week
UNIT-I
• Principles of OOP: Software Crisis, Software
Evolution-Programming Paradigms, Object
Oriented Technology- Basic concepts and
benefits of OOP – Application of OOP, OOP
languages
Need for studying this Subject
• C++ is a programming language developed by
Bjarne Stroustrup in the year 1983.
• It overcomes the complexity in the development
of software and the developed software is easy to
maintain.
• It provides a solution to the Software Crisis
Software Crisis
• Software Crisis is a term used in computer science for
the difficulty of writing useful and efficient computer
programs in the required time.
• Software was too complex to develop.
The crisis manifested itself in several ways:
• Projects running over-budget
• Projects running over-time
• Software was very inefficient
• Software was of low quality
• Software often did not meet requirements
Characteristics of Software
• Software must be modular, easily understandable and
reusable.
• Software development cost should be minimum.
• Some may feel difficulty in coding even though they know
the solution. So Software development must be easy.
• Software should be robust as it is used in scientific and
business applications.
• The programming languages earlier to C++ could not
provide a solution to the software crisis.
Evolution of programming languages
Machine languages
Machine language, or machine code, is a low-level language comprised of
binary codes or codes formed using binary digits (ones and zeros).
Example :
01001000 01100101 01101100 01101100 01101111 00100000 01010111
01101111 01110010 01101100 01100100
Disadvantages :
• It is very difficult to program in machine language. The programmer has to
know details of hardware to write program and has to remember the
codes for various arithmetic and logical operations.
• The programmer has to remember too many codes to write a program. It
results in programming errors. It is difficult to debug the program also.
Assembly languages
As programs grew, assembly language was invented so that a
programmer could deal with larger, increasingly complex programs,
using symbolic representations of the machine instructions (ADD
R1,R2, ). User need not remember binary codes.
Disadvantages:
• It takes a lot of time and effort to write
the code for the same.
• It is very complex and difficult to
understand.
• The syntax is difficult to remember.
• It has a lack of portability of program
between different computer architectures.
Example Code:
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World!', 10,
High level languages
As programs continued to grow, high-level languages were introduced
that gave the programmer more tools to handle complexity.
• The first language was FORTRAN. It was very impressive first step,
encourages clear, easy-to-understand program. But the programming
used to be monolithic.
• From 1950 onwards structured programming (blocks) and Procedure
(module/ function) oriented programming came into existence.
Instructions in the program were divided into Blocks,
modules/functions/procedures. Coding became easy as program can
be developed in small modules. A large task is divided into sub tasks
and each task is implemented using a procedure. Examples ?
Characteristics of procedure-oriented
Programming
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs
known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to
function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.
• Disadvantages of C language/
Procedure Oriented Programming?
• I think the most common problem is that the code does not
work according to our intention and it is very difficult to
debug.
• Some times the program does not terminate safely and the
computer gets hanged.
• Some may feel difficulty in coding even though they know
the solution.
• While developing large programs, these problems get
complicated. Several other issues will arise when we want
to develop software solutions for real world problems.
Software Crisis
Software for IRCTC
• Real world entities such as trains, passengers,
employees etc… must be properly represented in
programming.
• Software should be flexible to include changes made
by the Railway minister from time to time based on
the budget/ demand/profitability.
• Changes in Software must be made as fast as
possible. Cost of Software development must be
minimum.
• IRCTC Software must be robust.
Overview of C++
• The origin of C++
– C++ is developed by combining the best features of C
languge and SIMULA language.
– First invented by Bjarne Stroustrup in 1979 at Bell
Laboratories in Murray Hill, New Jersey.
– Initially named as “C with classes”.
– 1983 the name was changed to C++.
– Overcomes the increasing complexity in the
development of software.
Features of C++
• C++ facilitates object oriented programming
approach for problem solving to overcome the
disadvantages of Procedure oriented
programming approach.
• Object-Oriented Programming (OOP) is the term
used to describe a problem solving approach
based the concepts Data Abstraction, Data
Encapsulation, Inheritance and polymorphism.
• C++ provides exception handling.
Data Abstraction
• A programmer following the concept of data abstraction, will first
extract the crucial elements for the creation of the system, without its
implementation details.
• programmer must concentrate only on what is to be done rather than
how it needs to be done. Abstraction is a thought process, it solves
the problems at the design level.
• Programmer will focus on data first and tries to model it before he
actually starts providing the solution. He gathers all the data elements
and groups them according to their properties and behavior.
Example
In our real life we use abstraction in every aspect.
Take the example of Building construction.
 First the engineer gives the model and then a builder starts implementing it.
it. During modeling the engineer first defines the Outer structure using most
important elements such as pillars, beams, no. of floors etc… . So an engineer first
defines the crucial elements of the system. Then he connects them to give a
overall structure to the building.
 The model (also called as template) is generic and does not have
implementation details like which company cement, brand of steel, pipes. Type of
floor (tiles, ). Builder decides it according to the budget and other parameters.
 The template shows important features and hides implementation details.
 With that template a builder can build as many buildings as he wants by
making some changes during the implementation. Like changing color, texture of
the walls, tiles.
Data Abstraction
• A template can be created for any thing, not just for a
building. It can be for a car, phone, table, chair, movie or
a character in a movie .
• Template creation is followed in all manufacturing
companies.
• So why not in software companies.
• C++ language allowed programmers to create templates
before they start developing the actual software.
Programmers can create any number of templates
(classes) and they can implement these templates to
develop a program.
Data Abstraction using class
• In C++ there is a feature called Class which serves as a model or template.
Using Class we can model real world objects like train, employee, ticket
etc… . A train class serves as a model for all the trains running between
various stations. The train class captures the common properties of all the
trains and also their behavior.
• So while developing software for IRCTC, first we build classes(models) for
trains, employees, tickets etc.. To capture their properties and behaviour.
• Once the classes are ready, we use them to build the software by creating
as many train objects, tickets as we want.
Data Encapsulation
• A class is an advanced version of a structured
datatype present in C language. A structured data
type can hold only data items whereas a class can
hold dataitems and functions.
• So encapsulation binds together the data and the
functions that operate on it.
• It’s similar to a protective
wrapper that hides the code and data within the
class.
What Is a Class?
Low level definition)
• Class: A class in C++ is the building block, that leads to Object-
Oriented programming. It is a user-defined data type, which holds
its own data members and member functions, which can be
accessed and used by creating an instance of that class. A C++ class
is like a blueprint for an object.
• Examples : Bird, Fruit, Account,Student, Engineering, College
• Object: Any real world entity which has a name and can be
described is an entity.
• Examples: eagle, apple, sbaccount123, Chris , IT, GVPCE(A)
• OOP treats data as a critical element in the program
development and does not allow it to flow freely around the
system.
• It ties data more closely to the function that operate on it, and
protects it from accidental modification from outside
function.
• OOP allows decomposition of a problem into a number of
entities called objects and then builds data and function
around these objects.
• The data of an object can be accessed only by the function
associated with that object. However, function of one object
can access the function of other objects.
Difference between procedure oriented and
object oriented programming
• Divided Into
– In POP, program is divided into small parts called functions.
– In OOP, program is divided into parts called objects.
• Importance
– In POP, Importance is not given to data but to functions as well
as sequence of actions to be done.
– In OOP, Importance is given to the data rather than procedures
or functions because it works as a real world.
• Approach
– POP follows Top Down approach.
– OOP follows Bottom Up approach.
• Access Specifies
– POP does not have any access specified.
– OOP has access specifies named Public, Private, Protected, etc.
• Data Moving
– In POP, Data can move freely from function to function in the
system.
– In OOP, objects can move and communicate with each other
through member functions.
• Expansion
– To add new data and function in POP is not so easy.
– OOP provides an easy way to add new data and function.
• Data Access
– In POP, Most function uses Global data for sharing that can be
accessed freely from function to function in the system.
– In OOP, data can not move easily from function to function,it can be
kept public or private so we can control the access of data.
• Data Hiding
– POP does not have any proper way for hiding data so it is less secure.
– OOP provides Data Hiding so provides more security.
• Overloading
– In POP, Overloading is not possible.
– In OOP, overloading is possible in the form of Function Overloading
and Operator Overloading.
• Examples
– Example of POP are : C, VB, FORTRAN, Pascal.
– Example of OOP are : C++, JAVA, VB.NET, C#.NET.
Building software for
State Bank of India
• State bank of India is the largest bank in India. It has
many employees, customers, branches, assets etc..
• It gives loans, takes deposits, sells shares, bonds etc…
• Procedure oriented programming cannot be used as it
is not a simple problem. (so C language is not suitable)
• Object oriented programming is the only solution.
• In Object oriented approach the initial focus is on data.
(not the algorithm).
• First we have to identify crucial elements of the
Banking system (Data Abstraction).
• Loan, Deposit, Manager, Clerk, Cashier,
Branch, share, debts, assets etc…
• Identify static nature (Properties) and dynamic
nature (Behavior) of the above entities.
• Bind properties and behavior (Encapsulation)
Cashier
Name
Aadhaar Number
Service
Salary
recieveAmount()
provideAmount()
maintainBalanceSheet()
Manager
Name
Aadhaar Number
Service
Salary
IssueLoan()
IssueChequeBook()
WaiveOffoan()
availCarPetrolAllowance()
Clerk
Name
Aadhaar Number
Service
Salary
issueToken()
verifySignature()
issueApplications()
Branch
Name
IFSC code
Address
Assets
Loan
Customer name
AadharNumber
Loan Number
Loan Amount
Rate of interest
Number of years
Deposit
Customer name
AddharNumber
Deposit Number
Deposit Amount
Rate of interest
Number of years
Customer
Name
Aadhaar Number
Account number
Address
Apply for Loan()
Deposit()
withdrawAmount()
• Create class templates for each of these
entities in C++. (II UNIT)
• Create objects using the classes.
• Example: int x;
• Manager HyderbadBranch_Mgr,
VizagBranch_Mgr.
• Loan kingfisher_buildingConstr.
• Cashier VZNM_cashier.
• Write a c++ program using the relationship
between the objects.
• Reuse code by applying concepts such as
inheritance and polymorphism. (III UNIT)
• Try to identify class templates for chess game
software/ online reservation system/ library
management system/ Amazon.com
Features of OOPs
• Objects Ex: RVVMuraliKrishna, Mr.D.Arun Kumar
• Classes Ex: Faculty (name,service teach(), evaluate(), learn()
)
• Encapsulation (properties+behavior)
• Data abstraction (identifying crucial elements of system)
• Polymorphism ( an object exhibiting different types of behavior at
compile time or at run time)
• Inheritance
• Dynamic binding
• Message passing
Benefits of
Object oriented programming
• Readability of programs
• Easy to write programs
• Modularity for easier troubleshooting.
• Reuse of code through inheritance. ...
• Flexibility through polymorphism. ...
• Effective problem solving.
Applications of Object oriented
programming
• User interface design such as windows, menu.
• Real Time Systems.
• Simulation and Modeling.
• Object oriented databases.
• AI and Expert System.
• Neural Networks and parallel programming.
• Decision support and office automation
systems etc.
Structure of C++ Program
Identify the classes and their
properties and functions
• Sharukh khan, Brahmanandam,
• Shankar, Mahesh babu, Allu arjun,
• Shruthi hasan, Raja mouli, Allu arjun,
• Pawan Kalyan, Sunil,Pooja hegde
Tokens in C++
C++ Character Set
• The C++ character set consists of 3 main elements. They are:
• Letters: These are alphabets ranging from A-Z and a-z (both
uppercase and lowercase characters convey different
meanings)
• Digits: All the digits from 0 – 9 are valid in C++.
• Special symbols: There are a variety of special symbols
available in C++ like mathematical, logical and relational
operators like +,-, *, /, , ^, %, !, @, #, ^, &, (, ), [, ], ; and
many more.
Keywords
class,
public, private, protected
bool, true,false
try, catch, throw,
using, name space
new, this, delete, mutable
Keywords specific to C++
Data types
Variable declaraion
Example:
int a, b, c;
double x;
int sum;
char my-character;
bool status;
Input statement in C++
cin >> variable-name;
Meaning: read the value of the variable called <variable-name> from the user
• This operator ( >> ) applied to an input stream is known as
extraction operator Extracts and parses characters sequentially from the
stream to interpret them as the representation of a value of the proper
type, which is stored as the value of val
Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
Output statements in C++
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my-character << “ he he
he”
<< endl;
Hello World Program
When learning a new language, the first program people
usually write is one that salutes the world :)
Here is the Hello world program in C++.
#include <iostream.h>
int main() {
cout << “Hello world!”;
return 0;
}
• Using namespace std;
• We have sirnames
• Sirnames differentiate people having the same
name.
• Sirnames differentiate objects having the same
name.
• Namespaces differentiate objects having the
same name.
Reference variables
• A reference variable does not consume any extra
memory. It has the same memory address as the
variable it refers to. While a pointer needs extra
space for itself.
• In order to access the value of the referenced
variable using pointer, we need to use a
dereferencing operator(*) whereas we can do the
same using reference variable without any
dereferencing method.
• Because references must be initialized to valid
objects (cannot be null) and can not be
changed once set, references are generally
much safer to use than pointers (since there’s
no risk of dereferencing a null pointer).
However, they are also a bit more limited in
functionality accordingly. int &a=b;
• Reference variables act as shortcuts
Pointer Variables
• dynamic memory allocation.
• Malloc,calloc, realloc,free()
• Int *p= (int*)malloc(sizeof(int));
• Int *p=(int*) malloc(10*sizeof(int));
• new , delete
• int *p= new int; delete p;
• Int *p=new int[5];
Dynamic memory allocation
• Dynamic memory allocation is the process of
assigning the memory space during the execution
time or the run time.
Advantages
• When we do not know how much amount of
memory would be needed for the program
beforehand.
• When we want data structures without any upper
limit of memory space.
• When you want to use your memory space more
efficiently. Example: int Roll_Numbers[100];
Heap
Stack
Global and static data
Code
Logical Image of RAM
“new” operator
• The new operator requests for the memory
allocation in heap. If the sufficient memory is
available, it initializes the memory to the
pointer variable and returns its address.
• pointer_variable = new datatype;
• pointer_variable = new datatype(value);
• pointer_variable = new datatype[size];
“delete” operator
• The delete operator is used to deallocate the
memory. User has privilege to deallocate the
created pointer variable by this delete
operator.
• delete pointer_variable;
• delete[ ] pointer_variable;
Advantages of using
functions/procedures
• To increase readability of the code
• To make the program handy to make any changes
in future or to correct the errors.
Disadvantages
• Too many procedures/functions can reduce
performance (speed).
Function call implementation
Inline functions
• In C++, we can declare a function as inline.
This copies the function to the location of the
function call in compile-time and may make
the program execution faster.
• inline returnType functionName(parameters)
{
// code
}
Inline functions
Source: Programiz.com
When inline functions can be used?
• For small functions we can use inline
functions. It creates faster code and smaller
executables.
• When functions are small and called very
often, we can use inline.
When we should avoid the use of
inline functions?
• We should not use functions that are I/O
bound as inline functions.
• When large code is used in some function,
then we should avoid the inline.
• When recursion is used, inline function may
not work properly.
C++ Polymorphism
• The term "Polymorphism" is the combination of
two words "poly" + "morphs" which means many
forms or many behaviours. It is a greek word.
• Real Life Example Of Polymorphism
• Let's consider a real-life example of
polymorphism. A person behaves like an
employee in office, like a father or mother at
home and as a customer in a market. Here, a
single person is behaving differently according to
the changing environments.
• There are two types of polymorphism, static and
dynamic.
Static polymorphism
• When the behavior changes according to the
change in environment, it is called static
polymorphism.
• Here we can identify the behaviour (functions)
of a person based on the context.
• Consider an example of a lady faculty, when
she is in college she will teach. When she is at
home she will cook.
• This is called static polymorphism.
Dynamic polymorphism
• Sometimes we can judge the behaviour only
after he/she does something.
• This is called dynamic polymorphism
Polymorphism in C++
• Strousstrup has incorporated the concept of
polymorphism in C++ programming.
• In C++ a function or an operator can exhibit
different behavior based on the environment.
• >> behaves as a Right shift operator or as an
extraction operator (input operator)
• We can also change behavior of + operator
(arithmetic operator) as a concatenation
operator when it is with the strings.
Examples
• A function such as sort(int a[]) can change its
behaviour and will be able to sort strings.
• So it will accept string array as input
sort(char *s[])
.
• The environment for a function is its
parameters. For example : add(int a, int b)
• Since a C++ compiler can know the
parameters of a functions at compile time, it
can easily determine the behavior of the
function at compile time itself.
• So static polymorphism is also called as
compile time polymorphism.
2+3
“hello”+”world”
overloading
• In polymorphism, we are giving additional
responsibility /role to an existing operator or
function.
• So we are overloading the operator/functions
with additional responsibilities. So compile
time polymorphism is categorized into
function overloading and operator
overloading.
Function overloading
• Function overloading is a C++
programming feature that allows us to have more
than one function having same name but
different parameter list.
• So we can write functions with the same name
but with different logic.
source: beginnersbook.com
• The overloaded functions are invoked by
matching the type and number of arguments.
This information is available at the compile
time and, therefore, compiler selects the
appropriate function at the compile time.
• The easiest way to remember this rule is that
the parameters should qualify any one or
more of the following conditions, they should
have different type, number or sequence of
parameters.
source: beginnersbook.com
Different parameter type
• sum(int num1, int num2)
• sum(double num1, double num2)
Different number of parameters
• sum(int num1, int num2)
• sum(int num1, int num2, int num3)
Different sequence of parameters
• sum(int num1, double num2)
• sum(double num1, int num2)
Invalid function overloading
• int sum(int, int)
• double sum(int, int)

More Related Content

Similar to Programming using C++ - slides.pptx

Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
Mansi Tyagi
 
Unit 1 OOSE
Unit 1 OOSEUnit 1 OOSE
Unit 1 OOSE
saranive23
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
Abdii Rashid
 
Python-unit -I.pptx
Python-unit -I.pptxPython-unit -I.pptx
Python-unit -I.pptx
crAmth
 
Oose unit 1 ppt
Oose unit 1 pptOose unit 1 ppt
Oose unit 1 ppt
Dr VISU P
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
SherinRappai1
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
SherinRappai
 
OOSE Unit 1 PPT.ppt
OOSE Unit 1 PPT.pptOOSE Unit 1 PPT.ppt
OOSE Unit 1 PPT.ppt
itadmin33
 
Ahmadalsawad
AhmadalsawadAhmadalsawad
Ahmadalsawad
ahmadalsawad
 
CHAPTER-1.ppt
CHAPTER-1.pptCHAPTER-1.ppt
CHAPTER-1.ppt
Tekle12
 
Chapter no 1
Chapter no 1Chapter no 1
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
AttaullahRahimoon
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
Selvaraj Seerangan
 
OOSE UNIT-1.pdf
OOSE UNIT-1.pdfOOSE UNIT-1.pdf
OOSE UNIT-1.pdf
KarumuriJayasri
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
Gaditek
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
Gaditek
 
Intro to C++ - Class 2 - Objects & Classes
Intro to C++ - Class 2 - Objects & ClassesIntro to C++ - Class 2 - Objects & Classes
Intro to C++ - Class 2 - Objects & Classes
Blue Elephant Consulting
 
Intro To C++ - Class 2 - An Introduction To C++
Intro To C++ - Class 2 - An Introduction To C++Intro To C++ - Class 2 - An Introduction To C++
Intro To C++ - Class 2 - An Introduction To C++
Blue Elephant Consulting
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
The IOT Academy
 

Similar to Programming using C++ - slides.pptx (20)

Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
 
Unit 1 OOSE
Unit 1 OOSEUnit 1 OOSE
Unit 1 OOSE
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Python-unit -I.pptx
Python-unit -I.pptxPython-unit -I.pptx
Python-unit -I.pptx
 
Oose unit 1 ppt
Oose unit 1 pptOose unit 1 ppt
Oose unit 1 ppt
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
 
OOSE Unit 1 PPT.ppt
OOSE Unit 1 PPT.pptOOSE Unit 1 PPT.ppt
OOSE Unit 1 PPT.ppt
 
Ahmadalsawad
AhmadalsawadAhmadalsawad
Ahmadalsawad
 
CHAPTER-1.ppt
CHAPTER-1.pptCHAPTER-1.ppt
CHAPTER-1.ppt
 
Chapter no 1
Chapter no 1Chapter no 1
Chapter no 1
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
OOSE UNIT-1.pdf
OOSE UNIT-1.pdfOOSE UNIT-1.pdf
OOSE UNIT-1.pdf
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Intro to C++ - Class 2 - Objects & Classes
Intro to C++ - Class 2 - Objects & ClassesIntro to C++ - Class 2 - Objects & Classes
Intro to C++ - Class 2 - Objects & Classes
 
Intro To C++ - Class 2 - An Introduction To C++
Intro To C++ - Class 2 - An Introduction To C++Intro To C++ - Class 2 - An Introduction To C++
Intro To C++ - Class 2 - An Introduction To C++
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 

Recently uploaded

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 

Recently uploaded (20)

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 

Programming using C++ - slides.pptx

  • 1. Object Oriented Programming using C++ • Topics to be covered in the first week UNIT-I • Principles of OOP: Software Crisis, Software Evolution-Programming Paradigms, Object Oriented Technology- Basic concepts and benefits of OOP – Application of OOP, OOP languages
  • 2. Need for studying this Subject • C++ is a programming language developed by Bjarne Stroustrup in the year 1983. • It overcomes the complexity in the development of software and the developed software is easy to maintain. • It provides a solution to the Software Crisis
  • 3. Software Crisis • Software Crisis is a term used in computer science for the difficulty of writing useful and efficient computer programs in the required time. • Software was too complex to develop. The crisis manifested itself in several ways: • Projects running over-budget • Projects running over-time • Software was very inefficient • Software was of low quality • Software often did not meet requirements
  • 4. Characteristics of Software • Software must be modular, easily understandable and reusable. • Software development cost should be minimum. • Some may feel difficulty in coding even though they know the solution. So Software development must be easy. • Software should be robust as it is used in scientific and business applications. • The programming languages earlier to C++ could not provide a solution to the software crisis.
  • 5. Evolution of programming languages Machine languages Machine language, or machine code, is a low-level language comprised of binary codes or codes formed using binary digits (ones and zeros). Example : 01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 Disadvantages : • It is very difficult to program in machine language. The programmer has to know details of hardware to write program and has to remember the codes for various arithmetic and logical operations. • The programmer has to remember too many codes to write a program. It results in programming errors. It is difficult to debug the program also.
  • 6. Assembly languages As programs grew, assembly language was invented so that a programmer could deal with larger, increasingly complex programs, using symbolic representations of the machine instructions (ADD R1,R2, ). User need not remember binary codes. Disadvantages: • It takes a lot of time and effort to write the code for the same. • It is very complex and difficult to understand. • The syntax is difficult to remember. • It has a lack of portability of program between different computer architectures. Example Code: global _main extern _printf section .text _main: push message call _printf add esp, 4 ret message: db 'Hello, World!', 10,
  • 7. High level languages As programs continued to grow, high-level languages were introduced that gave the programmer more tools to handle complexity. • The first language was FORTRAN. It was very impressive first step, encourages clear, easy-to-understand program. But the programming used to be monolithic. • From 1950 onwards structured programming (blocks) and Procedure (module/ function) oriented programming came into existence. Instructions in the program were divided into Blocks, modules/functions/procedures. Coding became easy as program can be developed in small modules. A large task is divided into sub tasks and each task is implemented using a procedure. Examples ?
  • 8.
  • 9. Characteristics of procedure-oriented Programming • Emphasis is on doing things (algorithms). • Large programs are divided into smaller programs known as functions. • Most of the functions share global data. • Data move openly around the system from function to function. • Functions transform data from one form to another. • Employs top-down approach in program design.
  • 10. • Disadvantages of C language/ Procedure Oriented Programming?
  • 11. • I think the most common problem is that the code does not work according to our intention and it is very difficult to debug. • Some times the program does not terminate safely and the computer gets hanged. • Some may feel difficulty in coding even though they know the solution. • While developing large programs, these problems get complicated. Several other issues will arise when we want to develop software solutions for real world problems. Software Crisis
  • 12. Software for IRCTC • Real world entities such as trains, passengers, employees etc… must be properly represented in programming. • Software should be flexible to include changes made by the Railway minister from time to time based on the budget/ demand/profitability. • Changes in Software must be made as fast as possible. Cost of Software development must be minimum. • IRCTC Software must be robust.
  • 13. Overview of C++ • The origin of C++ – C++ is developed by combining the best features of C languge and SIMULA language. – First invented by Bjarne Stroustrup in 1979 at Bell Laboratories in Murray Hill, New Jersey. – Initially named as “C with classes”. – 1983 the name was changed to C++. – Overcomes the increasing complexity in the development of software.
  • 14. Features of C++ • C++ facilitates object oriented programming approach for problem solving to overcome the disadvantages of Procedure oriented programming approach. • Object-Oriented Programming (OOP) is the term used to describe a problem solving approach based the concepts Data Abstraction, Data Encapsulation, Inheritance and polymorphism. • C++ provides exception handling.
  • 15. Data Abstraction • A programmer following the concept of data abstraction, will first extract the crucial elements for the creation of the system, without its implementation details. • programmer must concentrate only on what is to be done rather than how it needs to be done. Abstraction is a thought process, it solves the problems at the design level. • Programmer will focus on data first and tries to model it before he actually starts providing the solution. He gathers all the data elements and groups them according to their properties and behavior.
  • 16. Example In our real life we use abstraction in every aspect. Take the example of Building construction.  First the engineer gives the model and then a builder starts implementing it. it. During modeling the engineer first defines the Outer structure using most important elements such as pillars, beams, no. of floors etc… . So an engineer first defines the crucial elements of the system. Then he connects them to give a overall structure to the building.  The model (also called as template) is generic and does not have implementation details like which company cement, brand of steel, pipes. Type of floor (tiles, ). Builder decides it according to the budget and other parameters.  The template shows important features and hides implementation details.  With that template a builder can build as many buildings as he wants by making some changes during the implementation. Like changing color, texture of the walls, tiles.
  • 17. Data Abstraction • A template can be created for any thing, not just for a building. It can be for a car, phone, table, chair, movie or a character in a movie . • Template creation is followed in all manufacturing companies. • So why not in software companies. • C++ language allowed programmers to create templates before they start developing the actual software. Programmers can create any number of templates (classes) and they can implement these templates to develop a program.
  • 18. Data Abstraction using class • In C++ there is a feature called Class which serves as a model or template. Using Class we can model real world objects like train, employee, ticket etc… . A train class serves as a model for all the trains running between various stations. The train class captures the common properties of all the trains and also their behavior. • So while developing software for IRCTC, first we build classes(models) for trains, employees, tickets etc.. To capture their properties and behaviour. • Once the classes are ready, we use them to build the software by creating as many train objects, tickets as we want.
  • 19. Data Encapsulation • A class is an advanced version of a structured datatype present in C language. A structured data type can hold only data items whereas a class can hold dataitems and functions. • So encapsulation binds together the data and the functions that operate on it. • It’s similar to a protective wrapper that hides the code and data within the class.
  • 20. What Is a Class? Low level definition) • Class: A class in C++ is the building block, that leads to Object- Oriented programming. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object. • Examples : Bird, Fruit, Account,Student, Engineering, College • Object: Any real world entity which has a name and can be described is an entity. • Examples: eagle, apple, sbaccount123, Chris , IT, GVPCE(A)
  • 21. • OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. • It ties data more closely to the function that operate on it, and protects it from accidental modification from outside function. • OOP allows decomposition of a problem into a number of entities called objects and then builds data and function around these objects. • The data of an object can be accessed only by the function associated with that object. However, function of one object can access the function of other objects.
  • 22. Difference between procedure oriented and object oriented programming • Divided Into – In POP, program is divided into small parts called functions. – In OOP, program is divided into parts called objects. • Importance – In POP, Importance is not given to data but to functions as well as sequence of actions to be done. – In OOP, Importance is given to the data rather than procedures or functions because it works as a real world. • Approach – POP follows Top Down approach. – OOP follows Bottom Up approach.
  • 23. • Access Specifies – POP does not have any access specified. – OOP has access specifies named Public, Private, Protected, etc. • Data Moving – In POP, Data can move freely from function to function in the system. – In OOP, objects can move and communicate with each other through member functions. • Expansion – To add new data and function in POP is not so easy. – OOP provides an easy way to add new data and function.
  • 24. • Data Access – In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system. – In OOP, data can not move easily from function to function,it can be kept public or private so we can control the access of data. • Data Hiding – POP does not have any proper way for hiding data so it is less secure. – OOP provides Data Hiding so provides more security. • Overloading – In POP, Overloading is not possible. – In OOP, overloading is possible in the form of Function Overloading and Operator Overloading. • Examples – Example of POP are : C, VB, FORTRAN, Pascal. – Example of OOP are : C++, JAVA, VB.NET, C#.NET.
  • 25. Building software for State Bank of India • State bank of India is the largest bank in India. It has many employees, customers, branches, assets etc.. • It gives loans, takes deposits, sells shares, bonds etc… • Procedure oriented programming cannot be used as it is not a simple problem. (so C language is not suitable) • Object oriented programming is the only solution. • In Object oriented approach the initial focus is on data. (not the algorithm). • First we have to identify crucial elements of the Banking system (Data Abstraction).
  • 26. • Loan, Deposit, Manager, Clerk, Cashier, Branch, share, debts, assets etc… • Identify static nature (Properties) and dynamic nature (Behavior) of the above entities. • Bind properties and behavior (Encapsulation) Cashier Name Aadhaar Number Service Salary recieveAmount() provideAmount() maintainBalanceSheet() Manager Name Aadhaar Number Service Salary IssueLoan() IssueChequeBook() WaiveOffoan() availCarPetrolAllowance()
  • 27. Clerk Name Aadhaar Number Service Salary issueToken() verifySignature() issueApplications() Branch Name IFSC code Address Assets Loan Customer name AadharNumber Loan Number Loan Amount Rate of interest Number of years Deposit Customer name AddharNumber Deposit Number Deposit Amount Rate of interest Number of years Customer Name Aadhaar Number Account number Address Apply for Loan() Deposit() withdrawAmount()
  • 28. • Create class templates for each of these entities in C++. (II UNIT) • Create objects using the classes. • Example: int x; • Manager HyderbadBranch_Mgr, VizagBranch_Mgr. • Loan kingfisher_buildingConstr. • Cashier VZNM_cashier.
  • 29. • Write a c++ program using the relationship between the objects. • Reuse code by applying concepts such as inheritance and polymorphism. (III UNIT) • Try to identify class templates for chess game software/ online reservation system/ library management system/ Amazon.com
  • 30. Features of OOPs • Objects Ex: RVVMuraliKrishna, Mr.D.Arun Kumar • Classes Ex: Faculty (name,service teach(), evaluate(), learn() ) • Encapsulation (properties+behavior) • Data abstraction (identifying crucial elements of system) • Polymorphism ( an object exhibiting different types of behavior at compile time or at run time) • Inheritance • Dynamic binding • Message passing
  • 31. Benefits of Object oriented programming • Readability of programs • Easy to write programs • Modularity for easier troubleshooting. • Reuse of code through inheritance. ... • Flexibility through polymorphism. ... • Effective problem solving.
  • 32. Applications of Object oriented programming • User interface design such as windows, menu. • Real Time Systems. • Simulation and Modeling. • Object oriented databases. • AI and Expert System. • Neural Networks and parallel programming. • Decision support and office automation systems etc.
  • 33. Structure of C++ Program
  • 34. Identify the classes and their properties and functions • Sharukh khan, Brahmanandam, • Shankar, Mahesh babu, Allu arjun, • Shruthi hasan, Raja mouli, Allu arjun, • Pawan Kalyan, Sunil,Pooja hegde
  • 36. C++ Character Set • The C++ character set consists of 3 main elements. They are: • Letters: These are alphabets ranging from A-Z and a-z (both uppercase and lowercase characters convey different meanings) • Digits: All the digits from 0 – 9 are valid in C++. • Special symbols: There are a variety of special symbols available in C++ like mathematical, logical and relational operators like +,-, *, /, , ^, %, !, @, #, ^, &, (, ), [, ], ; and many more.
  • 38. class, public, private, protected bool, true,false try, catch, throw, using, name space new, this, delete, mutable Keywords specific to C++
  • 40. Variable declaraion Example: int a, b, c; double x; int sum; char my-character; bool status;
  • 41. Input statement in C++ cin >> variable-name; Meaning: read the value of the variable called <variable-name> from the user • This operator ( >> ) applied to an input stream is known as extraction operator Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type, which is stored as the value of val Example: cin >> a; cin >> b >> c; cin >> x; cin >> my-character;
  • 42. Output statements in C++ cout << variable-name; Meaning: print the value of variable <variable-name> to the user cout << “any message “; Meaning: print the message within quotes to the user cout << endl; Meaning: print a new line Example: cout << a; cout << b << c; cout << “This is my character: “ << my-character << “ he he he” << endl;
  • 43. Hello World Program When learning a new language, the first program people usually write is one that salutes the world :) Here is the Hello world program in C++. #include <iostream.h> int main() { cout << “Hello world!”; return 0; }
  • 44. • Using namespace std; • We have sirnames • Sirnames differentiate people having the same name. • Sirnames differentiate objects having the same name. • Namespaces differentiate objects having the same name.
  • 45. Reference variables • A reference variable does not consume any extra memory. It has the same memory address as the variable it refers to. While a pointer needs extra space for itself. • In order to access the value of the referenced variable using pointer, we need to use a dereferencing operator(*) whereas we can do the same using reference variable without any dereferencing method.
  • 46. • Because references must be initialized to valid objects (cannot be null) and can not be changed once set, references are generally much safer to use than pointers (since there’s no risk of dereferencing a null pointer). However, they are also a bit more limited in functionality accordingly. int &a=b; • Reference variables act as shortcuts
  • 47. Pointer Variables • dynamic memory allocation. • Malloc,calloc, realloc,free() • Int *p= (int*)malloc(sizeof(int)); • Int *p=(int*) malloc(10*sizeof(int)); • new , delete • int *p= new int; delete p; • Int *p=new int[5];
  • 48. Dynamic memory allocation • Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. Advantages • When we do not know how much amount of memory would be needed for the program beforehand. • When we want data structures without any upper limit of memory space. • When you want to use your memory space more efficiently. Example: int Roll_Numbers[100];
  • 49. Heap Stack Global and static data Code Logical Image of RAM
  • 50. “new” operator • The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address. • pointer_variable = new datatype; • pointer_variable = new datatype(value); • pointer_variable = new datatype[size];
  • 51. “delete” operator • The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator. • delete pointer_variable; • delete[ ] pointer_variable;
  • 52. Advantages of using functions/procedures • To increase readability of the code • To make the program handy to make any changes in future or to correct the errors.
  • 53. Disadvantages • Too many procedures/functions can reduce performance (speed).
  • 55. Inline functions • In C++, we can declare a function as inline. This copies the function to the location of the function call in compile-time and may make the program execution faster. • inline returnType functionName(parameters) { // code }
  • 57. When inline functions can be used? • For small functions we can use inline functions. It creates faster code and smaller executables. • When functions are small and called very often, we can use inline.
  • 58. When we should avoid the use of inline functions? • We should not use functions that are I/O bound as inline functions. • When large code is used in some function, then we should avoid the inline. • When recursion is used, inline function may not work properly.
  • 59. C++ Polymorphism • The term "Polymorphism" is the combination of two words "poly" + "morphs" which means many forms or many behaviours. It is a greek word. • Real Life Example Of Polymorphism • Let's consider a real-life example of polymorphism. A person behaves like an employee in office, like a father or mother at home and as a customer in a market. Here, a single person is behaving differently according to the changing environments. • There are two types of polymorphism, static and dynamic.
  • 60. Static polymorphism • When the behavior changes according to the change in environment, it is called static polymorphism. • Here we can identify the behaviour (functions) of a person based on the context. • Consider an example of a lady faculty, when she is in college she will teach. When she is at home she will cook. • This is called static polymorphism.
  • 61. Dynamic polymorphism • Sometimes we can judge the behaviour only after he/she does something. • This is called dynamic polymorphism
  • 62. Polymorphism in C++ • Strousstrup has incorporated the concept of polymorphism in C++ programming. • In C++ a function or an operator can exhibit different behavior based on the environment. • >> behaves as a Right shift operator or as an extraction operator (input operator) • We can also change behavior of + operator (arithmetic operator) as a concatenation operator when it is with the strings.
  • 63. Examples • A function such as sort(int a[]) can change its behaviour and will be able to sort strings. • So it will accept string array as input sort(char *s[]) .
  • 64. • The environment for a function is its parameters. For example : add(int a, int b) • Since a C++ compiler can know the parameters of a functions at compile time, it can easily determine the behavior of the function at compile time itself. • So static polymorphism is also called as compile time polymorphism.
  • 66. overloading • In polymorphism, we are giving additional responsibility /role to an existing operator or function. • So we are overloading the operator/functions with additional responsibilities. So compile time polymorphism is categorized into function overloading and operator overloading.
  • 67. Function overloading • Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list. • So we can write functions with the same name but with different logic. source: beginnersbook.com
  • 68. • The overloaded functions are invoked by matching the type and number of arguments. This information is available at the compile time and, therefore, compiler selects the appropriate function at the compile time.
  • 69. • The easiest way to remember this rule is that the parameters should qualify any one or more of the following conditions, they should have different type, number or sequence of parameters. source: beginnersbook.com
  • 70. Different parameter type • sum(int num1, int num2) • sum(double num1, double num2)
  • 71. Different number of parameters • sum(int num1, int num2) • sum(int num1, int num2, int num3)
  • 72. Different sequence of parameters • sum(int num1, double num2) • sum(double num1, int num2)
  • 73. Invalid function overloading • int sum(int, int) • double sum(int, int)