0% found this document useful (0 votes)
76 views32 pages

Drawbacks of Procedural Language

The document discusses key concepts in object-oriented programming including classes, objects, encapsulation, data abstraction, inheritance, polymorphism, and reusability. It notes that object-oriented programming focuses on data and grouping related data and functions into logical units called classes. Classes can be reused and derived from using inheritance to create new classes. Functions can be overloaded to have the same name but different parameters.

Uploaded by

Divyanshu Kapoor
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views32 pages

Drawbacks of Procedural Language

The document discusses key concepts in object-oriented programming including classes, objects, encapsulation, data abstraction, inheritance, polymorphism, and reusability. It notes that object-oriented programming focuses on data and grouping related data and functions into logical units called classes. Classes can be reused and derived from using inheritance to create new classes. Functions can be overloaded to have the same name but different parameters.

Uploaded by

Divyanshu Kapoor
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Drawbacks of Procedural

Language
• Less importance to data, and more importance
to procedure
• Data is exposed, so it can be changed
accidentally
• The procedural approach is not similar to our
visualization of our system
• Procedural modules are not independent of each
other. It hampers its independent and
simultaneous development and reusability
• New data types can’t be created
Characteristics of OO Methodology
• CLASS & OBJECT
• Data and the instructions operating on that
data are enclosed in a logical shell. It’s
called a class
• An instance of a class is called an object
• To put the data and the related function
together is more logical
Encapsulation & Data Hiding
• The data and the function operating on that data
are enclosed in one shell, it’s called
encapsulation
• Outside this shell the data is not visible, only
functions are visible and accessible
• The member data are hidden from the outside
world and user, so it can’t be changed
accidentally like in procedural language
• * Data hiding and data security are different
aspects
Data Abstraction
• Abstraction means hiding of details. Hiding
always provides simplicity. The hiding may
be at three levels:
• Hiding of instruction level details
• Hiding of data level details
• Hiding of both details
Inheritance
• Inheritance is a programming technique
used to construct specialized classes from
existing classes

A
Super Class / Parent Class / Base Class

B C Sub class/ Child class/ Derived class


Reusability
• A class is an independent entity so it can be
used in many related application after its
development
• The concept of inheritance provides the facility
of generating new classes with the help of
existing classes without affecting the original
class
• Both the above concepts provides reusability.
Reusability saves time and effort both
Polymorphism
• Polymorphism means to assign more than
one task to one name
• The selection of task is decided depending
on the context
• The polymorphism is provided in two
ways:
• Function overloading
• Operator overloading
Creation of new data types
• A class is a data type, because it contains the
characteristics and the possible operations both
• Creation of new data type provides flexibility to
the user
• When a class ‘stack’ is prepared, we get a new
data type called ‘stack’. The data members are
its characteristics and the member functions are
the possible operations on it
A program in C++
• Program to find the area of circle:
# include <iostream.h>
int main()
{
float rad,area;
const float PI=3.14; //constant declaration
cout<< endl<<“Enter radius:”;
cin>> rad; // read data
area= PI * rad * rad; //calculate
cout<< endl<< “The area is: “ << area; // display area
return 0;
}
Note
• Single line comments by // {double slash}

• “iostream” is a header file. It contains the “iostream” class

• ‘cin’ and ‘cout’ are the predefined objects of this class, i.e. , iostream.h,
These are not functions

• ‘>>’, ‘<<‘ are extraction & insertion operator, These are basically functions

• In C++, we declare constant by using const


qualifier. It is more useful as compared to #define pre-processor statements;
because the identifier has a data type and memory space is allocated for it

• ‘endl’ – manipulator. It performs 2 tasks:-


Inserts a new line character ‘\n’
Flushes the o/p stream
Reference Variable
• A reference variable is an alias for an existing
variable
int x; // x is a variable
int& alias_x=x; // alias_x is a reference
To create a reference we simply put ‘&’ sign and
equate it to an existing variable of same data
type
Note: the ‘=‘ operator is not ‘assignment’
operator here and ‘&’ is not ‘address of’ operator
Reference Variable
Location is same for both
#include <iostream.h>
x
int main()
{ xx
int x;
int& xx=x; //xx is a reference of x
x=10;
cout<<endl<<x<<xx; //both are 10
xx++;
cout<<endl<<x<<xx; //both are 11
return 0;
}
Comparison between Reference
and Pointers
• Similarities
• Reference and pointers both can be
passed to and return from a function
• An identifier can have any number of
references or pointers
Comparison…
• Differences
A reference is a logical alternative name for
a variable. It does not occupypyany spacey
int x; <100>>
<<100>>
int& xx=x; // a reference
x
int y;
xx
int* py=&y; // py is a pointer to y
Comparison…
A reference must be initialized at place of declaration. Its
declaration cannot be deferred. The initialization of a
pointer can be deferred

int x;
int& xx;
xx=x; // Error: not possible
/////////////////////////////////////
int y;
int* py;
py=&y; // No Error: used frequently
Comparison…
• A reference once created and bound to a
variable, cannot be reinitialized to another
variable. A pointer can be reinitialized any time
int x,y;
int& xy=x; // a reference
xy=y; // not possible; only value of y will
// be copied, reference will not be changed
//////////////////////////////////////////////////////
int x,y;
int * pxy=&x; //p xy is a pointer to x
pxy=&y; // NO ERROR: used frequently
Comparison…
• There is no concept of NULL reference.
There exist NULL pointers
• A function returning a reference can
appear on the left hand side of the
assignment operator. There is no such
concept with pointers
Questions???
Comment about the follwing statements:
int& x=10;
const int& y=10;
Pass-by-Reference Parameter
Passing
#include <iostream.h>
void change(int&,int); //prototype
int main()
{
int a=10;
b=20;
change(a,b); //function called
cout<<endl<<a<<b; //a=11, b=20
return 0;
}

/*function to change two parameters,*/


void change(int& x, int y)
{
cout<<endl<<x<<y; //x=10, y=20
x++;
y++;
cout<<endl<<x<<y; //x=11, y=21
}
Advantages of Pass-by-Reference
• It saves space, because no extra space is
reserved for reference parameter
• It saves time, because there is no data
copying from calling function to called
function
Disadvantages of Pass-by-
Reference
• Pass-by-reference parameters increases
the chance of error in the program
• During debugging, it becomes tough to
identify the place of error
Function Overloading
• Same function name can be used for more than
one function definitions. It is a type of
polymorphism. The advantage is that the user
needs less function names to remember
• The overloading can be done in 3 different ways:
• A) Different number of arguments
• B) Different kind of arguments
• C) Different number and kind of arguments
Function Overloading Example…
#include<iostream.h>
// the complex structure
struct complex
{
float real;
float img;
};
// prototypes
void display(int);
void display(complex);
int main()
{
int x=10;
complex c;
c.real=5.0;
c.img=7.0;
display(x); //function to display integer is called
display(c); //function to display complex number is called
return 0;
}
// 1st function to display integer
void display(int a)
{
cout<<endl<<"Integer is:"<<a;
}
//2nd funciton to display complex number
void display(complex cc)
{
cout<<endl<<"Real Part:"<<cc.real

<<"imaginary Part:"<<cc.img;
}
Movie Example (tute)
#include<iostream.h>
#include<conio.h>
void printMovie(char arr[10], int rtime);
void printMovie(char arr[3]);

void main()
{
printMovie("hello",5);
printMovie("hello");

void printMovie(char arr[10], int rtime)


{
cout<<arr;
cout<<rtime;
}

void printMovie(char arr[3])


{
cout<<arr;
cout<<10;
}
Default Arguments
• Sometimes a pre-decided value for a
parameter is assigned, if the argument
corresponding to the parameter is not
supplied at the place of call
Default argument example…
#include<iostream.h>
float power(float xx, int nn=2); //prototype,

//default value of nn is 2
int main()
{float x;
int n;
x=5.0; n=3;
cout<<power(x,n); //5.03=124.0; default value not used
cout<<power(x); // 5.02=25.0; default value used; n=2
return 0;
}
/* Function to calculate power input
input: x and n
output: x to the power n
*/
float power(float xx, int nn)
{
float res=1.0;
for(int i=0;i<nn;i++) //loop to calculate power
res *=xx;
return res;
}
Facts…
• Default arguments are type checked at the
time of function declaration and evaluated
at the time of call. So, the default value is
given to the prototype declaration
• Default value can only be provided to the
trailing arguments
For example:
Float power(float xx=10, int nn); //Error
Example…

void sample(int x, int y=10, int z=20);

Following are the calls and their resolution


• Sample(5,25,12); // x=5, y=25, z=12
• Sample(5,25); // x=5, y=25, z=20
• Sample(5); // x=5, y=10, z=20
Default Argument Example(Tute)
#include<iostream.h>
#include<conio.h>
void printMovie(char arr[10], int rtime=20);

void main()
{
printMovie("hello",5);
printMovie("hello");

void printMovie(char arr[10], int rtime)


{
cout<<arr;
cout<<rtime<<endl;
}
Returning Reference from function
• A reference of an identifier can be returned from a function

#include<iostream.h>
int& sum(int x,int y);
int main()
{
int a=10, b=20;
int& s=sum(a,b);
cout<<endl<<“The sum is:”<<s;
return 0;
}
//function returning reference of a variable
int& sum(int x, inty)
{
int z;
z=x+y;
return z;
}
Output????
Program has an error???????

Yes, in main(), the variable s is an alias of z; but, z


does not exist

We can never return the reference of a local


variable (like pointers)

There are two solution:


(1) Take z as global variable
(2) Take z as static variable
Function call on the left hand side
of assignment operator
#include<iostream.h>
int x; //global variable for returning reference
int& setx(void); //prototype
int main()
{
setx()=100;
cout<<endl<<x;
return 0;
}
//function returning reference
int& setx()
{
return x; //returning reference
}

Note: This facility will be used to overlaod subscript ‘[ ]’ operator

You might also like