Está en la página 1de 17

Procedure oriented programming Some characteristics exhibited by POP are : Emphasis is on doing things (algorithm) Large programs are

divided into smaller programs known as function. Most of the functions share global data. Dave move openly around the system. Functions transform data from one form to another. Employs top-down approach in program.

Main function

Function 1

Function 2

Function 3

Function 4

Function 5

Function 6

Function 7

Object-oriented programming paradigm Object oriented programming treats data as critical element in the program development and does not allow it to flow freely around the system, if ties data more closely to the function that operate on it, and protects it from accidental modification from outside function. OOP allows decomposition of a problem into a number of entities called objects and then builds data and function around there objects. the organization of data and function in OOP is shown below:

Object A

Object - B

Data Functions

Data Functions

Object - C

Functions Data

Fig : Organization of data and function in OOP SOME OF THE FEATURES OF OOP ARE: Emphasis is on data rather than procedure Programs are divided into what are known as object. Date structure are designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure. Data is hidden and can not be accessed by external function. Objects may be communicate with each other through function. New data and function can be easily added whenever necessary Follows bottom of approach in program design.

BASIC CONCEPT OF OOP There includes:Objects Classes Data abstraction and encapsulation Inheritance Polymorphism Dynamic binding Message passing

OBJECT Objects are basic run time entities in an object-oriented system. They may represent in person a place, a bank account, a table of data or any item that the program has to handle. They may also represent user defined data such as vectors time and lists. Each objects contains data and code to manipulate the data.Objects can interact without having to known details of each others data or code. OBJECT: STUDENT DATA Name Date of birth Marks

FUNCTIONS Total Average

CLASSES Objects are containing data and code to manipulate that data. The entire set of data and code of an object can be made a user defined data type with the help of classes. Objects are variable of the class. Once a class has been defined, we can create any number of object belonging to that class. Each object is associated with the data of type class with which they are created. A class thus a collection of objects of similar type, For example: mango, apple and orange are members of the class fruit. Classes are user-defined data types and behave like the built in types of a programming language. DATA ABSTRACTION & ENCAPSULATION The wrapping up of data and function into a single unit is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessable to the outside world, and any those function, which are wrapped in the class, can access it. Abstraction refers to the act of representing essential features without including the background details or explanations. Classes are the concept of abstract attributes suchas size, weight and cost, and functions to operate on these attributes, The attributes are some-times called data members. Since the classes are the concept of data abstraction, they are known as abstract data types(ADT) INHERITANCE Inheritance is the process by which objects of one class acquire the properties of objects of another class. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing to an class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combine feature of both the classes. Inheritance is a technique of information in a hierarchical form. it is like a child inheriting the features of its parents. The technique of building new classes from the existing classes is called inheritance Forms of inheritance:

The derived class inherits some or any of the features of the base class depending on the visibility mode and level of inheritance. single inheritance multiple inheritance hierarchical inheritance multilevel inheritance hybrid inheritance multipath inheritance

POLYMORPHISM Polymorphism, a Greek term, means the ability to take more than one form. It is a mechanism, that allows a single name/operator to be associated with different operations depending on the type of data passed to it. In C++ it is achieved by function overloading, operator overloading, dynamic binding (virtual function). See the following Figure that a single function name can be used to handle different number and different types arguments. Using a single function name to perform different types of tasks is known as function overloading.

Shape Draw()

Circle object Draw(circle)


DYNAMIC BINDING

Box object Draw (box)

Triangle Obj. Draw(triangle)

Binding refers to the linking of a procedure call to the code to the executed in response to the call. Dynamic Binding means the code associated with the given procedure call is not known until the time of the call at run time. It is associated with polymorphism and inheritance. MESSAGE PASSING An OOP 1. 2. 3. consist a set of objects that communicate with each other steps: Creating classes that define objects and its behaviour Creating objects from class definations. Establishing communication among objects. communicate with one another by sending employee.salary(name); object message information and receiving

Objects information.

Include files Class declaration Member functions definitions Main function program

Message passing involves specifying the name of the object, the name of the function (manage)&the information to be sent. ADVANTAGES/BENEFITS OF OOP OOP offers several benefits to both the program designer and the user. The main advantages are: 1. Through inheritance, we can eliminate redundant code and extend the use of existing classes. 2. We can build programs from the standard working modules that communicate with one another. 3. The principles of data hiding the programmer to build secure program. 4. It is possible to have multiple instances of an object to co-exist without any interference. 5. It is possible to map objects in problem domain. 6. It is easy to partition the work in a project based on objects. 7. System can be easily operated. 8. Software complexity can be easily managed. APPLICATION OF OOP The promising areas application of OOP include: Real-Time system Simulation and modeling Object oriented database Hyper text, Hyper media and expertext Al and expert system Neural network and parallel programming Decision support and office automation system CIM/CAM/CAD systems.

EXAMPLES OF OOP : C++, JAVA, ADS, Eiffel, Simula, Smalltalk Basic Structure of C++

It is common practice to organize a program into three separate files. The class declarations are placed in a header file and the definitions of member functions go into another file. This approach enables the programmer to separate the abstract specification of the interface (class) from the implementation details. Finally the main program that uses the class is places in a third file which includes the previous two files as well as any other files required. Application of C++ These include: C++ allows us to create hierarchy related objects. C++ able to make the real world problems. C++ programs are maintainable and expendable when new features need to be implemented. C++ will replace C as general-purpose language.

Some of the claimed advantages of C++ are: New programs would be developed in less time because old code can be reduced. Creating and using new data types would be easier than C. The memory management in C++ would be easier and more transparent. Programs would be less bug-prone, as C++ uses a stricter syntax and type checking. Data hiding , the usage of data by one part while other program part while other program parts cannot access the data, would be easier to implement with C++.

Introduction to tokens: Keyword The keyword implement specific C++ language features. They are explicitly reserved identifiers cannot be used as names for the names for the programs variable or other user-defined program elements. Key words are reserved to the compiler for use by the language. You cannot define classes, variables, or the function that have these keywords as their names. The list is a bit arbitrary, as some of the keywords are specific are to a given compiler. Keyword Identifier Constant String Operator

Auto Continue Enum

break default extern

case delete float

char do for

class double friend

const else goto

etc.

Identifier &constant Identifiers refer to the name of variable, function, array etc. They are the fundamental s requirement of any language has its own rules for naming there identifiers: Rules: - Only alphabetical character, digit and underscore are permitted. - The name cannot be start with a digit. - Uppercase and lowercase letters are significant. - A declared keyword cannot be used as a variable name. Constant refer to fixed value that do not change during the execution of a program C++ support several kinds of literal constant. They include integer, character, floating-point number and string. Literal constant do not have the memory location. Example: 123 decimal integers 12.34 floating point a character constants C++ string constant C++ also recognize backslash character constant available in C. What is variable? In C++ a variable is a place to store the information. A variable is allocation in your computers memory in which you can store a value and from you can retrieve that value. Your computers memory can be viewed as a series of cubbyholes. Each cubbyhole is one of many, many such holes all lined up. Each cubbyholes or memory location is numbered sequentially. These numbers are known as memory address. Table: Variable Types. Type unsigned short int short int unsigned long int long int int(16 bit) int(32 bit) Size 2 bytes 2 bytes 4 bytes 4 bytes 2 bytes 4 bytes Value 0 to 65,535 -32,768 to 32,767 0 to 4,294,967,295 -2,147,483,648 to 2,147,483,647 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647

unsigned int(16 bit) unsigned int(32 bit) Char Float Double Reference Variable

2 bytes 4 bytes 1 byte 4 bytes 8 bytes

0 to 65,535 0 to 4,294,967,295 256 character values 1.2e-38 to 3.4e38 2.2e-308 to 1.8e308

C++ introduced a new kind of variable. A reference variable provides an alias for a previously defined variable. For example, if we make the variable sum a reverence to the variable total, then sum and total can be used interchangeably to represent variable. A reference variable is created as follows: Data-type & reference-name = variable name Example: float total=100; float &sum=total; Total is a float variable that has already been deceleration; sum is the alternative name declared to represent the variable total. Both the variable refers to the same data object in the memory. Now the statements: Cout<<total; And Cout<<sum; Both print the value 100. The statement. Total=total+10; Will change the value of both total and sum to 110.likewise, the assignment Sum=0; Will change the value of the variable to zero. OPERATOR IN C++ :: ::* ->* .* delete endl new setw Scope resolution operator Pointer-to-pointer declaration Pointer-to-member operator Pointer-to-member operator Memory release operator Line feed operator Memory allocation operator Field width operator

SCOPE RESOLUTION OPERATOR The syntax of C++ introduces a number of new operators, of which the scope resolution operator :: is described first. This operator can be used in situations where a global variable exists with the same name as a local variable. The scope resolution operator gets you out of situations in which the name the compiler chooses by default (the nearest name) isnt what you want. For example, suppose you have a structure with a local identifier a, and you want to select a global identifier a from inside a member function. The compiler would default to choosing the local one, so you must tell it to do otherwise. When you want to specify a global name using scope resolution,

you use the operator with nothing in front of it. Heres an example that shows global scope resolution for both a variable and a function. #include <stdio.h> int counter = 50; //global variable int main() { for(register int counter = 1; //this refers to the counter < 10; //local variable counter++) { printf(%d/n,::counter) //global variable printf(%d\n,counter); //local variable } return(0); } In this code fragment the scope operator is used to address a global variable instead of the local variable with the same name. MEMBER DEREFERENCING OPERATORS Operator ::* * ->* Function to declare a pointer to a member of a class to access a member using object name and a pointer to that member to access a member using a pointer to the object and a pointer to that member

MEMORY MANAGEMENT OPERATORS new and delete The C++ language defines two operators, which are specific for the allocation and de-allocation of memory. These operators are new and delete. Pointer-variable = new data-type The most basic example of the use of these operators is given below. An int pointer variable is used to point to memory, which is allocated by the operator new. This memory is later released by the operator delete. int *ip; ip = new int; //any other statements delete ip; Note that new and delete are operators and therefore do not require parentheses, which are required for functions like malloc() and free(). The operator delete returns void, the operator new returns pointer to the kind of memory thats asked for by its argument

Allocating and de-allocating arrays When the operator new is used to allocate an array, the size of the variable is placed between square brackets following the type. int *intarr; intarr = new int[20]; //allocates 20 ints

The syntactical rule for the operator new is that this operator must be followed by a type, optionally followed by a number in square brackets. The type and number specification lead to an expression, which is used by the compiler to deduce its size in C an expression like sizeof (int [20]) might be used. An array is de-allocated by using the operator delete: delete [] int arr; in this statement the array operator [] indicate that an array is being deallocated. The rule of thumb here is whenever new is followed by [], delete should be followed by it too. The new operator offers the following advantages over the function malloc() 1. 2. 3. 4. It automatically computes the size of the data object It automatically returns the correct pointer type It is possible to initialize the object while creating the memory space Like any operator, new and delete can be overloaded

MANIPULATORS MANIPULATORS are operators that are used to format the data display. The most commonly used manipulators are endl and setw. The endl operator, when used in an output statement, cause a linefeed to be inserted. It has the same effect as using the new line character \n. For example: cout<< welcome to c++<<endl; cout<< good bye; Now suppose we have three variable have cost as: a=1234 b=23 c=123 Then we can display them as follow: a=1234 b= 23 c = 123 is real output this number are right-justified. The setw do this job. It is used as under

cout<<setw(5)<<a<<endl; here field width is 5 for printing values of a. 1 2 3

TYPE CAST OPERATOR C++ permits explicit type conversion of variable or expression using the types cast operator (type-name) expression //c notation type-name (expression) // c++ notation Example: Average=sum/float (i) Elements of user defined functions : In order to make use of user-define function, we need to establish three elements that are related to functions. 1. 2. 3. 1. Function Declaration (Function Prototyping) Function Call (Calling) Function Definition (Actual Definition)

Function Declaration : The calling program should declare any function (like a declaration of a variable) that is to be used later in the program. This is known as the function declaration or function prototype. For example, void findmax(int, int); int getmin(int,int); float volume(int, int, float);

2.

Function Call : In order to use the function, we need to invoke it at a required place in the program. This is known as the function call. For example, findmax(3,10); x=getmin(7,4); y=volume(2,3,4.075);

3.

Function Definition : The function definition is an independent program module that is specially written to implement the requirements of the function. For example, void findmax(int, x, int y) { if (x > =y) printf(%d,x); else

printf(%d,y); function

A function definition also known as implementation and include the following elements : 1. 2. 3. 4. 5. 6.

Function Name Function Type List of parameters Local variable declaration Function statements A return statement (if function return a value)

A general format of a function definition to implement these elements is given below.

function_type function_name (parameter list) { local variable declaration; execuatable statement1; execuatable statement2; return statement; }

RETURN BY REFERENCE A Function can also return a reference. Consider the following function. int & max(int &x ,int &y) { if (x>y) return x; else return y; } since return type of max() is int &, the function return reference to x or y (and not values). Then a function call such as max(a,b) will yield a reference to either a or b depending on their values. INLINE FUNCTION When a function is called, it takes a lot of time in executing a series of instructions for task such as jumping to the function, saving registers, pushing arguments into the stack and returning to the calling function. When function

is small, a substantial percentage of execution time may be spent in such overhead. To eliminate the cost of calls to small function, C++ proposes a new feature called inline function. It is expanded inline when it is invoked.

inline function_header { function body }


Some of the situation where inline expansion may not work are : 1. 2. 3. 4. for function returning values. for function not returning values, if a return statement exists. if function contain static values. if inline function is recursive.

FUNCTION OVERLOADING Overloading term refers to the use of the same thing for different purpose. C++ also permits overloading of functions. This means that we can use the same function name to create functions that perform a variety of different task. This is known as function polymorphism in OOP. Declaration. int add(int a, int b); int add(int a, int b, int c); double add (double a, double b); double add (int p, double q); //prototype //prototype //prototype //prototype 1 2 3 4

The compiler first tries to find an exact match in which the types of actual arguments are the same and use that function. If an exact match is not found, the compiler uses the integral promotions to the actual arguments, to find the match. If all the steps fail, then the compiler will try the use define conversion in combination with integral promotions and built in conversions to find a unique match. SPECIFYING A CLASS

class class_name { private: variable declaration; function declaration; public: variable declaration; function declaration; };

The class declaration is similar to struct declaration. The keyword class specifies that what follows is an abstract data of type class_name. The class body contains declaration of variable and functions. These functions and variable are called members. They are usually grouped under two sections, private and public, the visibility labels of class. By default, class member are Private, and they are only accessed within the class. The public members can be accessed from outside the class also. The variables declared inside the class are known as data members and functions are known as member functions. The binding of data and functions together into a single class-type variable are referred as encapsulation. PRIVATE MEMBER FUNCTION A private member function can only be called by another function that of its class. Even an object cannot invoke a private member function using dot operator. Example: class sample {

int m; void read(void); public: void update(void); void write (void);

}; if s1 is an object of sample class, then s1. read(); //would not work; object cannot access private members. Therefore, the function read() can called by function update or write () function only. Void sample :: update(void) { read(); } STATIC DATA MEMBERS A data member of class can be qualified as static. The properties of static member variable are similar to that of C static operator. It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member crated for the entire class and is shared by all the objects of that class. It is visible only within the class, but its lifetime is the entire program. int item :: count; // definition of static data member

The type and scope of each static member variable must be defined outside the class definition. Here item is a class and count is a static data member. Object 1 no. object 2 no. object 3 no.

100

200

300

count (static data member : count , shared by all object)


STATIC MEMBER FUNCTION Like static data member variable, we can have static member function also. It have following properties. A static function can have access to only other static members (function or variables) declared in same class. A static member function can be called using the class name (instead of object) as follows : class_name :: function_name Example : Class test { int code; static int count; public : void secode(void) { code = ++count; } void show code (void) { cout << code << endl; } static void showcount (void) { cout << count << end; } }; int test :: count; main() { test t1, t2; t1.setcode();

t2.setcode(); test :: showcount(); test t3; t3.setcode(); test :: showcount(); t1.showcode(); t2.showcode(); t3.showcode(); } The output of the above program is : 2 2 1 2 3 FRIEND FUNCTION There could be a situation where we would like two classes to share a particular function. In such situation, c++ allows the common function to be made friendly with both the classes, there by allowing the function to have access to the private data of these classes. Such function need not be a member of any of these classes. To make an outside function friendly, to class, we have to simply declare this function as a friend of the class as shown below in example. class ABC; class XYZ { int x; public: void setdata(int i) { x=i; } friend void max(XYZ,ABC);

}; class ABC {

};

int a; public: void setdata(int i) { a=i; } friend void max(XYZ,ABC);

void max(XYZ m,ABC n) { if ( m.x>=n.a ) cout<<"THE MAX IS="<<m.x; else cout<<"THE MAX IS="<<n.a; } void main() { ABC s; XYZ r; clrscr(); s.setdata(88); r.setdata(33); max(r,s); getch(); } The output of above program is 88, here max ( ) function is used as friend function to operate the object of both class. The friend function have following characteristics : It is not in the scope of the class to which it has been declared as friend. Since it is not in the scope of class, it cannot be called using the object of that class. It can be invoked as normal function. Usually it has the object as arguments. It can be declared either in the private or public part of class without affecting its meaning.

También podría gustarte