Oop PPT 1
Oop PPT 1
1
CONTENTS OF UNIT - I
• Introduction to procedural, modular, object-oriented and generic
programming techniques
3
FUNDAMENTALS OF OO PROGRAMMING(CONTINUED)
• Defining Function & Function Prototyping
• Function Declaration
• Function Calling
• Inline Function
• I/O manipulation
A SURVEY OF PROGRAMMING TECHNIQUES
• Unstructured programming
• Modular programming
• Object-oriented programming
• Generic programming
5
UNSTRUCTURED PROGRAMMING
6
STRUCTURED PROGRAMMING
• Also called as Procedural Programming
• For each task procedure is created
The procedures are called from main
7
MODULAR PROGRAMMING
• Procedures with some common functionality
are grouped together into separate modules
• Program is categorized into several smaller
modules
8
OBJECT-ORIENTED PROGRAMMING
#include
#include
#include
• No importance to data.
• No true reuse.
13
Pro. R. G. Masand, VIIT, Pune
PROCEDURAL V/S OBJECT ORIENTED
PROGRAMMING
14
Feature Procedure oriented Programming Object oriented Programming
In POP Program is divided into small parts In OOP, program is divided into parts
Divided Into
called functions called objects.
Importance In POP, Importance is not given to data In OOP, Importance is given to the data
but to functions as well as sequence of rather than procedures or functions
actions to be done. because it works as a real world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach
16
Pro. R. G. Masand, VIIT, Pune
17
OBJECT ORIENTED FEATURES
INTRODUCTION
class Box{
public:
double length; // Length of a box
double breadth; // Breadth of a box double
height; // Height of a box
}; 19
CLASS AS ADT
• Abstract Data Types (ADTs)
● type implementation & operations
● hidden implementation
• built-in and user-defined types are ADTs
Implementation client
use
20
manufacturer’s client
responsibility
C++ OBJECTS
• A class provides the blueprints for objects
• an object is created from a class
• Object is variable of class type
25
DATA ENCAPSULATION
• The wrapping up of data and function into a single
unit (called class) is known as encapsulation. Data
and encapsulation is the most striking feature of a
class.
26
DATA ABSTRACTION
• Abstraction refers to the act of representing
essential features without including the
background details or explanation
• E.g TV
● a television separates its internal implementation from
its external interface and we can play with its interfaces
like the power button, channel changer, and volume
27
control without having any knowledge of its internals.
INFORMATION HIDING
• Data hiding is one of the important features of
Object Oriented Programming which allows
preventing the functions of a program to access
directly the internal representation of a class type
• class Base {
};
29
INFORMATION HIDING(CONTINUED)
• A public member is accessible from anywhere
outside the class but within a program
31
POLYMORPHISM
• Polymorphism is another important OOP concept.
Polymorphism, a Greek term, means the ability to
take more than on form
• An operation may exhibit different behavior is
34
USE OF C++
36
C++ IDENTIFIER
• A C++ identifier is a name used to identify a
variable, function, class, module, or any other
user-defined item.
return 0;
} 42
// endl, which inserts a new-line character after every
line
VARIABLES DEFINITION IN C++
• A variable definition means to tell the compiler where and how much to create the
storage for the variable
type variable_list;
• Example
#include <iostream>
using namespace std;
// Variable declaration:
Type Description
47
GLOBAL VARIABLES : EXAMPLE
#include <iostream>
using namespace std;
cout << g;
return 0; 48
}
LITERALS
49
INTEGER LITERAL
• An integer literal can be a decimal, octal, or
hexadecimal constant.
212 // Legal
215u // Legal
0xFeeL // Legal
078 // Illegal: 8 is not an octal digit
85 // decimal
0213 // octal
0x4b // hexadecimal
30 // int
30u // unsigned int
30l // long
51
30ul // unsigned long
FLOATING POINT LITERALS
• A floating-point literal has an integer part, a
decimal point, a fractional part, and an exponent
part
3.14159 // Legal
52
BOOLEAN LITERAL
53
CHARACTER LITERALS
• "hello,
\
dear“
56
CONST KEYWORD
• use const prefix to declare constants with a specific type as
follows:
const type variable = value;
#include <iostream>
• Function Name
• Parameters
• Function Body 58
FUNCTION DECLARATIONS
• A function declaration tells the compiler about a
function name and how to call the function. The
actual body of the function can be defined
separately.
64
C++ REFERENCES
• Think of a variable name as a label attached to the
variable's location in memory
• then think of a reference as a second label attached
to that memory location
65
PROGRAM
#include <iostream>
using namespace std;
int main ()
{
// declare simple variables
int i;
}
OUTPUT
Value of i : 5
Value of i reference : 5
67
C++ REFERENCES V/S POINTERS
• References are often confused with pointers but
three major differences between references and
pointers are:
int main( )
{
char name[50];
cout << "Please enter your name: ";
cin >> name;
69
cout << "Your name is: " << name << endl;
}
THE STANDARD INPUT STREAM (CIN)(CNTD….)
• The stream extraction operator >> may be used
more than once in a single statement. To request
more than one datum you can use the following:
70
THE STANDARD OUTPUT STREAM (COUT)
• The predefined object cout is an instance of ostream
class
• The cout object is said to be "connected to" the
standard output device, which usually is the display
screen
#include <iostream>
using namespace std;
int main( )
{
char str[] = "Hello C++"; 71
• 0-argument constructor
• Parameterized constructor
72
• E.g.
Class test_ctor{
private :
int x,y;
public :
test_ctor(int x=0, int y=0){
73
}
CONTINUED….
void main(){
test_ctor t1(10,20);
74
THE CLASS DESTRUCTOR
#include <iostream>
using namespace std;
Max (20,10): 20
79
STATIC KEYWORD
• We can define class members static using static
keyword
Constructor called.
Constructor called.
82
STATIC FUNCTION MEMBERS
• By declaring a function member as static, we make
it independent of any particular object of the class
• A static member function can be called even if no
objects of the class exist and the static functions
83
DYNAMIC MEMORY ALLOCATION AND
DE-ALLOCATION
85
NEW AND DELETE OPERATOR
• We can allocate memory at run time within the
heap for the variable of a given type using a special
operator in C++ which returns the address of the
space allocated. This operator is called new
• Syntax :
new data-type;
data-type could be any built-in data type
86
• E.g.
#include<iostream>
using namespace std;
int main()
{
int n, *pointer,c;
cout<<“enter an integer\n”;
delete[] pointer;
return 0;
87
}
NEW OPERATOR
void main(){
int x, float y, int *p;
p = new int;
}
88
NAMED & NAMELESS OBJECTS....!!!
class shape{
private :
89
• E.g. New and delete void main(){
operator
Class example { example *p1,*p2;
private : p1 = new example;
int i, float a; p2 = new
example(15,30.5);
public :
};
THIS POINTER
• Also called as constant pointer
93
REFERENCES
• http://www.uow.edu.au/~lukes/TEXTBOOK/notes-cp
p/index.html
• http://www.cplusplus.com/reference/library/manipula
tors/
94
• http://www.cplusplus.com/doc/tutorial/