Download - Oops Intro
-
8/6/2019 Oops Intro
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 andsimultaneous development and reusability
New data types cant be created
-
8/6/2019 Oops Intro
2/32
Characteristics of OO Methodology
CLASS & OBJECT
Data and the instructions operating on that
data are enclosed in a logical shell. Itscalled a class
An instance of a class is called an object
To put the data and the related functiontogether is more logical
-
8/6/2019 Oops Intro
3/32
Encapsulation & Data Hiding
The data and the function operating on that dataare enclosed in one shell, its calledencapsulation
Outside this shell the data is not visible, onlyfunctions are visible and accessible
The member data are hidden from the outsideworld and user, so it cant be changed
accidentally like in procedural language * Data hiding and data security are different
aspects
-
8/6/2019 Oops Intro
4/32
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
-
8/6/2019 Oops Intro
5/32
Inheritance
Inheritance is a programming technique
used to construct specialized classes from
existing classes
B
A
C
SuperClass / Parent Class / Base Class
Sub class/ Child class/ Derived class
-
8/6/2019 Oops Intro
6/32
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
-
8/6/2019 Oops Intro
7/32
Polymorphism
Polymorphism means to assign more than
one task to one name
The selection of task is decided dependingon the context
The polymorphism is provided in two
ways:
Function overloading
Operator overloading
-
8/6/2019 Oops Intro
8/32
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 arethe possible operations on it
-
8/6/2019 Oops Intro
9/32
A program in C++
Program to find the area of circle:
# include
int main()
{
float rad,area;
const float PI=3.14; //constant declaration
cout
-
8/6/2019 Oops Intro
10/32
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
>>,
-
8/6/2019 Oops Intro
11/32
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
typeNote: the = operator is not assignment
operator here and & is not address of operator
-
8/6/2019 Oops Intro
12/32
Reference Variable
#include
int main()
{
int x;
int& xx=x; //xx is a reference of x
x=10;
cout
-
8/6/2019 Oops Intro
13/32
-
8/6/2019 Oops Intro
14/32
Comparison
Differences
A reference is a logical alternative name for
a variable. It does not occupy any spaceint x;
int& xx=x; // a reference
int y;int* py=&y; // py is a pointer to y
x
py
>
y
xx
-
8/6/2019 Oops Intro
15/32
Comparison
A reference must be initialized at place of declaration. Itsdeclaration cannot be deferred. The initialization of apointer can be deferred
int x;
int& xx;
xx=x; // Error: not possible
/////////////////////////////////////
int y;
int* py;
py=&y; // No Error: used frequently
-
8/6/2019 Oops Intro
16/32
Comparison
A reference once created and bound to avariable, cannot be reinitialized to anothervariable. 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
-
8/6/2019 Oops Intro
17/32
Comparison
There is no concept of NULL reference.
There exist NULL pointers
A function returning a reference canappear on the left hand side of the
assignment operator. There is no such
concept with pointers
-
8/6/2019 Oops Intro
18/32
Questions???
Comment about the follwing statements:
int& x=10;
const int& y=10;
-
8/6/2019 Oops Intro
19/32
Pass-by-Reference Parameter
Passing#include void change(int&,int); //prototype
int main()
{
int a=10;
b=20;
change(a,b); //function called
cout
-
8/6/2019 Oops Intro
20/32
Advantages of Pass-by-Reference
It saves space, because no extra space is
reserved for reference parameter
It saves time, because there is no datacopying from calling function to called
function
-
8/6/2019 Oops Intro
21/32
Disadvantages of Pass-by-
Reference
Pass-by-reference parameters increases
the chance of error in the program
During debugging, it becomes tough toidentify the place of error
-
8/6/2019 Oops Intro
22/32
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
-
8/6/2019 Oops Intro
23/32
Function Overloading Example
#include
// 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
-
8/6/2019 Oops Intro
24/32
Movie Example (tute)
#include#includevoid 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
-
8/6/2019 Oops Intro
25/32
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
-
8/6/2019 Oops Intro
26/32
Default argument example#include
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
-
8/6/2019 Oops Intro
27/32
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
-
8/6/2019 Oops Intro
28/32
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
-
8/6/2019 Oops Intro
29/32
Default Argument Example(Tute)
#include
#include
void printMovie(char arr[10], int rtime=20);
void main()
{printMovie("hello",5);
printMovie("hello");
}
void printMovie(char arr[10], int rtime)
{cout
-
8/6/2019 Oops Intro
30/32
Returning Reference from function
A reference of an identifier can be returned from a function
#include
int& sum(int x,int y);
int main()
{
int a=10, b=20;int& s=sum(a,b);
cout
-
8/6/2019 Oops Intro
31/32
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
-
8/6/2019 Oops Intro
32/32
Function call on the left hand side
of assignment operator#include
int x; //global variable for returning reference
int& setx(void); //prototype
int main()
{
setx()=100;cout