Evaluation of Oop

32
C++ NOTES EVALUATION OF OOP:  The invent ion of the objec t ori ented techno logy elimi nates the flows faced because of procedure oriented approach. The oop approach binds the data and functions that work on it together.  This secur e d ata from any change s from outsi de th e mo dule. The system is divided into numerous invisible entities called objects. Objects ARE INDEPENDENT OF EACH OTHER. They are responsible for managing their own state and offering services to other objects. DEFINATION: “OBJECT ORIENTED PROGRAMING IS AN APPROACH THAT PROVIDES A WAY OF MODULARIZING PROGRAMS BY CREATING PARTITIONED MEMORY AREA FOR BOTH DATA AND FUNCTIONS  THAT CAN BE USED AS TEMPLA TES FOR CREATING COPIES OF SUCH MODULES ON DEMAND.” Benefits of oop: 1. The principle of data hiding helps the programmer to build secure programs. 2. It is easy to partition the work in a project based on objects. 3. Object oriented systems can be easily upgraded from small to large systems. 4. We can eliminate redundant code and extend the use of existing classes.

Transcript of Evaluation of Oop

Page 1: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 1/32

C++ NOTES

EVALUATION OF OOP:

 The invention of the object oriented technology eliminates theflows faced because of procedure oriented approach. The oop

approach binds the data and functions that work on it together.

 This secure data from any changes from outside the module. The

system is divided into numerous invisible entities called objects.

Objects ARE INDEPENDENT OF EACH OTHER. They are

responsible for managing their own state and offering services to

other objects.

DEFINATION:

“OBJECT ORIENTED PROGRAMING IS AN APPROACH THAT

PROVIDES A WAY OF MODULARIZING PROGRAMS BY CREATING

PARTITIONED MEMORY AREA FOR BOTH DATA AND FUNCTIONS

 THAT CAN BE USED AS TEMPLATES FOR CREATING COPIES OF

SUCH MODULES ON DEMAND.”

Benefits of oop:

1. The principle of data hiding helps the programmer to build

secure programs.

2. It is easy to partition the work in a project based on objects.

3. Object oriented systems can be easily upgraded from small tolarge systems.

4. We can eliminate redundant code and extend the use of 

existing classes.

Page 2: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 2/32

5. Oop provides an advantage in production and maintenance of 

software.

6. Object oriented program involves the identification and

implementation of different classes of objects and their behavior.

7. Software complexity can be easily managed.

8. It is possible to have multiple instance of an object to coexist

without any interface.

9. Oop leads to saving of development time and higher

productivity.

APPLICATIONS OF OOP:

• Artificial intelligence and expert system.

• Simulation and modeling studies.

• Object oriented database systems.

• Object oriented operating systems.

• Real time systems.

• Office automation systems.

• Cad / cam systems.

• Multimedia applications.

• Graphical user interface (GUI).

• Computer based training and education systems.

OBJECT ORIENTED LANGUAGE:

Page 3: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 3/32

A language that is specially designed to support the oop

concepts makes it easier to implement them. The language

should support several of the oop concepts to claim that theyare object oriented. Depending upon the features they

support, they can be classified into the following two

categories:

1. Object based programming languages.

2. Object oriented programming languages.

OBJECT ORIENTED PROGRAMING IS THE, style of 

programming with the features data encapsulation, data

hiding and access mechanisms, automatic initialization and

clear up of objects, operator overloading. Languages that

support programming with objects are said to be objectbased programming languages.

OBJECT ORIENTED PROGRAMING incorporates all of 

object based programming features and dynamic binding.

Languages that support these features include C++,

Smalltalk and object Pascal.

PROPERTIES/ CONCEPTS/ BUILDINGS OF OOPS:

Objects

Classes

Page 4: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 4/32

Inheritance

Polymorphism

Dynamic binding

Overloading

Data abstraction

Encapsulation

OBJECTS: objects are basic run time entities in an object-

oriented prgmng. An object may be defined as an identifiable

entity with some Characteristics and behaviour.Each of these has a define state, behaviour and identity. The

features of an object are.

(i) State

(ii) Behaviour

(iii) Identity

CLASSES:

  The most important feature of object oriented programming is

the Classes. A class is the way to binf the data and its

Page 5: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 5/32

associated functions together. It allows the data to be hidden if 

necessary from the external use.

Inheritance:

Inheritance is one of the most powerful features of OOP’S

.inheritance is a process of creating a new class from existing

class.

Polymorphism

Polymorphism comes from the Greek words “ploy” and“morphism”.

“poly” means many and “morphism” means forms i.e many

forms

In oops polymorphism refers to identically named methods

have different behaviours depending on the type of object.

Polymorphism means the ability to take more then one form.

Dynamic Binding:

Binding refers to tie –up of a procedure call. Dymanic binding

means that the code associated with a given procedure call is

not known until it call at runtime.

Overloading:

Overloading is a language features that allows a function or

operator to be given more than one definition. The type of the

argument with which the function or operator is called

overloading.

Page 6: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 6/32

Data Abstraction:

Creating a new datatype using the encapsulated –item thatare suited to an application to be programmed is known as

data abstraction.

 Th edata types created by the data abstraction is known as

abstract data type(ADT).

Encapsulation

 The wrapping of data and function into a single unit is known

as encapsulation. Data encapsulation is the most important

feature of a class.

By using encapsulation we can accomplish data hiding.

INTRODUCTION TO C++

C++ is an object oriented programming language. Initially

named ‘C with classes ‘. C++ was developed by “BJARNE

STORUSTUP” at AT & T Bell labs in 1980.

C++ is a versatile language for handling very large programes.

It is suitable for virtually any programming task including

Page 7: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 7/32

development of editors ,compilers ,databases,communicayion

systems and any complex real life applications.

DIFFERENCE BETWEEN C AND C++:

 

STRUTURE OF C++ PROGRAM:

A typical C++ program would contain four sections as

shown in figure. These sections may be place in separate code

files and then compiled independently of jointly.

C C++

1. No data security

2. Non reliability of 

programs

3. Top down design

approach

4. Function overloading

and operator

overloading are not

possible in procedure

oriented languages.

5. Polymorphism,

encapsulation, and

inheritance are not

possible in procedure

1. Data security

2. Reliability of programs

3. Bottom up design

approach.

4. Function overloading

and operator

overloading are possible

in object oriented

languages.

5. Polymorphism,

encapsulation, and

inheritance are possible

in procedure oriented

languages.

Page 8: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 8/32

It is common practice to organize a program into three

separate files. The class declarations are place in a

header files and the definition of member functions go

into another file. This approach enables the programmer

to separate the abstract specification of the interfacefrom the implementation details. Finally, the main

program that uses the class is placed in a third file which

“includes” the previous two files as well as any other

files required.

Include files

Class declaration

Class functions

definitions

Main function

program

Page 9: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 9/32

Structure of a C++ program…

[Preprocessors]

[Global declaration]

[Linkage section]

void main()

{

[<Declaration_Part>];

[<Execution_Part>];

}

Comments

Comments are those lines which are ignored by the

compiler.

C++ supports 2 types of comments…

1). Single line comment (//)

2). Multiline comment (/*……….*/)

Input/output Statements

Page 10: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 10/32

C++ supports input/output with the help of streams.

1). Cout

2). Cin

Cout: Used for display message and memory values.

Syntax:

Cout<<”[Message]”<<var1<<var2<<..<<var_n

Cin: Used for accept data from input device(keyboard).

Syntax:

Cin>>var1>>var2>>……>>var_n

Keywords/ Reserved words:

Page 11: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 11/32

Keywords are those words whose definition is already

defined in a compiler.

List of keywords:

Auto asm static int float double long short int

signed unsigned break Continue goto return void

switch if 

Else struct union enum extern typedef while

do for char const far huge volatile mutable

sizeof case default

C++ keywords:

Class public private protected virtual inline try

catch throw new delete type Friend .

Drawbacks of structured programming

• Data gets freely move in a program.

• More priority is given to procedure/function ratherthan data.

• Perfect code reusability is not provided.

 To overcome the above listed drawbacks foundations

were laid with a programming technique called “Object

Oriented Programming System(OOPS).

Using oops –

Page 12: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 12/32

->Data is restricted.

->Equal priority is given to data and procedure

->Perfect code reusability is provided.

Components of Oops

Following are the 2 component on which oops

dependent. Often they are called ‘Building Blocks of 

oops”.1.class2.object

class: It is a user-defined data type. Class is a

collection of data members and member functions.

Class is enclosed with 3 access specifiers.

• Private

• Public

• Protected

Syntax:

Page 13: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 13/32

Class [<class_name>]

{

private:

data members;

member functions;

friend functions;

Public:

data members;

member functions;

friend functions;

protected:

data members;

member functions;

friend functions;

};

Points to be remembered to work with class:

Page 14: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 14/32

Class is created with a keyword called “class”

class name to a class is optional

class can be created as local or global.

More than one class can be created in a singleprogram but always with unique class names

Instance variables can’t be assigned with constantvalues directly in a class

No sequence is followed for access specifiers of aclass

Access specifier can be used any no.of times

By default data members and member functions of aclass are “private”.

Every class should be terminated by semicolon(;)

Methods enclosed in a class can’t be provided withlooping constructs,switch case statemens,recursivefuns..etc. because by default methods are “inlinefunctions”

Object:

Instance of a class is refers to object.

(or)

It is the one through which instance variables and

methods of a class gets access.

Note:

Page 15: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 15/32

Minimum of one object is must

Redeclaration of object is invalid

Object created at one class the same can’t beused in other class

For every object separate memory is allocated

Object can be created in the following procedurea). class <class_name> <obj1,[obj2,…..]>;

b). <class_name> <obj1,[obj2,…..]>;

eg’s…..

Accessing procedures for 3 access specifiers

of a class.

1.when instance variables and methods arecreated at public access specifier, then they getaccess in and out side of the class.

2.when instance variables and methods arecreated at private access specifier they workonly in side a class.

3.minimum of one public method is must tointeract with private access specifier

4.protected access specifier is equal to privateaccess specifier

5.they can share the data among themselves

Page 16: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 16/32

i.e. private can access public,public can access

private and protected etc.

Defining methods outside a class

By default methods enclosed in a class works for

inline, where it can’t be expanded by writinglooping constructs,switch case statements. To

expand the method it should be defined outside

the class.

 To define the method outside the class operator

called (:: - Scope resolution operator) is used.

Note: when method is defined out side the class

then its prototype or declaration should be created in

a class.

Page 17: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 17/32

Syntax:

Return type class_name :: method_name(list of par)

{

// body of the method..

}

Friend Functions

Data stored in private instance variables can be

accessed with member functions and non-member

functions of a class.

A keyword called friend should be provided before the

return type of a function.when a friend function iscreated its prototype should be created in a class at any

access specifier when a definition of a friend function is

written outside the class then it should be written

without scope resolution operator and friend function

should be called without object.

Syntax:

Friend return_type method_name(list of parameters)

Page 18: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 18/32

Friend Class

Data stored in private instance variables of one classcan be with the methods of the different class, where it is

essential that a class should be made as friend to that

class.

Syntax:

Friend class <class_name>;

(or)

Friend class_name;

INLINE FUNCTION

Inline functions in c++ are used like macros. That is they

will be substituted at the point of the function call at thetime of compilation rather that making a function

definition will be preceded by the keyword inline.

Syntax:

Inline return_type func_name([parameters])

{//function body

}

Advantages:

Page 19: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 19/32

1) reduces the overheads raised in function calls

2) increases the modularity of the program

3) code will be substituted to the point of call at thetime of compilation.

4) Faster execution of the prgm.

5) Inline function can be used in the code just as

normal functions.

LIMITATIONS:

1) the function should be very simple.

2) The function should not have a loop,a switch,a

goto statements.

3) The function should not have a empty returnstatement if it doesn’t return something.

4) The function shouldn’t be recersive.

PASSING OBJECTS TO FUNCTIONS:

Like any other data type , an object may be used as a

function arguments . the object can be done in 3 ways

Page 20: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 20/32

1) A copy of the entire object is passed to the

function.

2) Only the address of the object is transferred to

the function

3) Retuning objects from the function

 The first method is called as pass-by-value .since a

copy of the object is passed to the function , any

changes to the object inside the function do not effect

the object used to call the function.

 The second method is called pass-by-reference.when

an address of the object is passed ,the called function

works directly on the actual objects used in the call.

 This means that any changes made to the object inside

the function will effect in the actual object.

RETURNING OBJECTS:

A function can not only receive the objects as

arguments but also can return them. The example

prgm shows how an object can created and returned toother function.

Page 21: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 21/32

Page 22: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 22/32

different tasks. This is known as function polymorphism

in oops.

It is a concept of polymorphism

CONSTRUCTORS:

Contructors are special methods of a class which are use

to construct thee data for an instance (object) when it is

created.

 The main operation of constructors is to allocate the

required resources such as memory and initialize the

objects of its class.

Characteristics of constructors:

1) It has the same name as that of the class name

which it belongs

2) It is executed automatically when ever the class

is instantiated(object is created)

3) It does not have any return type(not even void)

4) It is normally used to initialize the data members

of a class

5) It is also used to allocate memory to the dynamicdata members of a class.

Page 23: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 23/32

 TYPES OF CONSTRUCTORS

1) default constructor

2) parameterized constructor

3) copy constructor

Default Constructor:

Constructor with no parameters refers to default

constructor

Syntax:

Class-name ()

{

Stmt 1;

Stmt 2;

.

.

Stmt n;

}

1) Contructors should always be created with publicaccess specifier

2) Constructors support to make different

expressions for calculations

Page 24: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 24/32

3) Constructors can call other methods of a class

PARAMETERIZED CONSTRUCTOR:

Constructors with parameters refers to parameterizedconstructors ,more than one parameterized constructor

can be enclosed in a single class where it differentiates

them based on list of parameters.

It is often called as constructor overloading

Class-name(list of parameters)

{

Stmt 1;

Stmt n;

}

DYNAMIC CONSTRUCTOR

Allocation of memory during the creation of objects can

be done by constructors too.

 The memory is saved as it allocates the right amount of 

memory for each object. Allocation of memory to objects

at the time of their construction is known as dynamic

construction of objects.

New operator is used to allocate memory.

Page 25: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 25/32

OVERLOADED CONSTRUCTOR

 The other name of multiple constructors in a class isoverloaded constructor.

 They are

Constructor() //no arguments

Constructor (int, int) //two arguments

Constructor (int, float, int) // three arguments

DESTRUCTORS:

Destructors are the special methods of a class which

provides a confirmation about the memory which is

destroyed created for an object.

Syntax:

~class-name ()

{

Stmt 1;

Stmt n;

}

Page 26: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 26/32

ARRAY OF OBJECTS:

An array can be of any data type including struct. Wecan also have arrays of variables that are are of the class

type.

Such variables called as “array of objects”.

EX:

POINTER TO OBJECTS:Pointer can be used to hold the address of objects .the

need for using pointer to objectsbecome clear when

objects are to be created while prgm is being executed.

Syntax:

Class-name *ptr-obj;Where class name is the name of class and *ptr-obj is the

name of the pointer to the object of the class.

A pointer variable can be defined to hold the address of 

an object which is created statically or dymanically as

shown.

Prt-obj=&object

Where ptr-obj is the name of the pointer to the object

and object is the address of object created.

Page 27: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 27/32

This*

It is a keyword provided by c++

• It is by nature of class type• It is hidden objects which automatically enter into

the class when a message is passed.

• It accesses instance variables with the help of arrow(->) operator.

• It is used to differentiate between instance variables

and local variables when represents with samevariable names.

Dynamic Memory

Until now, in all our programs, we have only had as much

memory available as we declared for our variables,

having the size of all of them to be determined in thesource code, before the execution of the program. But,

what if we need a variable amount of memory that can

only be determined during runtime? For example, in the

case that we need some user input to determine the

necessary amount of memory space.

The answer is dynamic memory, for which C++ integrates the operatorsnew and delete.

Operators new and new[]

Page 28: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 28/32

In order to request dynamic memory we use the operator

new. new is followed by a data type specifier and -if a

sequence of more than one element is required- the

number of these within brackets []. It returns a pointer tothe beginning of the new block of memory allocated. Its

form is:

pointer = new typepointer = new type [number_of_elements]

The first expression is used to allocate memory to contain one singleelement of type type. The second one is used to assign a block (an

array) of elements of type type, where number_of_elements is

an integer value representing the amount of these. For example:

int * bobby;

 bobby = new int [5];

Operators delete and delete []

Since the necessity of dynamic memory is usually limited

to specific moments within a program, once it is no

longer needed it should be freed so that the memory

becomes available again for other requests of dynamic

memory. This is the purpose of the operator delete, whoseformat is:

delete pointer;

delete [] pointer;

Page 29: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 29/32

The first expression should be used to delete memory allocated for a

single element, and the second one for memory allocated for arrays of 

elements.

REFERNCE VARIABLE:

Refernce variable provides a different name for already defined

variable.this is possible with the help of the address operator &.

If we have an integer variable sum declared and initialized as follows

Syntax:

Data_type &refernce_var=var_name;

Int &x=10;

TEMPLATES:

Templates in c++ is called generic data types ,where generic refers tonon-specific or universal.template supports to create functions where

those functions are called gereric functions.

Templates is a technique that allows using a single function or class to

work with different data types .using templates a single function can

 process any type of data i.e., formasl arguments of template function areof template type.

Template are divided into two types

Page 30: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 30/32

1) Function template

2) Class template

Guidelines for template

1)templates should always be created as global

2) templates variable should be created of class type enclosed under < >

3) templates variable can be used anywhere in generic function i.e., can

 be used at formal parameters ,declaring local variables can be used asreturn type.

4) templates can be attached with max of one function & one class.

5) if other function of as program to be created as generic function’s

Must that templates should be used.

6) the process of changing formal parameters with different datatypes depends on the sending parameters .it is often called as

“instantiating of an object”

function templates:

 

A generic function defines set of operations that will be applied to

various types of data. A generic function has the type of data that it will

operate upon passed to it as a parameter.

Page 31: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 31/32

Page 32: Evaluation of Oop

7/31/2019 Evaluation of Oop

http://slidepdf.com/reader/full/evaluation-of-oop 32/32

Class <class_name>

{

Private:

Protected:

Public:

}