Introduction to Object Oriented Programming Concepts (1).pdf

jabiliadabala22 1 views 73 slides Nov 02, 2025
Slide 1
Slide 1 of 73
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73

About This Presentation

oops concept of c++


Slide Content

OBJECT ORIENTED
PROGRAMMING
DR. DEEPANWITA DAS
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
NATIONAL INSTITUTE OF TECHNOLOGY, DURGAPUR

ACKNOWLEDGEMENT
The contents (figures, concepts, graphics, texts etc.) of the slides are gathered
and utilized from the books mentioned and the corresponding PPTs available
online:
Books:
•Addison Wesley - The C++ Standard Library. A T.
•Competitive Programmer’s Handbook Antti Laakson
•DataStructures_and_Algorithms_in_C++ by Adam Drozdec
•Effective C++ by Scott Meyers
•The C++ Programming Language by Bjarne Stroustrup
• Thinking in Java by Bruce Eckel
• TICPP by Bruce Ekcel
Web References:
1.Programming in C++, Prof. P P Das, NPTEL Video Lectures. Link:
https://nptel.ac.in/noc/courses/noc21/SEM1/noc21-cs02/
2.Compile and Execute C Online (GNU GCC v7.1.1), Link:
https://www.tutorialspoint.com/compile_c_online.php
Disclaimer: The study materials/presentations are solely meant for academic
purposes and they can be reused, reproduced, modified, and distributed by
others for academic purposes only with proper acknowledgements.

Outline
•Procedure-oriented paradigm
•Features & Limitations
•Real world perception
•Object-oriented Paradigm
•Features
•Terminologies
•Advantages
•Object-based vs Object-oriented Language
•C++ as a Object-oriented Language
•The 1
st
C++ code

PROCEDURE ORIENTED PROGRAMMING
•In procedure oriented programming the problem is divided
into several parts and each part is represented by functions.
•The output of one function may be the input of some other
function.
•All the functions are solved to solve the whole problem.
•Main focus is on the functions.
•The functions consists of a set of instructions to be followed
by the computer.
Ex: Result Processing to create rank list & Finding attendance
defaulters using c language.

We need to store marks obtained by the students -1D array
We need to store attendance for every lecture – 2D array
Functions – main() - > readmarks()->createrank() -> sort()
main()-> readattendance()-> find defaulter()
main()-> display()

PROCEDURE ORIENTED PROGRAMMING
•One flowchart is used to organize these functions and
represent the flow of control from one function to another.
main ()
find_defautr()
read_attnd()
sort()
read-
marks()
create_rank()
display_rank()
Top-down design approach

(Features)
•Top-down approach
•Task: Identify procedures (sub-routines/modules/functions)
•Procedures properties:
•Implement a module using structured code (no overlapping of loops, no jump
directly into a loop using goto label)
•Highly cohesive (each consist of related set of instructions are put together)
•Low coupling (low interaction and dependency with others)
•Say a function f2() can be called iff another function f1() is passing 40 variables so
the dependency is very high as calling of f2() depends on f1(). This is referred as
high coupling.
•So the number of parameters passed must be as less as possible to achieve low
coupling.
•One way to ensure low coupling if we make everything as global, then no need to
pass parameters to call any function. In multifunction program, many important
items are placed as global so that they may be accessed by all the functions.
•As the data are global, some inadvertent changes to the data may occur. One
function may transform the data which some other function may be using that time.
•In a large program it is very difficult to identify what data is used by which function.
In case of any modification we need to revise all functions that access the data.
PROCEDURE ORIENTED PROGRAMMING

Relationship between data and function
PROCEDURE ORIENTED PROGRAMMING
Global data Global data
Function - 1
Local data
Function - 2
Local data
Function - 3
Local data

(Features)
•If a function keeps everything as local, then to call another function it need to pass
all argument to the called function, which results high dependency or high
coupling. This is not ideal.
•Moreover, reusability becomes very difficult if we do not use highly cohesive code
or related codes. If we put everything in main() they do not remain highly cohesive
and a part of the code cannot be reused separately.
•Main focus is on the functions and very little attention is given to the data that are
used by various functions.
•Data and code that operate on this data are not tightly coupled.
•It does not model real world problems very well.
PROCEDURE ORIENTED PROGRAMMING

•Inherent cohesion of data and procedures can’t be captured in the design
•Not totally reusable (can reuse only procedures not data)
•No keyword to have precise control over data within a module
•Global (full coupling)
•Local variables (zero coupling)
•Complete focus on modules; no emphasis on data
(Limitations)
PROCEDURE ORIENTED PROGRAMMING

MODEL THE STAKEHOLDERS SHOWN IN THIS SCENARIO
T
1
T
2
T
3
T
5
T
4
P
1 P
2
P
3
P
4
P
5
P
6
C
3C
1 C
2
B
3B
1B
2
NITD
SBI
Identified Entities
Person – 6
phones - 6
Vehicles – 6
Trees – 5
Buildings - 2

Phone
Identify the difference between the Entities – characteristics of the entities
Ph No, Receiver, Make
Make call
Recv call
Send msg
Consists of
Capable of
Vehicle
Vehicle No, wheels
Color, Make
Drive
Park
Characteristics
Capabilities/
behaviour
Definitions of all phones
Definitions of all Vehicles
Can you find the difference
between defining the
entities in C and OOP?
Account
Acc No, Name, balance
UID
Get cash
Deposit cash
Get balance
Not physically visible entity

Phone
Identify the difference between the Entities – characteristics of the entities
Ph No, Receiver, Make
Make call()
Recv call()
Send msg()
Vehicle
Vehicle No, wheels
Color, Make
Drive
Park
Definitions of all phones
Definitions of all Vehicles
Account
Acc No, Name,
balance, UID
Get cash()
Deposit cash()
Get balance()
Definitions of accounts
Account
Name, age, gender,
vehicle, phone,
account
b.Deposit cash()
v.Drive()
p.Make call()
Register()
Takeclass()
Definitions of all person
n a g
pv
b
So all persons
have similar
behavior and
characteristics -
this is called a
class of persons
Each individual
entity is called an
object.

OBJECT ORIENTED PROGRAMMING
•It removes the drawbacks of procedure oriented
programming.
•In this approach, a problem is divided into number
of objects.
•Objects bind data more closely to the functions that
operate on it and protect it from accidental
modification from outside functions.
•The data of an object can be accessed by the
functions associated with that object.

OBJECT ORIENTED PROGRAMMING
•Objects may communicate with each other
through functions.

Data
Functions
Data
Functions
Data
Functions
Object A
Object B
Object C

OBJECT ORIENTED PROGRAMMING
Some important features of object-oriented programming are:
•Emphasis is on data rather than procedure
•Programs are divided into what are known as objects
•Data structures are designed such that they characterize the
objects
•Functions that operates on the data of an object are tied
together in the data structure
•Data is hidden and cannot be accessed by external functions
•New data and functions can be easily added whenever
necessary
•Follows bottom-up approach

BASIC CONCEPTS OF OOP
Objects:
•objects are the basic runtime entity in an object oriented
system.
•May be person, place or anything.
•Each object contains data and code to manipulate the data.
•Problem is analyzed in terms of objects and the nature of
communication between them.
•When a program is executed the objects interact by sending
messages to one another.

BASIC CONCEPTS OF OOP
Class :
•Collection of similar type of objects.
•Objects are the variable of type class.
•Once class has been defined, we can create any number of
objects belonging to that class.
•Each object is associated with the data of type class with
which they are created.
•For example: mango, apple, orange are objects of the class
fruit.

Identify the difference between the Entities – characteristics of the entities
Account
Name, age, gender,
vehicle, phone,
account
b.Deposit cash()
v.Drive()
p.Make call()
Register()
Takeclass()
Definitions of all person
Person
phone vehicle
account
A person class entity is composed of an
account entity, vehicle entity and phone entity
has-a
has-a
has-a

Identify the difference between the Entities – characteristics of the entities
Phone
Ph No, Receiver, Make
Make call()
Recv call()
Send msg()
Smart Phone
Ph No, Receiver, Make,
touchpad, camera
Make call()
Recv call()
Send msg()
Take photo()
Play music()
Browsing()
Enhance the features and behavior
Smartphone is derived
from the phone class

Identify the difference between the Entities – characteristics of the entities
Person
phone vehicle
account
has-a
has-a
has-a
Smart
phone
keypad
phone
is-a is-a
Smartphone class definition is derived from phone class.
Composition
Inheritance

Identify the difference between the Entities – characteristics of the entities
Person
phone vehicle
account
has-a
has-a
has-a
Smart
phone
keypad
phone
is-a is-a
4Wheeler
2Wheeler
Savings
current
is-ais-a
is-a
is-a
is-a
is-a
is-a
student
stuff
faculty

•Real world consists of identifiable, classifiable, interacting set of items objects
•Each object is having
•Identifiable characteristics (set of attributes) Data Variables
•Well-defined behavior (set of functionalities/capabilities) Methods/Functions
•All objects having the same behavior are grouped under a category called class
•State of an object is determined by the values assigned to its attributes
•Some attributes and behavior may not be accessible to others (Private) outside the scope of the object;
whereas remaining ones may be accessible as (Public)
•Different Classes may be related to other classes in following way:
•Unrelated
•Exhibit has-a relationship
•Exhibit is-a relationship
•Any two objects may interact with each other by invoking their behavior
Object Oriented Language Features to support Object Oriented Design (OOD)
Encapsulation Class
definition
Access Specifiers (for data and methods)
Composition
Inheritance
Message passing

FEATURES OF OOP
•Real world abstraction can be modelled in a better way
•Encapsulation – ability to bind related data & method together to obtain more
cohesive modules
•Hiding – ability to hide private class members (data & method) so that they
can’t be accessed by others outside
•Inheritance – increases reusability of the class module as a whole
•Polymorphism - Programmer-friendly and increased more code readability
•Easy to maintain – modification or changes in design can easily be
incorporated

ENCAPSULATION AND DATA ABSTRACTION
•Wrapping up of data and function into a single unit (called
class) is called data encapsulation.
•Data can be accessed by only those functions which are
wrapped in the class with it.
•This insulation of data from direct access by the program is
called data hiding or information hiding.
•Abstraction means representing essential features without
including the background details or explanations.
•Class uses the concept of abstraction.
•They are having some attributes and functions but
encapsulates all the essential properties at the objects that
are to be created.

INHERITANCE
•Process by which objects of one class acquires the properties
of another class.
•It provides the idea of reusability .
•We can add features to an existing class without modifying it.
Bird
Flying Non flying
Robin Swallow
Penguin

POLYMORPHISM
•Means ability to take more than one form.
•A function may exhibit different behaviors in different
instances- function overloading
•For operators the same thing is known as operator
overloading.
Shape
Area()
Circle
Area()
Rectangle
Area()

Analogy
•Data Type
•Variable
•Function/procedure/sub-routine
•Function call
•Header file (library of pre-defined functions/procedures)
Procedure-oriented approach
•Abstract Data Type
•Object
•Method/service
•Message passing
•Header file (library of pre-defined class definitions,
pre-declared objects)
Object-oriented approach

INTRODUCTION TO C++
•C is a programming language developed in the 1970's alongside
the UNIX operating system.
•C provides a comprehensive set of features for handling a wide
variety of applications, such as systems development and
scientific computation.
•C++ is an “extension” of the C language, in that most C
programs are also C++ programs.
•C++, as opposed to C, supports “object-oriented programming.”

GENERAL STRUCTURE OF PROGRAM
Include files
Class declaration
Member function definition
Main program

GENERAL FORM OF A C++ PROGRAM
// Include files
#include directives
//Class declaration
class classname
{
};
//Member function definition
member_function()
{
function body
}
//Main function
int main(void)
{
constant declarations
variable declarations
executable statements
return 0;
}

EXAMPLE – ADDING 2 NUMBERS
Peter: Hey Frank, I just learned how to add two numbers together.
Frank: Cool!
Peter : Give me the first number.
Frank: 2.
Peter : Ok, and give me the second number.
Frank: 5.
Peter : Ok, here's the answer: 2 + 5 = 7.
Frank: Wow! You are amazing!
after Frank says “2”, Peter has to keep this number in his mind.
2 5 7First number: Second number: Sum:
after Frank says “5”, Peter also needs to keep this number in his mind.

The Corresponding C++ Program
#include <iostream>
using namespace std;
int main(void)
{
int first, second;
cout << "Peter: Hey Frank, I just learned how to add”
<< “ two numbers together."<< endl;
cout << "Frank: Cool!" << endl;
cout << "Peter: Give me the first number."<< endl;
cout << "Frank: ";
cin >> first;
cout << "Peter: Give me the second number."<< endl;
cout << "Frank: ";
cin >> second;
int sum = first + second;
cout << "Peter: OK, here is the answer:";
cout << sum << endl;
cout << "Frank: Wow! You are amazing!" << endl;
return 0;
}
printf(“Frank: Cool! \n”);
scanf("%d", &first);
printf("Peter: OK, here is the answer: %d", sum);
#include<stdio.h>
int main()
Dynamic declaration

C++ PROGRAM FEATURES
•Like C, C++ program consists of several functions. The execution begins
with only one function that is main().
•The two keywordscout in C++andcin in C++are used very often for
printing outputs and taking inputs respectively.
•To use cin and cout in C++ one must include the header fileiostreamin the
program.
•Standard input stream (cin): Usually the input device in a computer is the
keyboard. C++, cin statement is the instance of the classistreamand is
used to read input from the standard input device which is usually a
keyboard. The extraction operator(>>) is used along with the objectcinfor
reading inputs. The extraction operator extracts the data from the
objectcinwhich is entered using the keyboard. cout << "Frank: Cool!"
<<endl;
•Standard output stream (cout): The coutstatement is the instance of the
ostream class. It is used to produce output on the standard output device
which is usually the display screen. The data needed to be displayed on the
screen is inserted in the standard output stream (cout) using the insertion
operator(<<).

C++ PROGRAM FEATURES
•>> & << are also known as the bitwise right and left shift operators.
•The definitions of how the cin>> and cout<< work are included in the
stream classes (i.e., istream and ostream), both of which are inherited
class from base class iostream (is-a relationship).
•The multiple use of << in one statement is called cascading. The
cascading for >> operator can also be done in a following way: cin >>
first>>second;
Input devices
Keyboard
Mouse
Scanner, files
Output devices
Printer
Monitor
Plotter
Input buffer
Output buffer
Extraction of input info
cin>>
Insertion of output info
cout<<
processing

C++ COMMENTS
•Comments are explanatory notes; they are ignored by the
compiler.
•There are two ways to include comments in a program:
// A double slash marks the start of a //single line
comment.
/* A slash followed by an asterisk marks the start of a
multiple line comment. It ends with an asterisk followed by
a slash. */

C++ KEYWORDS
•Keywords are those words whose meaning is already
defined by Compiler.
•Each keyword has a predefined purpose in the language.
•Do not use keywords as variable and constant names!!
•Some frequently used keywords are:
bool, break, case, char, const, continue,
do, default, double, else, extern, false,
float, for, if, int, long, namespace,
return, short, static, struct, switch,
typedef, true, unsigned, void, while

C++ COMPILER DIRECTIVES
The #include directive tells the compiler to include some already existing C++
code in your program.
The included file is then linked with the program. There are two forms of #include
statements:
•#include <iostream> //for pre-defined files
iostream stands for standard input-output stream. This header file contains
definitions to objects like cin, cout etc.
•#include "my_lib.h" //for user-defined files
Header File is the file where all the headers name are mentioned that going to be
used or consumed in the main code file. It have the function declaration only.
Header file have an extension ".h"
Library file have the implementation code of the header i.e., it has the actual
function definition which is declared in the Header file. Library file have the
extension of .lib.
In programming of any language both header file and library combines and provide
some specific interface and its functionality to be used or consumed in the main
file. So both header file and library are combined and used as single unit

•All C++variablesmust beidentifiedwithunique names. These unique names
are calledidentifiers.
•An identifier is a name for a variable, constant, function, etc.
•Only alphabetic characters, digits and underscores are permitted.
•The name cannot start with a digit. It consists of a letter followed by any
sequence of letters, digits, and underscores.
•Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
•Examples of valid identifiers: First_name, age, y2000, y2k
•Examples of invalid identifiers: 2000y
•Identifiers cannot have special characters in them. For example: X=Y, J-20,
~Ricky,*Michael are invalid identifiers.
•Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI,
whoami are unique identifiers.
•A major difference between C and C++ is that C++ places no limit on its length
Note:It is recommended to use descriptive names in order to create
understandable and maintainable code:
C++ IDENTIFIERS

NAMESPACE
•A namespace is a declarative region that defines the scope for the identifiers
inside it that are used in a program. Namespaces are used to organize code
into logical groups and to prevent name collisions that can occur especially
when your code base includes multiple libraries.
•For example, say a code has a function called xyz() and there is another
library available which is also having same function name xyz(). Now the
compiler has no way of knowing which version of xyz() function is referred in
the code.
•Anamespaceis designed to overcome this difficulty and is used as additional
information to differentiate similar functions, classes, variables etc. with the
same name available in different libraries.
•Namespaces are an element of the programming language - they don't exist
as a file system object, but rather as a designation within code telling the
compiler that certain things are within that namespace. Namespaces typically
group interfaces (functions, classes/structs, types) of similar (but not
necessarily interdependent) items.
•For instance, the std namespace in C++ contains all of the Standard Library
functions and classes.

NAMESPACE
Defining a Namespace
•A namespace definition begins with the
keywordnamespacefollowed by the namespace name as follows −

namespace namespace_name
{ // code declarations }
•Namespace is written with the “using” directive. This directive tells
the compiler that the subsequent code is making use of names in the
specified namespace.

EXAMPLE

PROGRAMMING STYLE
C++ is a free-format language, which means that:
•Extra blanks (spaces) or tabs before or after identifiers/operators are ignored.
•Blank lines are ignored by the compiler just like comments.
•Code can be indented in any way.
•There can be more than one statement on a single line.
•A single statement can continue over several lines.
In order to improve the readability of your program, use the following
conventions:
•Start the program with a header that tells what the program does.
•Use meaningful variable names.
•Document each variable declaration with a comment telling what the variable
is used for.
•Place each executable statement on a single line.
•A segment of code is a sequence of executable statements that belong
together.
•Use blank lines to separate different segments of code.
•Document each segment of code with a comment telling what the segment
does.

WHAT MAKES A BAD
PROGRAM?
•Writing Code without detailed
analysis and design
•Repeating trial and error without
understanding the problem
•Debugging the program line by
line, statement by statement
•Writing tricky and dirty programs

DECLARATION OF VARIABLES
Declaration of variables
•In C variables must be declared before they are used.
•C requires all the variables to be defined at the
beginning of the scope.
•For C++ it is not mandatory . You can declare variables
anywhere within the program before using it.

DYNAMIC INITIALIZATION
•In C variables must be initialized using a constant
expression. C compiler fix the initialization code at the time
of compilation.
•C++ permits initialization of the variable at runtime. This is
referred as dynamic initialization.
•The variable can be initialized at run time using expressions
at the place of declaration.
int n = strlen(string);
float area = 3.14 * rad * rad.
•Both the declaration and initialization of a variable can be
done simultaneously at the place where the variable is used
for the first time.
float average;
average =sum/i ; float average =sum/i ;

REFERENCE VARIABLE
•A reference variable provides an alias (alternative name) for
a previously defined variable.
•Syntax: datatype & ref_name =var_name
Example : float total =100;
float & sum =total;
•Both the variables refer to the same data object in the
memory.
cout<< total;
cout<< sum;
total =total+30; - both total and sum become 130.
sum =0; - both total and sum become 0.
•A reference variable must be initialized at the time of
declaration. Note that the initialization of a reference variable
is completely different from assignment to it.
•Here & is not address of operator. The notation float& means
reference to float.
100
total
sum
Both will print 100

NEW & DELETE OPERATOR IN C++
•C uses malloc() and calloc() functions to allocate memory
dynamically at run time. Similarly a function free() is used to
free the memory.
•C++ uses two operators new and delete to allocate and free
memory in a better and easier way.
•New takes the form:pointer-variable = new data_type;
•Example: int * p; p= new int;
•Initialize memory: pointer-variable = new data_type(value);
•Example: int *p = new int(25)
•Creating array: pointer-variable = new data_type[size];
•Example: int *p = new int[10];
•Creating multi dimentional array: int *p = new int[10][4][5];

NEW & DELETE OPERATOR IN C++
•When a data object is no longer needed, it is destroyed to
release the memory space for reuse.
•General form: delete pointer-variable;
•New has several advantages over malloc():
•It automatically computes the size of the data object. Sizeof
operator need not to be used.
•It automatically returns the correct pointer
•Type, so that there is no need to use a type cast.
•It is possible to initialize the object while creating the memory
space.
•Like any other operator, new and delete can be overloaded.

SCOPE RESOLUTION OPERATOR
Like C, C++ is also a block-structured language.
Same variables can be declared in different blocks because the
variables declared in blocks are local to that function.
In C, the global version of a variable can't be accessed from the inner
block.
C++ resolves this problem by introducing a new operator :: called the
scope resolution operator. This can be used to uncover a hidden
variable.

SCOPE RESOLUTION OPERATOR
•Syntax: :: variable_name;
•Scope resolution operator (::) in C++ is used to define a function outside a
class or when we want to access a global variable but also has a local
variable with the same name.

SCOPE RESOLUTION OPERATOR
In C scope can not
be created inside a
local scope
But in C++ nested
scopes can be
created.
Pair of parenthesis
{} can be used to
create nested
scope
:: operator is used
to access the
variables in global
scope

Whenever we access the
local copy of the variable
(i.e. without :: ) the compiler
moves up to find a local
declaration of the variable
within the current scope or
it scans upward to find a
local declaration within its
outer scope.
If there is no global scope
declared use of :: displays
error message.

If a local copy is declared
inside the scope where the
variable is accessed, it
always uses the value of
that local copy.

The global copy can also be
updated using ++ or --.

CONTROL STRUCTURES
Mainly three control structures are used:
1.Sequence structure
2.Selection structure
3.Loop structure
Control structures are
implemented using the
1.If-else
2.Switch case
3.Do-while, while, for loops

FUNCTIONS IN C++
•Dividing a program into functions is one of the major principles of top-
down approach and in structured programming.
•It is also possible to reduce the size of a program by calling and using
them at different places in the program.
•In C++ functions are used and modified to make them more reliable
and flexible.
•Like operators, a C++ function can be overloaded to make it perform
different tasks depending on the arguments passed to it.
Different types of functions used:
1.The main function
2.The methods or member functions
3.Inline function
4.Friend function
5.Virtual function

MAIN() FUNCTIONS
The main() function : starting point for the execution of a program
In C, main() does not return any value. The definition of main is as
follows:
main()
{
// main program body
}
In C++, the main() returns a value of type int to the operating system.
C++ explicitly defines main() as
int main(void);
int main(int argc, char* argv[ ]); This declaration is used when your
program must take command-line arguments. When run like such:
argc: it is the number of arguments passed to the program.
argv[]: points to a string array and is called theargument vector. It is a
one dimensional string array of function arguments.
myprogram arg1 arg2 arg3

MAIN() FUNCTIONS
The functions that have a return value should use the return statement for
termination. The main() function in C++ is defined as:
int main(void)
{
\\ body of program
return 0;
}
Many operating systems test the return value ( exit value) to determine if
there is any problem.
The normal convention is, if the exit value =0 means the program ran
successfully.
The explicit use of a return (0) statement will indicate that the program
was successfully executed.

MEMBER FUNCTIONS
The methods or member functions:
1.Function prototyping type function_name (argument_list);
int sum(int, int);
2.Function definition type function_name (argument_list)
{
\\ body of the function
}
int sum (a, b)
{
int c=a+b;
return c;
}
3.Function calling function_name(arguments);
int n=sum(5,10);

INLINE FUNCTION
Execution of a function call have some steps –
(1)CPU stores the address of the function call instruction in the
memory
(2)CPU copies the arguments on the stack by PUSH method
(3)CPU transfers the control to the specified function
(4)Next that specified function code is executed by the CPU and the
POP method returns value that is stored to predefined memory.
(5)Controls are returned to the caller function.
•Say, time required to execute steps (1) (2) (3) (5) that is time taken
for calling a function is T1 and time for execution of function is T2
•If the program code is large and complex then T1 is insignificant in
comparison to T2.
•But for small functions T1 is often much more than T2. Because the
execution time of small function is less than the switching time,
•C++ provides inline function to reduce the overhead of time T1.

INLINE FUNCTION
•Inline function is expanded inline when it is called, the whole code
of inline function gets inserted at the point of the function call. This
is done by the compiler at compiler time.
•Variable in inline function uses registers so calling and returning
overhead of variables decreases.
inline function-name()
{
function body
}
Example: inline double cube (double a)
{
return(a*a*a);
}
The above inline function can be invoked by statements like
c=cube(3.0);
d=cube(2.5+1.5);

INLINE FUNCTION
•The inline keyword merely sends a request, not a compulsory
command to the compiler.
•The compiler may ignore this request if the function definition is too
long or too complicated and compile the function as a normal function.
•It is possible that the compiler does not inline the functions marked as
inline by the programmer. Simultaneously, the compiler can decide to
inline a function even if it is not marked as inline.
•Functions that are declared and defined inside a class need not be
defined as inline functions explicitly since they are already treated as
inline functions by default.

INLINE FUNCTION
•Some of the situations where Compiler may not grant the request for
in-lining and inline expansion may not work are:
1.For functions returning values, if a loop, a switch or a go to exists.
2.for functions not returning values, if a return statement exists.
3.if functions contain static variables.
4.if inline functions are recursive
•Inline expansion makes a program run faster because the overhead of
a function call and return is eliminated.
•However, it makes the program to take up more memory because the
statements that defines the inline function are reproduced at each
point where the function is called. So a trade-off becomes necessary.

EXAMPLE: INLINE FUNCTION
#include<iostream.h>
#include<stdio.h>
inline float mul(float x, float y)
{
return(x*y);
}
inlinedouble div(double p,doubleq)
{
return(p/q);
}
main( )
{
float a=12.345;
float b=9.82;
cout<<mul(a,b)<<endl;
cout<<div (a,b)<<endl;
}

DIFFERENCE BETWEEN INLINE AND MACRO IN C++
Inline: An inline function is a normal function that is
defined by the inline keyword. An inline function is
a short function that is expanded by the compiler.
And its arguments are evaluated only once. An
inline functions are the short length functions that
are automatically made the inline functions without
using the inline keyword inside the class.
#include <iostream>
using namespace std;
//Inline function
inline int Maximum(int a, int b)
{
return (a > b) ? a : b;
}
int main()
{
cout << "Max (100, 1000):" << Maximum(100, 1000) << endl;
cout << "Max (20, 0): " << Maximum(20, 0) << endl;

return 0;
}
#include <iostream>
using namespace std;
//Macro with parameter
#define MAXIMUM(a, b) (a > b) ? a : b
int main()
{
cout << "Max (100, 1000):";
int k = MAXIMUM(100, 1000);
cout << k << endl;
cout << "Max (20, 0):";
int k1 = MAXIMUM(20, 0);
cout << k1;
return 0;
}
Macro: It is also called preprocessors
directive. The macros are defined by the
#define keyword. Before the program
compilation, the preprocessor examines
the program whenever the preprocessor
detects the macros then preprocessor
replaces the macro by the macro
definition.

Inline Macro
An inline function is defined by the inline
keyword.
Whereas the macros are defined by the #define
keyword.
Through inline function, the class’s data
members can be accessed.
Whereas macro can’t access the class’s data
members.
In the case of inline function, the program can
be easily debugged.
Whereas in the case of macros, the program
can’t be easily debugged.
In the case of inline, the arguments are
evaluated only once.
Whereas in the case of macro, the arguments
are evaluated every time whenever macro is
used in the program.
In C++, inline may be defined either inside the
class or outside the class.
Whereas the macro is all the time defined at the
beginning of the program.
In C++, inside the class, the short length
functions are automatically made the inline
functions.
While the macro is specifically defined.
Inline is not as widely used as macros. While the macro is widely used.
Inline function is terminated by the curly brace
at the end.
While the macro is not terminated by any
symbol, it is terminated by a new line.
DIFFERENCE BETWEEN INLINE AND MACRO IN C++

DEFAULT ARGUMENTS
•C++ allows us to call a function with out specifying all its arguments.
•In such cases, the function assigns a default value to the parameter
which does not have a matching arguments in the function call.
•Default values are specified when the function is declared. The
compiler looks at the prototype to see how many arguments a function
uses and alerts the program for possible default values.
Example: float amount (float principle, int period, float rate=0.15);
•The default value is specified in a manner syntactically similar to a
variable initialization. The above prototype declares a default value of
0.15 to the argument rate.
value=amount(5000,7); //one argument missing
A subsequent function call like the above passes the value of 5000 to
principle and 7 to period and then lets the function, use default value of
0.15 for rate.

DEFAULT ARGUMENTS
The call:- value=amount(5000,5,0.12); //no missing argument passes
an explicit value of 0.12 rate.
•Only the trailing arguments can have default values. That is, we must
add default from right to left. We cannot provide a default to a
particular argument in the middle of an argument list.
Example:-
int mul(int i, int j=5,int k=10);
int mul(int i=0,int j,int k=10);
int mul(int i=5,int j);
int mul(int i=2,int j=5,int k);
•Default arguments are useful in situation whose some arguments
always have the some value.

CONSTANT ARGUMENTS
•In C++, an argument to a function can be declared as unit as const as
shown below.
int strlen(const char *p);
int length(const string &s);
•The qualifier const tells the compiler that the function should not
modify the argument.
•The compiler will generate an error when this condition is violated.
•This type of declaration is significant only when we pass arguments
by reference or pointers.

FUNCTION OVERLOADING
•Overloading refers to the use of the same thing for different
purposes.
•Function overloading means that we can use the same function
name to create functions that perform a variety of different tasks.
This is known as function polymorphism in oops.
•Using the concepts of function overloading, a family of functions
with one function name but with different argument lists can be
created.
•The function would perform different operations depending on the
argument list in the function call.
•The correct function to be invoked is determined by checking the
number and type of the arguments but not on the function type.
•For example an overloaded add() function handles different types of
data as shown below.

FUNCTION OVERLOADING
//Declaration
int add(int a, int b); //prototype 1
int add (int a, int b, int c); //prototype 2
double add(double x, double y); //prototype 3
double add(double p , double q); //prototype 4
//function call
cout<<add(5,10); //uses prototype 1
cout<<add(15,10.0); //uses prototype 4
cout<<add(12.5,7.5); //uses prototype 3
cout<<add(5,10,15); //uses prototype 2
cout<<add(0.75,5); //uses prototype 5
A function call first matches the prototype having the same no and type
of arguments and then calls the appropriate function for execution.

EXAMPLE
#include<iostream>
#include<stdio.h>
using namespace std;
int area (int);
int area (int, int);
int main()
{
cout<<"square="<<area(5)<<endl;
cout<<"rectangle="<<area(3,4)<<endl;
return 0;
}
int area(int s)
{
return (s*s);
}
int area(int a, int b)
{
return(a*b);
}