Structured Languages- Need and Characteristics of OOP, Data Types and Modifiers, Arrays, Classes, Objects, Pointers, References, Difference between Pointers and References, Inheritance, Constructors, Destructors, and Polymorphism.
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Structured Languages
1. Name Roll No
Mufaddal Nullwala 15-I-131
MIM Second Year (2015 – 18)
STRUCTURED LANGUAGE
JBIMS MIM Sem-IV
Structured Language
2. Object Oriented Programming, Need and
Characteristics
Basic Data Types and Modifiers
Arrays, Classes and Objects
Pointers, Reference, Difference between
Pointers and Reference
Inheritance, Constructors, Destructors and
Polymorphism.
3. Why we need OOPs in Programming language?
Procedure
Oriented
Programmin
g
Object
Oriented
Programmi
ng
4. Characteristics of Object Oriented Programming
Abstraction: Abstraction means showing essential features and hiding non-
essential features to the user.
For Eg: Yahoo Mail...
When you provide the user name and password and click on submit button..It will
show Compose, Inbox, Outbox, Sent mails...so and so when you click on
compose it will open...but user doesn't know what are the actions performed
internally....It just Opens....that is essential; User doesn't know internal actions
...that is non-essential things...
For Eg: TV Remote..
Remote is a interface between user and TV. Which has buttons like 0 to 10 ,on
/of etc but we don't know circuits inside remote. User does not need to know.
Just he is using essential thing that is remote.
5. Encapsulation: Encapsulation means which binds the data and code (or) writing
operations and methods in single unit (class).
For Example:
A car is having multiple parts like steering, wheels, engine etc. which binds together
to form a single object that is car. So, Here multiple parts of cars encapsulates itself
together to form a single object that is Car.
In real time we are using Encapsulation for security purpose...
Encapsulation = Abstraction + Data Hiding.
6. Polymorphism :
Polymorphism means ability to take more than one form that an operation
can exhibit different behavior at different instance depend upon the data
passed in the operation.
1) We behave differently in front of elders, and friends. A single person is
behaving differently at different time.
2) A software engineer can perform different task at different instance of time
depending on the task assigned to him .He can done coding , testing ,
analysis and designing depending on the task assign and the requirement.
7. While doing programming in any
programming language, we need to use
various variables to store various information.
Variables are nothing but reserved memory
locations to store values. This means that when
we create a variable, it reserves some space in
memory.
9. Integers are whole number such as 5,39,-1917,0
etc.
They have no fractional part
Integers can have positive as well as negative
value
An identifiers declared as int cannot have
fractional part
10. characters can store any member of the c++
implementation’s basic character set
An identifiers declared as char becomes
character variable
char set is often said to be a integer type
11. A number having a fractional part is a floating-
point number
the decimal point shows that it is a floating-
point number not an integer
for ex-31.0 is a floating-point number not a
integer but simply 31 is a integer
12. It is used for handling floating-point
numbers
It occupies twice as memory as float
It is used when float is too small or
insufficiently precise
13. The void data type specifies an empty set of
values .
It is used as the return type for functions that
do not return a value.
It is used when program or calculation does not
require any value but the syntax needs it.
14. Array
Consecutive group of memory locations
Same name and type (int, char, etc.)
To refer to an element
Specify array name and position number (index)
Format: arrayname[ position number ]
First element at position 0
N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
Nth element as position N-1
15. Array elements like other variables
Assignment, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
Can perform operations inside subscript
c[ 5 – 2 ] same as c[3]
17. When declaring arrays, specify
Name
Type of array
Any data type
Number of elements
type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
Declaring multiple arrays of same type
Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
18. Initializing arrays
For loop
Set each element
Initializer list
Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements 0
To set every element to same value
int n[ 5 ] = { 0 };
If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array
19. Classes are templates in real life it has variables,
arrays & functions
Example: Students Admission form consisting of
Full Name, Age, Birthdate, Standard, Class etc.
Bank Account Saving Form
Account Holders Name, Birthdate, Gender,
Residential Address, Office Address etc.
20. Example:
public class student{
String name
Int Age
Proteted Function getStudentInfo(){
Return (info)
}
}
It’s a Template
Stores data
Reusable
It can have subclass
21. • Objects are specific instance of classes, it
contains the information related to the
specific record it has.
• Reference ID
• Information related to the item
22. A pointer is a programming language object,
whose value refers to (or "points to") another
value stored elsewhere in the computer
memory using its memory address.
A pointer references a location in memory, and
obtaining the value stored at that location is
known as dereferencing the pointer.
23. A pointer variable is a variable whose value
is the address of a location in memory.
To declare a pointer variable, you must
specify the type of value that the pointer
will point to, for example,
int* ptr; // ptr will hold the
address of an int
char* q; // q will hold the address
of a char
24. int x;
x = 12;
int* ptr;
ptr = &x;
NOTE: Because ptr holds the address
of x,
we say that ptr “points to” x
2000
12
x
3000
2000
ptr
25. int x;
x = 12;
int* ptr;
ptr = &x;
cout<<*ptr;
NOTE: The value pointed to by ptr is
denoted by *ptr
2000
12
x
3000
2000
ptr
26. int x;
x = 12;
int* ptr;
ptr = &x;
*ptr = 5;
2000
12
x
3000
2000
ptr
5
// changes the value
at the address ptr
points to 5
27. A reference variable is an alias, that is, another
name for an already existing variable.
Once a reference is initialized with a variable,
either the variable name or the reference name
may be used to refer to the variable.
Application : References are primarily used as
function parameters
28. #include <iostream.h>
// Function prototypes
(required in C++)
void p_swap(int *, int *);
void r_swap(int&, int&);
int main (void){
int v = 5, x = 10;
cout << v << x << endl;
p_swap(&v,&x);
cout << v << x << endl;
r_swap(v,x);
cout << v << x << endl;
return 0;
}
void r_swap(int &a,
int &b)
{
int temp;
temp = a; (2)
a = b; (3)
b = temp;
}
void p_swap(int *a,
int *b)
{
int temp;
temp = *a; (2)
*a = *b; (3)
*b = temp;
}
29. 1. No explicit de-referencing is required
2. Guarantee that the reference will not be
NULL (though it may be invalid)
3. References cannot be rebound to
another instance
4. You don’t have to pass the address of a
variable
30. Provides a way to create a new class from an
existing class
The new class is a specialized version of the
existing class
31.
32. Base class (or parent) – inherited from
Derived class (or child) – inherits from the base
class
Notation:
class Student // base class
{
. . .
};
class UnderGrad : public student
{ // derived
class
. . .
};
33. 1) public – object of derived class can be treated
as object of base class (not vice-versa)
2) protected – more restrictive than public,
but allows derived classes to know details of
parents
3) private – prevents objects of derived class
from being treated as objects of base class.
34. Derived class inherits from base class
Public Inheritance (“is a”)
Public part of base class remains public
Protected part of base class remains protected
Protected Inheritance (“contains a”)
Public part of base class becomes protected
Protected part of base class remains protected
Private Inheritance (“contains a”)
Public part of base class becomes private
Protected part of base class becomes private
35. An object of a derived class 'is a(n)' object of the
base class
Example:
an UnderGrad is a Student
a Mammal is an Animal
A derived object has all of the characteristics of
the base class
36. An object of the derived class has:
• All members defined in child class
• All members declared in parent class
An object of the derived class can use:
• All public members defined in child class
• All public members defined in parent class
37. • A derived class can have more than one
base class
• Each base class can have its own access
specification in derived class's definition:
class cube : public square,
public rectSolid;
class
square
class
rectSolid
class
cube
38. Problem: what if base classes have member
variables/functions with the same name?
Solutions:
Derived class redefines the multiply-defined
function
Derived class invokes member function in a
particular base class using scope resolution
operator ::
Compiler errors occur if derived class uses
base class function without one of these
solutions
39. A class constructor is a special member function of
a class that is executed whenever we create new
objects of that class.
A constructor is a special member function whose
task is to initialize the objects of its class.
It is special because its name is same as the class
name.
The constructor is invoked whenever an object of
its associated class is created.
It is called constructor because it constructs the
values of data members of the class.
40. A constructor will have exact same name as the
class and it does not have any return type at all,
not even void.
Constructors can be very useful for setting
initial values for certain member variables.
Constructors can not be virtual.
41.
42.
43. Destructor is a special class function which
destroys the object as soon as the scope of
object ends. The destructor is called
automatically by the compiler when the object
goes out of scope.
The syntax for destructor is same as that for the
constructor, the class name is used for the
name of destructor, with a tilde ~ sign as prefix
to it.
44. Destructors are special member functions of the
class required to free the memory of the object
whenever it goes out of scope.
Destructors are parameter less functions.
Name of the Destructor should be exactly same
as that of name of the class. But preceded by ‘~’
(tilde).
Destructors does not have any return type. Not
even void.
45.
46.
47. Polymorphism is the capability of a method to do
different things based on the object .
In other words, polymorphism allows you define one
interface and have multiple implementations.
1. It is a feature that allows one interface to be
used for a general class of actions.
2. An operation may show different behavior in
different instances.
3. The behavior depends on the types of data used
in the operation.
4. It plays an important role in allowing objects
having different internal structures to share the
same external interface.
5. Polymorphism is extensively used in
implementing inheritance.
48. There are two types of
polymorphism in java :
1. Runtime polymorphism (Dynamic
polymorphism)
2. Compile time polymorphism (static
polymorphism).
49. Method overriding is a perfect example of Runtime
polymorphism. In this kind of polymorphism,
reference of class X can hold object of class X or an
object of any sub classes of class X. For e.g. if class Y
extends class X then both of the following statements
are valid:
50.
51. Method Overloading is a perfect example of
compile time polymorphism. In simple
terms we can say that a class can have
more than one methods with same name
but with different number of arguments or
different types of arguments or both.