Unit 1
Unit 1
Introduction:
Object-oriented programming – As the name suggests uses objects in programming.
Object-oriented programming aims to implement real-world entities like inheritance,
hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the
data and the functions that operate on them so that no other part of the code can access
this data except that function.
Characteristics of an Object Oriented Programming language
It is a programming language
that is derived from structure Object-oriented programming
programming and based upon is a computer programming
the concept of calling design philosophy or
1. Definition procedures. It follows a step-by- methodology that organizes/
step approach in order to break models software design around
down a task into a set of data or objects rather than
variables and routines via a functions and logic.
sequence of instructions.
It follows a bottom-up
3. Approach It follows a top-down approach.
approach.
In procedural programming,
In OOP, objects can move and
Data data moves freely within the
4. communicate with each other
movement system from one function to
via member functions.
another.
It is structure/procedure-
5. Orientation It is object-oriented.
oriented.
In Procedural programming, a
In OOP, a program is divided
Program program is divided into small
14. into small parts that are
division programs that are referred to as
referred to as objects.
functions.
• Class
• Objects
• Encapsulation
• Data Hiding
• Abstraction
• Inheritance
• Overloading
• Polymorphism
• Abstraction
Class − A class is a data-type that has its own members i.e. data members and member
functions. It is the blueprint for an object in objects oriented programming language. It is
the basic building block of object oriented programming in C++. The members of a class
are accessed in programming language by creating an instance of the class.
Some important properties of class are −
• Class is a user-defined data-type.
• A class contains members like data members and member functions.
• Data members are variables of the class.
• Member functions are the methods that are used to manipulate data members.
• Data members define the properties of the class whereas the member functions
define the behavior of the class.
A class can have multiple objects which have properties and behaviour that in common
for all of them.
Syntax
class <class_name>
{
< data_type> < data_name>;
< return_type>< method_name>(parameters);
}
Message Passing: Objects communicate with one another by sending and receiving
information to each other. A message for an object is a request for execution of a
procedure
Structure of C++ program with simple C++ program:
Example:
//Program Name: First C++ Program
/*
Version: 1.0
Author: @noname
Date Created:21-02-2018*/
Link Preprocessor :- These preprocessor directive tell the compiler to link the
functions from system library.
Example:
#include <iostream>
Definition Preprocessor :- This the section where we define all our symbolic constant
we gonna use in our program.
Example:
#define PI 3.142
#define TRUE 1
Global Declarations :- This is the section where all the global declaration comes. All of
the variables, structures, classed and function defined or declared outside the main
function are treated as global.
Class Definition :- The classes are used to map real world entities into programming.
The Classes are the key building block of any C++ program. A C++ program may include
several class definitions. This is the section where we define all of our classes.
Main Function Definition : - This is the most vital part of each and every C++ program,
it is mandatory for C++ program to have a main() method definition. There can be only
one main() method in C++ program. The execution of a C++ program starts with the
main() method. The C++ program can not be executed without the main() method. The
main() method is responsible for the execution of all the user defined statement,
functions and library functions. The main() method further structured into – variable
declaration, function declaration and user defined executable statements.
User defined functions :- This is the section of where we put all the user defined
functions created to perform a specific task. A user defined function must be defined
before use it. User defined function can be written before or immediately after the main (
) function and called inside the main ( ) function.
Control Structures are just a way to specify flow of control in programs. Any
algorithm or program can be more clear and understood if they use self-contained
modules called as logic or control structures. It basically analyzes and chooses in which
direction a program flows based on certain parameters or conditions. There are three
basic types of logic, or flow of control, known as:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow
Let us see them in detail:
1. Sequential Logic (Sequential Flow)
Sequential logic as the name suggests follows a serial or sequential flow in which the
flow depends on the series of instructions given to the computer. Unless new
instructions are given, the modules are executed in the obvious sequence. The
sequences may be given, by means of numbered steps explicitly. Also, implicitly
follows the order in which modules are written. Most of the processing, even some
complex problems, will generally follow this elementary flow pattern.
}
else
{
Iteration Logic (Repetitive Flow): The Iteration logic employs a loop which involves a
repeat statement followed by a module known as the body of a loop.
While
initialization;
While(condition)
{
Body of the loop;
Updation;
}
Repeat While Flow
In this, there requires a statement that initializes the condition controlling the loop, and
there must also be a statement inside the module that will change this condition leading
to the end of the loop.
Syntax of Function
return_type function_name (parameter_list)
{
//C++ Statements
}
Let’s take a simple example to understand this concept.
A simple function example
#include <iostream>
using namespace std;
/* This function adds two integer values
* and returns the result
*/int
sum(int num1, int num2){
int num3 = num1+num2; return num3;
}
int main(){
//Calling the function
cout<<sum(1,99);
return 0;
}
Output:
100
The same program can be written like this: Well, I am writing this program to let you
understand an important term regarding functions, which is function declaration. Lets
see the program first and then at the end of it we will discuss function declaration,
definition and calling of function.
#include <iostream>
using namespace std;
//Function declaration
int sum(int,int);
//Main function
int main(){
//Calling the function
cout<<sum(1,99);
return 0;
}
/* Function is defined after the main method
*/
int sum(int num1, int num2){
int num3 = num1+num2;
return num3;
}
Function Declaration: You have seen that I have written the same program in two
ways, in the first program I didn’t have any function declaration and in the second
program I have function declaration at the beginning of the program. The thing is that
when you define the function before the main() function in your program then you don’t
need to do function declaration but if you are writing your function after the main()
function like we did in the second program then you need to declare the function first,
else you will get compilation error.
Function definition: Writing the full body of function is known as defining a function.
syntax of function definition:
return_type function_name(parameter_list) {
//Statements inside function
}
Calling function: We can call the function like this:
function_name(parameters);
Now that we understood the working of function, lets see the types of function in C++
Types of function
1) Built-in functions
Built-in functions are also known as library functions. We need not to declare and define
these functions as they are already written in the C++ libraries such as iostream, cmath
etc. We can directly call them when we need.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
/* Calling the built-in function
* pow(x, y) which is x to the power y
* We are directly calling this function
*/
cout<<pow(2,5);
return 0;
}
Output:
32
2) User-defined functions
We have already seen user-defined functions, the example we have given at the
beginning of this note is an example of user-defined function. The functions that we
declare and write in our programs are user-defined functions. Lets see another example
of user-defined functions.
User-defined functions
#include <iostream>
#include <cmath>
using namespace std;
//Declaring the function sum
int sum(int,int);
int main()
{
int x, y;
cout<<"enter first number: ";
cin>> x;
Function Declaration
return_type function_name( param_1,param_2 ... param_n );
main() Function in C++
main() function is the entry point of any C++ program. It is the point at which
execution of program is started. When a C++ program is executed, the execution
control goes directly to the main() function. Every C++ program have a main()
function.
Void main()
{
……………………
……………………..
}
In above syntax;
• void: is a keyword in C++ language, void means nothing, whenever we use void as
a function return type then that function nothing return. here main() function no
return any value.
• In place of void we can also use int return type of main() function, at that time
main() return integer type value.
• main: is a name of function which is predefined function in C++ library.
#include<iostream>
using namespace std;
void main()
{
cout<<"This is main function";
}
Output
This is main function
Any changes made to an inline function will require the inline function to be recompiled
again because the compiler would need to replace all the code with a new code;
otherwise, it will execute the old functionality.
Inside the main() method, when the function fun1() is called, the control is transferred
to the definition of the called function. The addresses from where the function is called
and the definition of the function are different. This control transfer takes a lot of time
and increases the overhead.
When the inline function is encountered, then the definition of the function is copied to
it. In this case, there is no control transfer which saves a lot of time and also decreases
the overhead.
#include <iostream>
using namespace std;
inline int add(int a, int b)
{
return(a+b);
}
int main()
{
cout<<"Addition of 'a' and 'b' is:"<<add(2,3);A
return 0;
}
Once the compilation is done, the code would be like as shown as below:
#include<iostream>
using namespace std;
inline int add(int a, int b)
{
return(a+b);
}
int main()
{
cout<<"Addition of 'a' and 'b' is:"<<return(2+3);
return 0;
}
The main use of the inline function in C++ is to save memory space. Whenever the
function is called, then it takes a lot of time to execute the tasks, such as moving to the
calling function. If the length of the function is small, then the substantial amount of
execution time is spent in such overheads, and sometimes time taken required for
moving to the calling function will be greater than the time taken required to execute
that function.
The solution to this problem is to use macro definitions known as macros. The
preprocessor macros are widely used in C, but the major drawback with the macros is
that these are not normal functions which means the error checking process will not be
done during the compilation.
C++ has provided one solution to this problem. In the case of function calling, the time
for calling such small functions is huge, so to overcome such a problem, a new concept
was introduced known as an inline function. When the function is encountered inside
the main() method, it is expanded with its definition thus saving time.
o If a function is recursive.
o If a function contains a loop like for, while, do-while loop.
o If a function contains static variables.
o If a function contains a switch or go to statement
o The variables that are created inside the inline function will consume additional registers.
If the variables increase, then the use of registers also increases, which may increase the
overhead on register variable resource utilization. It means that when the function call is
replaced with an inline function body, then the number of variables also increases,
leading to an increase in the number of registers. This causes an overhead on resource
utilization.
o If we use many inline functions, then the binary executable file also becomes large.
o The use of so many inline functions can reduce the instruction cache hit rate, reducing
the speed of instruction fetch from the cache memory to that of the primary memory.
o It also increases the compile-time overhead because whenever the changes are made
inside the inline function, then the code needs to be recompiled again to reflect the
changes; otherwise, it will execute the old functionality.
o Sometimes inline functions are not useful for many embedded systems because, in some
cases, the size of the embedded is considered more important than the speed.
o It can also cause thrashing due to the increase in the size of the binary executable file. If
the thrashing occurs in the memory, then it leads to the degradation in the performance
of the computer.