structured languages
TRANSCRIPT
JBIMS MIM Sem-IV Structured Language
Name Roll NoMufaddal Nullwala 15-I-131
MIM Second Year (2015 – 18)STRUCTURED LANGUAGE
TOPICS TO BE COVERED
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.
Why we need OOPs in Programming language?
Procedure Oriented Programming
Object Oriented Programming
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.
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.
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.
Data Types 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.
FUNDAMENTAL DATA TYPES
INTEGER
CHARACTER
DOUBLE
VOID
FLOAT
FUNDAMENTAL DATA TYPES ARE THOSE THAT ARE NOT COMPOSED OF
OTHER DATA TYPES
Int Data Type 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
Char Data Type 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
Float Data Type 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
Double Data Type 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
Void Data Type 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.
Arrays 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 cc[ 0 ], c[ 1 ] … c[ n - 1 ]
Nth element as position N-1
Arrays Array elements like other variables
Assignment, printing for an integer array cc[ 0 ] = 3;cout << c[ 0 ];
Can perform operations inside subscriptc[ 5 – 2 ] same as c[3]
Arrays
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all elements of this array have the same name, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Position number of the element within array c
Declaring Arrays When declaring arrays, specify
Name Type of array
Any data type Number of elements type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integersfloat d[ 3284 ]; // array of 3284 floats
Declaring multiple arrays of same type Use comma separated list, like regular
variablesint b[ 100 ], x[ 27 ];
Examples Using Arrays Initializing arrays
For loop Set each element
Initializer list Specify each element when array declaredint n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements 0
To set every element to same valueint n[ 5 ] = { 0 };
If array size omitted, initializers determine sizeint n[] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array
CLASSESClasses 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 FormAccount Holders Name, Birthdate, Gender, Residential Address, Office Address etc.
Example: public class student{
String name Int Age Proteted Function getStudentInfo(){ Return (info) }}
It’s a Template Stores data Reusable It can have subclass
OBJECTS• Objects are specific instance of classes, it contains the information related to the specific record it has. • Reference ID • Information related to the item
Pointers 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.
What is a pointer variable?
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
Using a Pointer Variable
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
int x; x = 12;
int* ptr; ptr = &x;
cout<<*ptr;
NOTE: The value pointed to by ptr is denoted by *ptr
*: dereference operator
2000
12
x
3000
2000
ptr
int x; x = 12; int* ptr; ptr = &x;*ptr = 5;
Using the Dereference Operator
2000
12
x
3000
2000
ptr
5
// changes the value at the address ptr points to 5
References 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
Reference Variables Example#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;
}
Difference Between Pointers and References
1. No explicit de-referencing is
required2. 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
What Is Inheritance? Provides a way to create a new class from
an existing class The new class is a specialized version of
the existing class
Example: Insect Taxonomy
Inheritance – Terminology and Notation
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. . .};
Class Access Specifiers1) 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.
Forms of Inheritance 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
‘Is a’ Relationship 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
What Does a Child Have?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
Multiple Inheritance• 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;classsquare
classrectSolid
classcube
Multiple Inheritance 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
Constructor
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.
Characteristics of Constructors
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.
Example for Constructors
Destructors
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.
Characteristics of Destructors
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.
Example for Destructors
What is polymorphism in programming?
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.
Types of polymorphism
There are two types of polymorphism in java :
1. Runtime polymorphism (Dynamic polymorphism)
2. Compile time polymorphism (static polymorphism).
Runtime Polymorphism( or Dynamic polymorphism)
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:
Lets see the below example to understand it better :
Compile time Polymorphism( or Static polymorphism)
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.
Lets see the below example to understand it better :