structured languages

53
Name Roll No Mufaddal Nullwala 15-I-131 MIM Second Year (2015 – 18) STRUCTURED LANGUAGE JBIMS MIM Sem-IV Structured Language

Upload: mufaddal-nullwala

Post on 21-Apr-2017

13 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Structured Languages

JBIMS MIM Sem-IV Structured Language

Name Roll NoMufaddal Nullwala 15-I-131

MIM Second Year (2015 – 18)STRUCTURED LANGUAGE

Page 2: Structured Languages

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.

Page 3: Structured Languages

Why we need OOPs in Programming language?

Procedure Oriented Programming

Object Oriented Programming

Page 4: Structured Languages

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.

Page 5: Structured Languages

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.

Page 6: Structured Languages

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.

Page 7: Structured Languages

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.

Page 8: Structured Languages

FUNDAMENTAL DATA TYPES

INTEGER

CHARACTER

DOUBLE

VOID

FLOAT

FUNDAMENTAL DATA TYPES ARE THOSE THAT ARE NOT COMPOSED OF

OTHER DATA TYPES

Page 9: Structured Languages

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

Page 10: Structured Languages

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

Page 11: Structured Languages

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

Page 12: Structured Languages

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

Page 13: Structured Languages

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.

Page 14: Structured Languages

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

Page 15: Structured Languages

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]

Page 16: Structured Languages

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

 

Page 17: Structured Languages

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 ];

Page 18: Structured Languages

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

Page 19: Structured Languages

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.

Page 20: Structured Languages

Example: public class student{

String name Int Age Proteted Function getStudentInfo(){ Return (info) }}

It’s a Template Stores data Reusable It can have subclass

Page 21: Structured Languages

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

Page 22: Structured Languages

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.

Page 23: Structured Languages

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

Page 24: Structured Languages

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

Page 25: Structured Languages

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

Page 26: Structured Languages

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

Page 27: Structured Languages

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

Page 28: Structured Languages

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;

}

Page 29: Structured Languages

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

Page 30: Structured Languages

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

Page 31: Structured Languages

Example: Insect Taxonomy

Page 32: Structured Languages

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. . .};

Page 33: Structured Languages

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.

Page 34: Structured Languages

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

Page 35: Structured Languages

‘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

Page 36: Structured Languages

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

Page 37: Structured Languages

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

Page 38: Structured Languages

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

Page 39: Structured Languages

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.

Page 40: Structured Languages

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.

Page 41: Structured Languages

Example for Constructors

Page 42: Structured Languages
Page 43: Structured Languages

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.

Page 44: Structured Languages

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.

Page 45: Structured Languages

Example for Destructors

Page 46: Structured Languages
Page 47: Structured Languages

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.

Page 48: Structured Languages

Types of polymorphism

There are two types of polymorphism in java :

1. Runtime polymorphism (Dynamic polymorphism)

2. Compile time polymorphism (static polymorphism).

Page 49: Structured Languages

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:

Page 50: Structured Languages

Lets see the below example to understand it better :

Page 51: Structured Languages

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.

Page 52: Structured Languages

Lets see the below example to understand it better :

Page 53: Structured Languages