31
qwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwe rtyuiopasdfghjklzxcvbnmqwer tyuiopasdfghjklzxcvbnmqwert yuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyu iopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiop asdfghjklzxcvbnmqwertyuiopa sdfghjklzxcvbnmqwertyuiopas dfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdf Venkteshwar Institute Of Tech.Indore OOT notes(CS-403) BY: Er.NIVEDITA SHIRKE (CS/IT dpt.)

OOT(unit I According to rgtu syllabus)

Embed Size (px)

Citation preview

Page 1: OOT(unit I According to rgtu syllabus)

qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmrtyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqw

Venkteshwar Institute Of Tech.Indore

OOT notes(CS-403)

BY:Er.NIVEDITA SHIRKE

(CS/IT dpt.)

Page 2: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Object Oriented Technology

(CS-403)

History The terms "objects" and "oriented" in something like the modern sense of object-oriented programming seem to make their first appearance at MIT in the late 1950s and early 1960s. Object-oriented programming developed as the dominant programming methodology in the early and mid 1990s when programming languages supporting the techniques became widely available. More recently, a number of languages have emerged that are primarily object-oriented yet compatible with procedural methodology, such as Python and Ruby. Probably the most commercially important recent object-oriented languages are Visual Basic.NET (VB.NET) and C#, both designed for Microsoft's .NET platform, and Java, developed by Sun Microsystems.

Simula (1967) is generally accepted as the first language to have the primary features of an object-oriented language. It was created for making simulation programs, in which what came to be called objects were the most important information representation.Smalltalk is the example for it.

Programming Approaches

1. Modular Programming Approach:- In this approach we have some code that are implemented thru short of modules. We basically empasize on that modules and that modules are collectively form a program in a sequence.Like-JAVA Simula.

2. Top-Bottom Programming Approach :- It includes POP programming3. Bottom-Up Programming Approach :- It includes OOP programming.4. Structured Programming Approach :- Structured programming (sometimes known as

modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. It was unale to make reusable code, also it was difficult to maintain, so was not much popular.

Difference Between OOP and POP

Procedure Oriented Programming (POP)

1. Main program is divided into small parts depending on the functions.

Object Oriented technology Page 2

Page 3: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

2. The Different part of the program connects with each other by parameter passing & using operating system.

3. Every function contains different data.

4. Functions get more importance than data in program.

5. Most of the functions use global data.

6. Same data may be transfer from one function to another

7. There is no perfect way for data hiding.

8. Functions communicate with other functions maintaining as usual rules.

9. More data or functions can not be added with program if necessary. For this purpose full program need to be change.

10. To add new data in program user should be ensure that function allows it.

11. Top down process is followed for program design.

12. Example: Pascal, Fortran

Object Oriented Programming (OOP)

1. Main program is divided into small object depending on the problem.

2. Functions of object linked with object using message passing.

3. Data & functions of each individual object act like a single unit.

4. Data gets more importance than functions in program.

5. Each object controls its own data.

6. Data does not possible transfer from one object to another.

7. Data hiding possible in OOP which prevent illegal access of function from outside of it. This is one of the best advantages of OOP also.

8. One object link with other using the message passing.

Object Oriented technology Page 3

Page 4: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

9. More data or functions can be added with program if necessary. For this purpose full program need not to be change.

10. Message passing ensure the permission of accessing member of an object from other object.

11. Bottom up process is followed for program design.

12. Example: C++, Java.

We can summarize:

The difference between OOP an d POP are as foolows.

S.No. POP OOP 1. User defined types Classes 2. Variables Objects 3. Structure members Instance Variables 4. Functions Methods 5. Function call Message passing

Merits of OOP:

1. It is possible to have multiple instances of an object to co-exist without any interference.2. Each object leads itself to achieve greater modularity.3. Object shares their responsibilities to achieve the consistency.4. Data centered design approach enables to capture more implementation details.5. We can extend the code through reusability.

Demerits of OOP:

1. Type of the software to be developed.2. Preferences of the software developer.3. Resources and environment available.

Object Oriented technology Page 4

Page 5: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Applications of OOP:

1. Office automation system2. Real time systems3. Hypermedia.4. Computer aided designing.5. Neural networks6. Simulation and modelling.

Features of OOPs

1. Object 2. Classes3. Data abstraction and encapsulation4. Inheritance5. Polymorphism6. Dynamic Binding7. Message passing

Abstract Data Types

Abstrac t word refers to “hide”. It means it will relate to hidden data.Abstract data types are implemented as classes.A class represent essential features without including the background details or explanations.They encapsulate all the essential properties of the objects that are to be created.The classes use the concept of data abstraction ,they are known as ABSTRACT DATA TYPES.

Example: Arithmatic operations like addition, substraction,division,multiplication of two numbers,so in this case there is no need to know how the number is stored or which algo.is used,or which variable or numbers are to be taken. Another example in terms of C++ are templates and generic data structure.

Properties:

1. It provides an interface without exposing the implementation details.

Object Oriented technology Page 5

Page 6: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

2. It exports a type with set of operations.3. It provides a unique mechanism to access data structure of the data types.4. It defines an application domain.5. It defines an attributes and methods.

OBJECTS

Note:If you got the Question about Object -like describe objects or explain objects or give details about object then you have to cover the following points,write same as according to the following points:

1. Defination of object(1 bolck diagram)2. Representation of objects3. Memory allocation of objects4. Syntax

5. Components of object6. Example(program of object)

1.Defination: An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program. Functions have previously been described as "black boxes" that take an input and spit out an output. Objects can be thought of as "smart" black boxes. That is, objects can know how to do more than one specific task, and they can store their own set of data. Let's say that we are writing a text-based medieval video game. Our video game will have two types of characters: the players and the monsters. A player has to know the values of certain attributes: health, strength, and agility. A player must also know what type of weapon and what type of armor they possess. A player must be able to move through a maze, attack a monster, and pick up treasure. So, to design this "player object", we must first separate data that the player object must know from actions that the player must know how to execute. The definition for a player object could be:

Player Object:

data:

health

strength

Object Oriented technology Page 6

Page 7: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

agility

type of weapon

type of armor

actions:

move

attack monster

get treasure

END;

There is a very important distinction between an object and an instance of an object. An object is actually a definition, or a template for instances of that object. An instance of an object is an actual thing that can be manipulated. For instance, we could define a Person object, which may include such member data as hair color, eye color, height, weight, etc. An instance of this object could be "Dave" and Dave has values for hair color, eye color, etc. This allows for multiple instances of an object to be created. Let's go back to the medieval video game example and define the monster object.

Monster Object:data: health skin thickness claws tail spikesactions: move attack player with claws attack player with tailEND;

Now, our game could have one instance of a player: Player Instance #1:data: health = 16 strength = 12

Object Oriented technology Page 7

Page 8: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

agility = 14 type of weapon = "mace" type of armor = "leather"END;

2. Representation of objects: An object takes up space in the memory and have an associated address.These are the class variables.A class provide a template ,which defines the member functions and variables that are required for objects of the class type.

3. Memory allocation of objects: The necessary memory space is allocated to an object at the time of its declaration.The class specification provides only a template and does not create any memory space for the object.

4. Syntax:

Class user defined name

{

Private:

Data members;

Member function;

Public:

Member functions;

Protected:

Data members;

}; class_name obj1,obj2,objn;

5.Components:

1. Data members2. Member functions3. Static data members

Object Oriented technology Page 8

Page 9: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

4. Static member function5. Const member function

Example:

Let

Box b1,b2;

Here b1 and b2 are the objects of class Box.Then

class Box

{

private : int l,h,w;

public :

void get_data()

{

cout<<”enter the length width and height of box”;

cin>>l>>h>>w;

}

void display()

{

cout<<”the length of box is<<l;

cout<<”the height of box is<<h;

cout<<”the width of box is<<w;

}

Object Oriented technology Page 9

Page 10: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

};

main ()

{

Box b1,b2;

}

In above example b1 and b2 are the two objects of the class Box. Here the object b1 and b2 both have their own copy of variables l, h, w ,and created on the same line. The Object can be created on separate line

Box b1;

Box b2;

There is another place where objects can be created i.e. at the time of defining a class by placing objects name after the ending brace but before the semicolon of class.

class Box

{

members

}b1,b2;

Classes

Note:Same as question related to class should be written as follows:

1. Defination of class2. Representation of class3. Components of class4. Example(program of class)5. Varieties of class

Object Oriented technology Page 10

Page 11: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Representation:

Components of class:

Data encapsulation Data abstraction Inheritance Polymorphism

Varieties of classes:

1. Abstract class: A class that is not used to create an object.

2. Generic class: A class that serves as a template for other object.

3. Container class: A class whose instances are collection of objects.

4. Object class: A class do not explicitly extend any other class.

5. Super class: A class having more than one member.

6. Base class7. Derived class

Object Oriented technology Page 11

IT

Getinfo()

Showinfo()

CLASS : IT

DATA MEMBERS: Name; Age: ……..MEMBER FUNCTIONS: Getinfo(); Showinfo(); ………..

Page 12: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Attributes or Variables Methods or functions

Note:Same as question related to function should be written as follows:

1. Defination2. Need 3. Types(main,inline,friend,member)4. Uses(to read,write debug and modify complex problems)5. syntax6. Example(program)

1.Defination: A complex program may be decomposed into a small or easily manageable parts or module called FUNCTION. To perform various tasks,a function can be called for many times.

2. NEED: Functions are useful when we need to divide large and complex programs into small one.

3. Program:

#include <iostream>

using namespace std;

int addition (int a, int b)

{

int r;

r=a+b;

return (r);

}

Object Oriented technology Page 12

Page 13: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

int main ()

{

int z;

z = addition (5,3);

cout << "The result is " << z;

return 0;

Inline Function:

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided.A function is declared inline by using the inline function specifier or by defining a member function within a class or structure definition. The inline specifier is only a suggestion to the compiler that an inline expansion can be performed; the compiler is free to ignore the suggestion.

In C++, both member and nonmember functions can be inlined. Member functions that are implemented inside the body of a class declaration are implicitly declared inline. Constructors, copy constructors, assignment operators, and destructors that are created by the compiler are also implicitly declared inline. An inline function that the compiler does not inline is treated similarly to an ordinary function: only a single copy of the function exists, regardless of the number of translation units in which it is defined.

General Format of inline Function:

The general format of inline function is as follows:

inline datatype function_name(arguments)

The keyword inline specified in the above example, designates the function as inline function. For example, if a programmer wishes to have a function named exforsys(execution for system) with return value as integer and with no arguments as inline it is written as follows:

Object Oriented technology Page 13

Page 14: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

inline int exforsys( )

Example: the concept of inline functions:

#include <iostream.h>

int exforsys(int);

void main( )

{

int x;

cout << “\n Enter the Input Value: ”;

cin>>x;

cout<<”\n The Output is: “ << exforsys(x);

}

inline int exforsys(int x1)

{

return 5*x1;

}

The output of the above program is:

Enter the Input Value: 10

The Output is: 50

One of the important advantage of the inline function in a program is that they help us to save memory space.This is useful in case when a function is likely to be called many times.The overheads involved in calling a function are:

Jump to the function generated by compiler Passing values Returning values

Saving register Pushing arguments into stack

Object Oriented technology Page 14

Page 15: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

So, by calling again and again it takes a lot of time in executing a series of instruction.So, the solution to this problem is:One solution to this problem is to use macro definations, but these are popular in C.

Macros definition: Macros are most frequently used to define names for constants that occur repeatedly in a program. The more preferred way of defining these constants is by using the keyword const. Since the current focus is not on which is the best way to define constants, let us leave it alone. The #define directive is quite powerful and hence allows the macro name to have arguments and thus behave like a function. Such forms of macros are referred to as function-like macros. Each time the macro name is encountered with arguments, the arguments used in its definition are replaced by the actual arguments found.

For example:

Collapse

#include "iostream.h"

#define MAX(a, b) ((a < b) ? b : a)

int main( void)

{

cout << "Maximum of 10 and 20 is " << MAX(10, 20) << endl;

return 0;

}

Collapse

Output:

Maximum of 10 and 20 is 20.

Advantage of Macro: useful for introducing name constant.

Object Oriented technology Page 15

Page 16: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Disadvantage: They are not really functions and that’s why the usual checking doesn’t occur during compilation of a program.

Advantages and Disadvantages of inline function:-

Advantages :-

1) It does not require function calling overhead.

2) It also save overhead of variables push/pop on the stack, while function calling.

3) It also save overhead of return call from a function.

4) It increases locality of reference by utilizing instruction cache.

5) After in-lining compiler can also apply intraprocedural optmization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..

Disadvantages :-

1) May increase function size so that it may not fit on the cache, causing lots of cahce miss.

2) After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization.

3) It may cause compilation overhead as if some body changes code inside inline function than all calling location will also be compiled.

4) If used in header file, it will make your header file size large and may also make it unreadable.

5) If somebody used too many inline function resultant in a larger code size than it may cause thrashing in memory. More and more number of page fault bringing down your program performance.

6) Its not useful for embeded system where large binary size is not preferred at all due to memory size constraints.

Friend function:

Object Oriented technology Page 16

Page 17: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Need for Friend Function:

when a data is declared as private inside a class, then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. A programmer may have a situation where he or she would need to access private data from non-member functions and external classes. For handling such cases, the concept of Friend functions is a useful tool.

What is a Friend Function?

A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class.

How to define and use Friend Function in C++:

The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend. The friend function must have the class to which it is declared as friend passed to it in argument.

Some important points to note while using friend functions in C++:

1. The keyword friend is placed only in the function declaration of the friend function and not in the function definition.

2. It is possible to declare a function as friend in any number of classes.

3. When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend.

4. A friend function, even though it is not a member function, would have the rights to access the private members of the class.

5. It is possible to declare the friend function as either private or public.

Object Oriented technology Page 17

Page 18: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

6. The function can be invoked without the use of an object. The friend function has its argument as objects, seen in example below.

Example to understand the friend function:

#include

class exforsys

{

private:

int a,b;

public:

void test()

{

a=100;

b=200;

}

friend int compute(exforsys e1)

//Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passed to it

};

int compute(exforsys e1)

{

Object Oriented technology Page 18

Page 19: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

//Friend Function Definition which has access to private data

return int(e1.a+e2.b)-5;

}

main()

{

exforsys e;

e.test();

cout<<"The result is:"< //Calling of Friend Function with object as argument.

}

The output of the above program is :

The result is:295

Object interaction(message passing):

Objects of two different classes in object oriented programming interact with one another by sending and receiving messages. Objects communicate with one another by sending and receiving information much the same way as people pass message to one another.In OOP, objects communicate with each other through creating classes and objects and establishing communication aomg them.the step of creating classes defines not only objects but also their behavior.Object may be created by their class definations.

Example: suppose we are having two objects:-

1. Customer object2. Account object

Then the “customer object” may send a message to the “account object” to know the bank balance as an request.Customer obj.contains data and code to manipulate the data.Customer obj.interact with account object without knowing the details of each other’s data and code.

Object Oriented technology Page 19

Page 20: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Message passing involves specifying the name of the object,the name of the function(message) and the information to be sent.

Example:

Employee.salary(name);

Here:

1. Employee ia an object2. Salary is a message3. Name is an information

Types of objects:

1. External or global objects: Existence- Throughout the lifetime of the whole program.Visibility-Globally available to all modules.

2. Automatic or local objects:Existence- Within the scope.Visibility-Local scope in which objects are created.

3. Static objectsExistence- Throughout the whole program.Visibility-Local scope in which objects are created.

4. Dynamic objects:Existence-Lifetime may be controlled within a particular scope.Visibility-Within a particular scope.

State of Objects:

1. Active Objects- An object which has its own thread of control.2. Passive objects-An object which does not have its own thread of control.

Object Oriented technology Page 20

Page 21: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Meta Class

In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses. Among those that do, the extent to which metaclasses can override any given aspect of class behavior varies. Each language has its own metaobject protocol, a set of rules that govern how objects, classes, and metaclasses interact.

A metaclass handles the followings :

- the base definition the described class is derived from ,

- the metaclass type (dynamic, generic or capsule),

- the described class name

- the described class tag identification

- the default described class size (sizeof class),

- "a default constructor pointer" of the described class (only if it is a dynamic one).

A metaclass handles static attributes too. Their definitions are as follow :

- all declared metaclasses list access for the binary and its potential modules,

- a function that searches for a metaclass from a given class name,

- a function that searches for a metaclass from a given class tag identification,

- a function that checks a derivation status between two metaclasses,

- a function that searches for derived metaclass described classes definition,

- a function that returns the declared metaclasses list hierarchy as a tree.

The Object Model

Object Oriented technology Page 21

Page 22: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

The object model is central to the method and uses a diagram which is similar to the ERDs used in Yourdon. Classes and their attributes and operations are shown, together with relationships between classes. The relationships which can be drawn include inheritance, aggregation and association. The modelling syntax is rich enough to represent the majority of 00 constructs in common use.

The diagram shows the symbol for a class in OMT notation. The attributes and operations correspond to C++ data and function members.

Here we are illustrating single inheritance. The land and water vehicle classes are subclasses of vehicle.

Object Oriented technology Page 22

Page 23: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

The diamond symbol is used to show aggregation or 'part-of relationships

Object Oriented technology Page 23

Page 24: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

. The above diagram shows how the notation would be used for the design of a petrol station control system. Note that we have also used the association notation (e.g. between the CONSOLE and the PUMP), which is similar to a relationship in entity-relationship notations.

The Dynamic Model

The dynamic model consists of a state transition diagram (STD) for each of the classes in the object model.

Object Oriented technology Page 24

Page 25: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

The STD notation is that defined by Harel (1987), which allows for decomposable states. This is richer and more powerful than that commonly used in Yourdon.

The Functional Model

The functional model uses a hierarchy of data flow diagrams (DFDs) in a similar fashion to the Yourdon method. However, the DFDs are not very well integrated with the object and dynamic models and it is difficult to imagine many projects making extensive use of the functional model.

Some aspects of the functional model are, however, quite useful. For example, the context diagram is vital for defining the scope of the system. Also, it may be possible to strengthen the real-time aspects of the method by introducing the concept of a processor and task model using DFDs.

Object Oriented technology Page 25

Page 26: OOT(unit I According to rgtu syllabus)

Venkteshwar Institute Of Tech.Indore

Summary:

OMT is a very useful, expressive object-oriented analysis and design method. The object model allows a smooth transition from analysis, through design, to code - especially when systems are implemented in an OO language such as C++. The dynamic model is very useful for illustrating the way in which the system responds to events. However, the functional model must be used with great care in order to avoid distorting the structure of the final application.

Object Oriented technology Page 26