You are on page 1of 11

Procedures, also known as routines, subroutines, or functions, simply contain a series of

computational steps to be carried out. Any given procedure might be called at any point
during a program's execution, including by other procedures or itself.

Modularity is generally desirable, especially in large, complicated programs. Inputs are


usually specified syntactically in the form of arguments and the outputs delivered as
return values.

Scoping is another technique that helps keep procedures modular. It prevents the
procedure from accessing the variables of other procedures (and vice versa).

Because of the ability to specify a simple interface, to be self-contained, and to be reused,


procedures are a convenient vehicle for making pieces of code written by different people
or different groups, including through programming libraries.

The design method used in procedural programming is called Top Down Design. This
is where you start with a problem (procedure) and then systematically break the problem
down into sub problems (sub procedures). This is called functional decomposition,
which continues until a sub problem is straightforward enough to be solved by the
corresponding sub procedure.
When changes are made to the main procedure (top), those changes can cascade to the
sub procedures of main, and the sub-sub procedures and so on, where the change may
impact all procedures in the pyramid.
In C, a data member must be declared GLOBAL in order to make it accessible by all
functions in the program.
What happens when 2 or more functions work on the same data member?
If there are 10 functions in a program, all these 10 functions can access a global data
member. It is possible one function may accidentally change values of this global data
member. If this data member is a key element of the program, any such accidental
manipulation will affect the whole program. It will be too difficult to debug & identify which
function is causing the problem if the program is really big.
Advantages of Procedural Programming:
Its relative simplicity, and ease of implementation of compilers and interpreters.
The ability to re-use the same code at many places in the program without copying it.
An easier way to keep track of program flow.
Disadvantages of Procedural Programming:
Data is exposed to whole program, so no security for data.
Difficult to relate with real world objects.
Difficult to create new data types reduces extensibility.
Importance is given to the operation on data rather than the data.
Characteristics of Procedural Oriented Programming
1. Emphasis is on procedure.
2. Large programs are divided into smaller programs know as functions.
3. Most of the functions share global data.
4. Data moves openly around from function to function.
5. Functions transform data from one form to another.
6. Employs top-down approach in program design
OBJECT ORIENTED PROGRAMMING:
It is an approach that provides a way of modularizing programs by creating
partitioned memory are area for both data and functions that can be used as
templates for creating copies of such module on demand,

Characteristics of Object Oriented Programming:

1. Emphasis is on data rather than procedure.


2. Programs are divided into objects.
3. Data structures are designed such that they characterize the objects.
4. Function that operates on the data of an object are tied together in the data
structure.
5. Data is hidden and cannot be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added whenever necessary.
8. Follows bottom-up approach in program design.

Procedure oriented programming Object Oriented Programming


Emphasis is on algorithm. Emphasis is on data.

Large programs are divided into smaller Programs are divided into objects.
programs know as functions.

Employs top-down approach in program Follows bottom-up approach in program


design design.

Most of the functions share global data. Data is hidden and cannot be accessed by
external functions.
DATA ENCAPSULATION
1. The wrapping up of data and functions together into single unit is known as
Encapsulation.
2. Data is not accessible by external functions, and accessible only to functions
that are wrapped inside the class.
3. The member functions provide the interface between the Objects data and the
program.
4. The insulation of the data from direct access by the external function is called
DATA HIDING.
DATA ABSTRACTION:
1. The act of representing essential features without including the details is known
as abstraction.
2. Class use the concept of abstraction and are defined as list of abstract attributes
and functions that operate on these attributes.
3. The attributes are known as data members.
4. The functions that operate on data members are called member functions.

PRIVATE PUBLIC

B GET_A

ADD MEMBER

MEMBER FUNCTION

FUNCTION

FUNCTION CALL
DATA ACCESS

DATA
MAIN
Insertion Operator (<<)
It is used along with keyword “cout” to display values on screen;
EXAMPLE:
int a=5,b=10;
cout<<a<<”\t”<<b<<”\n”;
cout<<”hello”;

Extraction Operator(>>)

It is used along with keyword “cin” to accept values from user;


EXAMPLE:
int a,b;
cin>>a>>b;

STRUCTURE OF C++ PROGRAM


Include files
Class declarations
Member functions definitions
Main function program

REFERENCE VARIABLES
A reference variable provides an alternative name for previously defined variable.
General form:
datatype & reference-name = variable-name
Example:
int total=100;
int &sum=total;
If we make the variable sum a reference to the variable total,
then sum and total can be used interchangeably to represent
that variable.
Note: A reference variable must be initialized at the time of declaration.
It can be used in function call by reference mechanism in C++, because it permits the
manipulation of objects by reference, and eliminates the copying of object parameters.
#include<iostream>
using namespace std ;
int main()
{
int a=10;
void change(int&);
cout<<"\n"<<a;
change(a);
cout<<"\n"<<a;
return 0;
}

void change (int &b)


{b=b+10;}

SCOPE RESOLUTION OPERATOR (::)


Scope of a variable: It extends from point of declaration till the end of the block
containing the declaration. A variable declared inside the block is said to be local to that
block.
The scope resolution operator allow to access global version of a variable.
#include<iostream>
using namespace std ;
int m=10;
int main()
{ int m=20;
{
cout<<m<<endl;
m=m*2;
int m=30;
cout<<m<<endl;
cout<<::m<<endl;
::m=::m*3;
}
cout<<m<<endl;
cout<<::m<<endl;
return 0;
}

NOTE: A major application of scope resolution operator is in the classes to identify the
class to which member function belongs.

RETURN BY REFERENCE
A function can also return by reference
#include<iostream>
using namespace std ;
int m=10;
int main()
{
int & max(int &,int&);

int a=10,b=20;
cout<<a<<" "<<b<<endl;
max(a,b)=-1;
cout<<a<<" "<<b<<endl;
return 0;
}

int & max(int &x,int&y)


{
if(x>y) return x; else return y;

The function returns reference to x or y. Function call max(a,b) will yield a reference to
a or b depending on their values. This means that function call can appear on the left
hand side of assignment statement.

INLINE FUNCTIONS
An inline function is a function that is expanded in line when it is invoked. That is, the
compiler replaces the function call with the corresponding function code. The inline
functions are defined as follows:
Inline function-header
{
Function body
}
The functions are made inline when they are small enough to be defined in one or two
lines.
Note: The inline keyword merely sends a request, not a 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.
Situations where inline expansion may not work:
1. For functions returning values, if a loop, a switch exists.
2. For functions not returning values, if a return statements exists.
Advantage: Inline expansion makes the program run faster because the overhead of a
function call and return is eliminated.
Disadvantage: It makes the program to take up more memory because the statements
that define the inline function are reproduced at each point where the
function is called.

DEFAULT ARGUMENTS
C++ allows us to call function without specifying all its arguments.IN such cases the
function assigns a default value to the parameter which does not have a matching
argument in the function call.
Default values are specified when the function is declared.
A default argument is checked for type at the time of declaration and evaluated at the
time function call.
NOTE: Only trailing arguments can have default values
ADVANTAGES:
i) It is used to add new parameters to the existing functions.
ii) Default arguments can be used to combine similar functions into me.
#include<iostream>
using namespace std;
int main()
{
int sample(int a,int b=50,int c=30);
cout<<sample(10,20,50);
cout<<"\n"<<sample(10,20);
cout<<"\n"<<sample(10);
return 0;
}

int sample(int a,int b,int c)


{return(a+b+c);}

FUNCTION OVERLOADING

You can have multiple definitions for the same function name in the same scope. The definition
of the function must differ from each other by the types and/or the number of arguments in the
argument list.

#include<iostream>
using namespace std;
int main()
{
float area (float);
float area(float ,float);
cout<<”\nArea of a circle with radius 5 : “<<area(5);
cout<<”\nArea of a rectangle with length and breath 5 and 3 : “<<area(5,3);
return 0;
}

float area(float x)
{ return(22/7.0*x*x);}

float area(float x,float y)


{return(x*y);}
CLASSES AND OBJECTS

Class: It is a way to bind data and its associated functions together to implement data
encapsulation and Data abstraction.

By implementing a class we create a new data type.

Class specification has two parts:

1. Class Declaration: The class declaration defines the type and scope of its members.

General form of class declaration:

class class_name
{
private :
Variable declarations;
Function declarations;
public :
Variable declarations;
Function declarations;
};

/* text in blue above are keywords */

i) The class body contains the declaration of variables and functions. These
functions and variables are collectively called class members.
ii) They are usually grouped under two sections namely private and public.
iii) The keywords private and public are known as visibility labels.
iv) Class members that have been declared as private can be accessed only
from within the class. The public members can be accessed from outside the
class also.
v) The use of keyword private is optional. By default the members of the class
are private.
vi) The variables declared inside the class are known as data members and the
functions inside the class are known as member functions.
vii) Only the member functions can have access to private data members and
functions.
viii) However the public data members and public member function can be
accessed outside the class.
NOTE: by default in structures members are public.

2. Class function definition (member function definition)

Member functions can be defined in two places:

1. Outside the class definition.


2. Inside the class definition.

General form of member function defined outside the class:

return-type class-name : : function name (argument declaration)


{
Function Body
}

A member function incorporates a membership identity label in the header. This label
tells the compiler “to which class the function belongs to”. Scope of the function is
restricted to the class-name specified in the header line.

Member functions characteristics:

i) Several different classes can use the same function name. The
“membership label” will resolve there scope.
ii) Member functions can access the private data of the class.
iii) A member function can call another member function directly without
using the dot operator.

Member function defined inside the class:

When a member function is defined inside a class, it is treated as an inline


function. Therefore, all restrictions of inline functions are also applicable here.

NOTE: We can define a member function outside the class definition and
yet make it inline by just adding the keyword “inline” in the header
line of function definition.

Example:
class student
{
int roll;
public: void input();
};

inline void student :: input()


{
cin>>roll;
};

NOTE: A private member function can only be called by another function


that is the member of its class.

EXAMPLE: OBJECTS AS FUNCTION ARGUMENTS

#include<iostream>
using namespace std ;
class time
{ int hours;
int minutes;
public: void get_time() { cin>>hours>>minutes;}
void display() { cout<<hours<<" "<<minutes;}
void sum(time,time);
};

void time::sum(time t1,time t2)


{
minutes=t1.minutes +t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}

int main()
{
time x,y,z;
x.get_time();
y.get_time();
z.sum(x,y);
z.display();
return 0;
}
CONSTRUCTORS AND DESTRUCTORS

Constructor: A Constructor is a special member function whose task is to initialize the objects of
its own class. It is special because its name is the same as the class name. The constructor is
invoked whenever an object of its associated class is created.

Characteristics of Constructor:

1. The should be declared in public section.


2. They are invoked automatically when the objects are created.
3. They do not have return types not even void.
4. They can have default arguments.
5. They can be overloaded.
6. They cannot be inherited, though a derived class can call the base class constructors.

DEFAULT CONSTRUCTOR: A constructor that accepts no parameters is called default


constructor. If no such constructor is defined, then the compiler supplies a default constructor.

PARAMETERIZED CONSTRUCTOR: C++ permits us to initialize the various data members of


different objects with different values when they are created by passing arguments to the
constructor function when the objects are created.

Parameterized constructor can be called in two different ways:

1. By calling the constructor explicitly.


2. By calling the constructor Implicitly.

Example:
class student
{
int roll;
float percent ;
public: student (int a, float b) { cin>>roll>>percent;}
};

int main()
{
student z= student(1000,78.89); /*explicit call to constructor*/
student x(2000, 56.78); /* implicit call to constructor */
.
.
.
.
return 0;
}

COPY CONSTRUCTORS:

A copy constructor is used to declare and initialize an object from another object. The process
of initializing through a copy constructor is known as copy initialization. A reference variable has
been used as an argument to the copy constructor. We cannot pass argument by value to the
copy constructor. When no copy constructor is defined the complier supplies its own copy
constructor.

EXAMPLE:

class sample
{
int a,b;
public: sample() {a=10; b=20;} /* default constructor */
sample( int x, int y=99) {a=x; b=y;} /* parameterized constructor */
sample( sample &p) { a=p.a; b=p.b;} /* copy constructor*/
void display()
{ cout<<a<<” “<<b<<endl;}
};
int main()
{
sample z; /* default constructor called */
sample y(88); /* parameterized constructor called);*/
sample w(77,66); /* parameterized constructor called);*/
sample m(w); /*copy constructor called;*/
z.display();
y.display();
w.display();
m.display();
return 0;
}

DYNAMIC CONSTRUCTORS

The constructor can be used to allocate memory while creating objects which enables the
system to allocate the right amount of memory for each object when the objects are not of
same size.

Allocation of memory to objects at the time of construction is known as dynamic construction of


objects.

class sample
{
char *name;
int length;
public:
sample() { length=0; name= (char*) malloc(length+1);}
sample(char *s) { length=strlen(s); name= (char*) malloc(length+1); strcpy(name,s);}
void display() {cout<<name<<endl;}
};

int main()
{
sample s1;
sample s2(“john”);
s1.display();
s2.display();
return 0;
}

DESTRUCTORS:

A destructor is special member function whose name is same as the class name but preceded
by a tilde (~). It is used to destroy the objects that have been created by constructors.
It doesn’t take any arguments nor does it return any value.
It is invoked implicitly by the compiler upon exit from block, function and program.

You might also like