2. OBJECT ORIENTED PARADIGM
Why new Programming paradigms?
Features of Object oriented paradigm.
“Object oriented programming is designed around the
data being operated upon as opposed to the
operations themselves.”
Workstep %
Requirements 3
Design 8
Programming 7
Testing 15
Maintenance 67
3.
4. Encapsulation: It is a mechanism that associates the
code and the data it manipulates into a single unit.
Data Abstraction: The technique of creating new
data types that are well suited to an application to be
programmed.
Inheritance: It allows the extension and reuse of
existing code without having to rewrite the code from
scratch.
Multiple Inheritance: The mechanism by which a
class is derived from more than one base classes.
5. Polymorphism: It allows single name/ operator to be
associated with different operations depending on the
type of data passed to it.
Message passing: It is the process of invoking an
operation on an object.
Extensibility: It is the feature, which allows the
extension of the existing software components.
Persistence : The phenomenon where the object(
data) outlives the program execution time and exists
between executions of the program.
Delegation: An alternative to class inheritance.
Genericity: It is a technique for defining software
components that have more than one interpretation
depending on the data type of parameters.
6. Elements of Object oriented
programming.
Object oriented programming is centered around new
concepts such as objects , classes, polymorphism,
inheritance etc.
It is best suited for:
Modeling real world problem as close as possible to the
user’s perspective.
Constructing reusable software components and easily
extendable libraries.
Easily modifying and extending implementations of
components without having to recode .
7. “OOP is a method of implementation in which
programs are organized as co-operative
collections of objects, each of which represents an
instance of some class and whose classes are all
members of a hierarchy of classes united through
the property called inheritance.”
Grady Booch.
8. Objects
Entities which have some physical or conceptual
boundaries that separate them from the rest of the
problem.
12. Merits and Demerits of OO
Methodology
Easy to trap existing pattern of human thought into
programming.
Greater modularity
The independence of each object eases the development
and maintenance of the program.
Information hiding and data abstraction increase
reliability .
Many OO languages provide a standard class library that
can be extended by the users.
13. Disadvantages:
Compiler overhead
Runtime overhead
Re-orientation of software developer to object oriented
thinking.
Requires mastery over the following areas
Software Engineering
Programming methodologies
Benefits only in the long run while managing large software
projects .
14. Hello World
Output Streams
cout<<variable;
<< is called the insertion operator.
Input streams
cin>>variable
>> is called the extraction operator
15. Literals –constant Qualifiers
Literals are constants, to which symbolic names are
associated for the purpose of readability.
Three ways of defining constants
#define
Enumerated data types
const keyword.
16. .
Scope resolution variable ::
Scope resolution operator permits a program to
reference an identifier in the global scope that has
been hidden by another identifier with same name in
the local scope.
Example
Variable Definition at point of use:
for( int i =1;i<10; i++)
18. Inline Functions
inline returntype functionname(parameters)
{
//body of the main function
}
Example
19. Function Overloading
Two or more functions can be given same names
provided they have different signatures.
swap(int, int);
swap( float, float);
21. Default arguments
In C++ function call, when one or more of the
arguments are omitted, the function may be defined to
take default value for the omitted arguments by
providing the default value in the function prototype.
Example
22. .
Write a program to calculate simple interest using a
function which takes default argument for rate of
interest as 10%.
Format the output as follows
---------------------------------------------------------------
PRINCIPAL AMOUNT YEAR RATE INTEREST
----------------------------------------------------------------
XXXXXXX XXXXXX XXXX XX XXXXXX
----------------------------------------------------------------
Use inline function to draw the required lines.
23. Data Types and Sizes
Primary
Derived data types
User- defined data types
Qualifiers- Size
Sign
sizeof operator
Relational operator, Logical Operator
Bitwise operator
Conditional Operator(Ternary Operator)
Expression?epxression2 :expression3
Typedef statement
24. Constants
Integer constant – Octal , Decimal, Hexadecimal
Character constants ‘A’ and “A”
Enumerated data type:
An enumerated data type is a user defined type with
values ranging over a finite set of identification called
enumeration constants.
enum color(red, blue, green);
Example
25. Control Flow
Branching Statements
If statement
If- else
Switch
goto
Looping Statements
For
While
Do - while
27. Arrays and strings
An array is a group of logically related data items of the
same data type addressed by a common name and
stored in contiguous memory locations.
Multi dimensional array s
A multi dimensional array is defined as follows
Datatype array name[s1][s2]…..[sn]
Ex: int marks[4][3]
30. Strings
Strings are used to store and manipulate text. It is
represented as an array of characters and the end of
the string is marked by the NULL (‘0’) character.
Initialization
Char name[] ={‘s’, ’a’, ‘m’, ’0’};
Char name[] =“sam”;
31. String manipulators
strlen();
strcpy(destination, source);
strcat();
strcmp();
<0 first string is less than the second
==0 equal
>0 first string is greater than the second.
Strupr(string);
strlwr(string);
33. Function prototype:
It provides the following information to the compiler
The name of the function
The type of the value returned
The number and types of the arguments that must be
supplied to call the function.
34. Function definition
The function itself is referred to as function
definition.
The first line is known as function declarator and the
is followed by the function body.
35. Parameter passing
Parameter passing is a mechanism for communication
of data and information between the calling function
(caller) and the called function(callee).
C++ Supports the three types of parameter passing
schemes.
Pass by value
Pass by Address
Pass by reference
37. Recursive function
A function that contains a function call to itself or a
function call to a second function which eventually
calls the first function is called as recursive function.
Two conditions which must be satisfied by any
recursive function are:
Each time a function calls itself it must be nearer , in
some sense to a solution.
There must be decision criterion for stopping the
process or computation.
Example
38. Function and arrays
Example
Arrays can be passed to function by address or by
reference.
39. Storage Classes
The period of time during which memory is associated
with a variable is called the extent of the variable and
is characterized by storage classes.
Types of storage classes
auto
register
extern
static
40. Storageclass datatype variable1…
auto Variables
By default all the variables are defined as auto variables.
They are created when the function / block is entered
and destroyed when the function/ block is terminated.
41. Register variable
The allocation of CPU registers to variables , speeds up
the execution of the program.
Memory is not referred when such variables are
accessed.
No of variables which can be declared as register are
limited.
Faster execution
Example
42. static variables
The static storage class allows to define a variable
whose scope is restricted to either a block , a function
or a file and extent is the life span of the program.
Example
43. Extern variables
When a program spans across different files, they can
share information using global variables .
Global variables are one defined and can be accessed
by all others.
It can be achieved by declaring such variables as extern
48. Using the const Qualifier with
Pointers.
The const qualifier informs the compiler that the value of a
particular variable should not be modified.
Non constant pointer to constant data
const int *
Example
Constant pointer:
A constant pointer to a nonconstant data is a pointer
that always points to the same memory location and the
data can be modified through pointer.
Example
Example2