Sie sind auf Seite 1von 21

AKS-1666

PAGE-

LANGUAGE = C++
Q. WHAT IS OBJECT ORIENTED PROGRAMMING? DESCRIBE THE FEATURE OF OBJECT ORIENTED PROGRAMMING? ANS. The Object Oriented Programming is that type of element which can be
uniquely identified. That means those types of elements are called object that can be distinguished from other element to identify an object uniquely. Every object must have some identifying properties to identify the object. The identifying properties are called data member in C++, Instance variable in JAVA, Attributes in DBMS and FIELD in general. So it is also possible to say, Objects are the encapsulated form of data - members. Every object has some specific task and we know that without function task can not be performed. So every object must content some function to perform task .So finally we can say that objects are the encapsulated form of data member and member function. Functions are common for a set of objects, so they are created at the time of class declaration and occupy memory in the primary memory RAM. Data members different for every objects, and they are allocates their memory after the creation of the object. Objects can be created at the time of compilation or execution. After allocation of memory by the data members, a linking is established, which is called data binding or encapsulation between data member and member function and create a complete object, which is the basic component of Object Oriented Programming. Object Oriented Programming is those type of programming in which member function and data member allocates their memory at different time and at different allocation, and at the time of compilation or execution make a link between them to create a complete object, which is the basic component of the programming. Alternately we can say that Object Oriented Programming is that type of programming, in which, data member and member function allocates partitioned memory in different time and at the time of compilation or execution a linking is established between data member and member function to create a complete object, which is the basic component of programming.

AKS-1666

PAGE-

FEATURES OF OBJECT ORIENTED PROGRAMMING


1. 2. 3. Class and Objects. Encapsulation and abstraction. Inheritance.

4. Polymorphism. 5. 6. 7. 8. Constructor and Destructor. Exception. Generic programming. Message passing.

Objects are distinguishable elements. That means those type of element which can be distinguished from other element is called Object. It is also possible to say that, that type of element which can be uniquely identified is called object. It is also possible to say that class type variable is called Object. (Class is users define data type to designed blue print of Object.) To identify an Object uniquely some identifying properties must be present with the Object. Using the identifying properties an Object can be uniquely identified. Every Object must have some function to perform task. That type of Object can not be present, which can not perform any task. Functions present in Object are called member function in C++. Member functions are common for a set of Object. So member function created a single copy and allocates their memory at the time of designing of Object. That means a single copy of member function is created at the time of class declaration. Data member allocates their memory at the time of creation of Object. Object can be created at the time of compilation or execution. A binding process is performed between data member and member function to create a complete Object. This binding process is also called Encapsulation. If the data member allocates their memory statically at the time of compilation then, the binding process is also performed at the time of compilation. This type of binding is called compile time binding or early binding or static binding. If data member allocates their memory dynamically at the time of execution then the binding process is also performed at the time of execution. This type of binding is called run time binding or execution time binding or late binding or dynamic binding. After binding a
2

AKS-1666

PAGE-

complete object is created which can be use in Object Oriented Programming as a single unit? We know that member functions are common for a set of Object, so only one copy of member function is created and shared by all the Objects. But data members are created individually for all the Objects. That means data members are not common for all the Objects. In C++ programming language access specifies or visibility is arising to specify common components and individual component. In C++ three types of visibility are present:-

Private Protected Public Since member functions are common for a set of Object, so
member functions are generally present in the public section, because all the Objects access the member function. Data member are individual for every Object so generally they are declared in the private section or protected section. Private and protected section can not be directly access from the outside of the Object. Private section can not be inherit into other classes but protected can be inherit as well as public can also be inherit.

CLASS:-

Common name of a set of Object is called class . It is also

possible to say that imaginary designed of Object is called class. Alternately we can say that logical designed or blue print of Objects is called class. Finally we can say that class is user-define data type to create new data type as the requirement of the user by combining different standard variable and their associated function. It is also possible to say that class is a user defines data type to create a new data type by encapsulating data members and their associated member functions as the requirement of the user by using appropriate visibility Private, Protected and Public. When a class is declared, data members can not allocate their memory in the primary memory RAM, so it is not possible to initialize the data member. Since member functions are common for a set of Objects, so member function allocates their memory at the time of class declaration, so user can initialized data members in the function at the time of declaration.

AKS-1666

PAGE-

In C++ three types of visibility are present of data member and member
function.

Private Protected Public Private visibility specifies that this section can not be

access directly from out side of the class but can access by the protected and public section of the same class. Private section can not be inherited into other classes. Generally data member are individual for every Object, so data members are declared in the private section of the class. User can also declare member function in the private section if they want. Constructor and destructor can not be declared in the private section. Protected visibility is also specified that, this section can not be accessed directly from the out side of the class but can be access by the private and public section of the same class. Protected section can be inherited into other classes as the requirement of the user. Generally data members are declared in the protected section. It is also possible to declare member function in the private section. The only difference between private and protected is that private is not inheritable but protected is inheritable. So we can say that inheritable private is called protected. Constructor and destructor can not be declared in the protected section. Public visibility specifies that this section can be directly accessed from the out side of the class. That means that type of component can be present in the public section which is common for all the objects. Generally member functions are present in the public section. It is also possible to declare data member in the public section. Constructor and destructor must be declared in the public section of the class. Public section can be inherited into other classes as the choice of the user.

AKS-1666

PAGE-

Inheritance is a process by which we can create a new class using existing classes. The existing classes are called base classes in C++, super classes in JAVA and parent classes in General. The new classes are called derived classes in C++, sub classes in JAVA and child classes in general. The main purpose of
inheritance is to implement re-use ability of existing code .The main advantages of inheritance are:-

(1) (2) (3) (4)

Using inheritance existing code can be re-used in other classes. That means code re-use ability can be implemented by using inheritance. Productivity of the programmer can be increased using inheritance. Product cost as well as maintenance cost can be decreased, so acceptability

of the product can be increased. Inheritance also increases code security by providing extra layer around the component in every level of inheritance.

TYPES OF INHERITANCE
According to the visibility Inheritance is divided in three categories:-

(1) (2) (3)

Private inheritance Protected inheritance Public inheritance

AKS-1666

PAGE-

In private inheritance the protected section and public section of base class are
inherit into the private section of derived class. By default private inheritance is implemented in C++. The private section of base class can not be inherited into derived classes in any situation. Graphically we can represent private inheritance as:-

In Protected inheritance the private section of base class can not


be inherit. The protected section and public section of base class will be inherited into the protected section of derived class. Graphically we can represent protected inheritance as:-

AKS-1666

PAGE-

In public inheritance the private section of base class can not be inherit into derived classes. The protected section of base class will be inherited into the protected section of derived class. The public section of base class will be inherited into the public section of derived class. In inheritance visibility of base class component can not be decrease in any situation. Graphically we can represent public inheritance as:-

According to the number of base class and derived class inheritance is


divided in five categories:-

(1) Simple and single inheritance (2) Multilevel (3) Tree / Hierarchical (4) Multiple (5) Hybrid
7

AKS-1666

PAGE-

(1)

Simple or single inheritance

Simple 0r single inheritance is those

type of inheritance in which a single base class is used to create a single derived class. That means, in single inheritance, number of base class and derived class is only one. Graphically we can represent single inheritance as:-

(2) Multilevel

inheritance

multilevel inheritance is those type of

inheritance in which a derived class is used as a base class to create another derive class. In multilevel inheritance, single inheritance is repeatedly used. Graphically we can represent multilevel inheritance as:-

AKS-1666

PAGE-

3) Tree structure

Tree structure or hierarchical inheritance is those type of

inheritance in which a single base class is used to create one or more then one derived classes. It is also possible to say that, the tree structure inheritance is those type of inheritance in which every derived class can be at most one base class. Graphically we can represent tree structure inheritance as:-

AKS-1666

PAGE-

10

(4)

Multiple inheritances Multiple inheritance is those type of


inheritance in which more then one base class are inherit into a single derived class. Multiple inheritances are the opposite of tree structure inheritance. In C++ multiple inheritances is possible but in JAVA multiple inheritance can not be possible. Because more then one super class can not be inherit into a single sub-class. Multiple inheritance can be represented graphically as:-

10

AKS-1666

PAGE-

11

Hybrid inheritance Hybrid inheritance is the combination of tree structure


inheritance and multiple inheritances. In C++ hybrid inheritance is implemented using the concept of virtual base class, but in JAVA hybrid inheritance can not be implemented. Because more then one super class can not be inherit in to a single sub

11

AKS-1666

PAGE-

12

class. Graphically we can represent hybrid inheritance as:-

12

AKS-1666

PAGE-

13

Constructor are the special member function thats name are same with the class
name and which are automatically invoked at the time of object creation. Constructor must be declared in the public section of the class. It is not possible to declare constructor in the private section and protected section. Constructor has no return type even VOID constructor can take argument or cannot be. According to the argument constructors can be overloaded. That means, in a single class more then one constructor can be present. Constructors are divided in three categories, according to the argument:-

(1) (2) (3)

Default constructor Simple parameterized constructor Copy constructor Default constructor is that type of constructor which has no return type and no argument. C++ compiler as well as JAVA
compiler will automatically supply a do-nothing default constructor to satisfy that the compile and create object. Do-nothing default constructor is that type of default constructor which has void definition. That means, no statement is present in the definition. Without constructor object can not be created in any Object Oriented Programming language.

(1) Default constructor

(2) Simple parameterized constructor

If the constructor can

takes simple variable as argument then the constructor is called simple parameterized constructor. In simple parameterized constructor any type of argument and any number of arguments supply at the time of invocation. In a single class more then one simple parameterized constructor can present but only one default constructor can be present.

(3) Copy constructor If a constructor can takes argument object using


the concept of reference variable, then that type of constructor is called copy constructor. That means, copy constructor is special type of parameterized constructor which takes only object as argument using the concept of

(Reference location different name).


reference variable

variables are the alias, same memory

Constructor can not be inherit into derived classes but can be


invoked using derived class constructor. If base class contains a default constructor then it is not necessary to present a constructor in the derived class. If base class
13

AKS-1666

PAGE-

14

contains a parameterized class constructor then the derived class must contains a constructor to invoke base class constructor.

NOTE:-

If a class contains copy constructor then a default constructor must be

present in the same class but if the class contains a single parameterized constructor then it is not necessary to present a default constructor in the same class. But recommendation is present to declare a default constructor in the same class.

DESTRUCTOR
Destructor is also a special member function thats name is same with the class name
and which is automatically invoked after the scope of object. The destructor must be declaring in the public section of the class, it is not possible to declare destructor in the private and protected section of the class. Destructor has no return type even VOID. Destructor has no argument, so destructor can not be overloaded. A tilde (~) sign must be present before the actual name of the destructor. The main purpose of destructor is to release the memory spaces occupied by the data members of the object using the operator DELETE. Destructor works as the opposite direction of constructor. That means, destructor release the memory spaces occupied by the objects in the reverse order as the creation of the objects. Destructor can not be inherit in derived classes but can be automatically invoked from the derived classes.

14

AKS-1666

PAGE-

15

The general meaning of polymorphism is same thing in many forms. In


programming language polymorphism is those type of concept in which a particular function or operator can take different form using different types of argument or different number of arguments and different types of operand and can perform different task accordingly. If a function can take different form and can perform different task, then those type of programming system is called function overloading. Similarly if a particular operator can perform different task on different types of operand besides the original task of the operator, this type of programming system is called operator overloading. That means, finally we can say that polymorphism is those type of programming concept in which a particular function can perform different task using the concept of function overloading and operator overloading. Alternately we can say that polymorphism is that type of programming concept which is implemented using the concept of function overloading and operator overloading. In real life every object is overloaded as well as the member function of every object are also overloaded. That means, in real life a particular object can take different form according to the position of the object as well as functions present in the objects is also overloaded and can perform different task using a single function according to the form of the function.

Advantages of Polymorphism:

Polymorphism is implemented using the concept of function overloading. That means, a single function can perform different task according to the number of arguments and types of argument. So it is not necessary to remember the function name, only the types of argument and number of argument should be remember. Polymorphism is also implemented using the concept of operator overloading. That means, a single operator can perform different operation on different types of operand besides the original task of the operator. That means using a single operator user can perform any types of operation, which is not possible normally.

15

AKS-1666

PAGE-

16

Operator overloading
Operator overloading is those type of programming system in which a particular
operator can perform different type of operation on different types of operand. The operator which is used to implement operator overloading is called overloaded operator. For example + operator can addition operation on numeric types of operand. But using the concept of operator overloading strings, objects, structure type variable and any other types of variable can be added. That means, + operator can be overloaded. In C++ the concept of operator overloading is present but in JAVA operator overloading is not present. In C++ almost all operator can be overloaded except the following.

(1) (2) (3) (4)

:: ? .

(scope resolution operator ) ( Size of( ) operator) (turnery operator) (class member access operator) In C++ three types of

operators are present in which turnery operator can not be overloaded, so only unary and binary operator can be overloaded. Operator overloading is implemented using the keyword operator. In C++ operator overloading is implemented by using two techniques:-

(a) (b)

Using member function. Using friend function.

overloading is implemented using member function, if the first operand is an object. If the first operand is not an object the member function is not capable to overload this type of operation, then the friend function is ultimate solution. If the first operand is not an object and user wants to overload the operator then using friend function it can be possible. Friend function has the wide scope to overload operators. That type of operator can also be overloaded using friend function which is overloaded using member function. In operator overloading an operator function is implicitly called and perform appropriate task according to the statement present in the function. Operator function is always called implicitly but user can also called the function except licitly (externally).
16

Operator

AKS-1666

PAGE-

17

Function overloading
overloading is that type of programming technique in which a particular function can perform different task by taking different types of argument and different number of arguments. That means, function overloading is those type of programming technique in which a particular function can take different form by taking different types of arguments and different number of arguments and can perform different task according to the form of the function. In C++ function can not be distinguish according to the return type. Function can be distinguished using the types of arguments and numbers of arguments. Function overloading is implemented by using two techniques:-

Function

(1) (2) (3)

Function overloading using early binding. Function overloading using late binding. Function overloading using virtual function.

We know that functions are common for a set of objects, so functions allocate their
memory at the time of class declaration. Data member are common for all the objects and they allocate their memory at the time of object creation and a link is established between the data member and member function which is called data binding or encapsulation. If data member allocates their memory at the time of compilation statically, then after that binding is performed between data member and member function. This type of binding is also known as early binding or compilation time binding or static binding. After the binding, function can perform operation on the data member. If function overloading is implemented using the early binding, then it is called function overloading using static binding. It is also possible to allocate memory for the data members dynamically at the time of execution using the new operator, then after allocate the memory. A link is also established between the data member and the member function. This type of linking is called late binding or run time binding or dynamic binding. Function overloading can also be implemented using the concept of late binding. Function overloading using late binding can also be implemented using the concept of virtual function.

17

AKS-1666

PAGE-

18

Exceptions are a type of error besides the syntax error and logical error. That means, exceptions are not arises during compilation, they are arises during execution. Alternately we can say that exceptions are those types of error which are arises during execution and they are neither syntax error nor logical error. For example:- division by zero, number format exception, array index out of bounds etc. In C++ two types of exceptions are arise.

(a) (b)

Synchronous exception. Asynchronous exception. Synchronous are those types

of exception which are arises due to incapability of statement. Synchronous exception are also called software exception because this type of exception arises due to software failure. C++ has the capability to handle synchronous types of execution. Asynchronous exception is those types of exception which are arising due to hardware failure. This type of exception is also called hardware exception. C++ has no capability to handle asynchronous types of exception. In C++ as well as any Object Oriented Programming have three tools to handle exceptions.

(1) (2) (3)

Try. Throw. Catch.

The try block is used to identify the exception. After identification of the exception
using the throw statement the exception are thrown by the try block to the catch block. In a particular try block more then one exception can be identified, so more then one throw statement can be present. In a particular program more then one try block can be present. More then one catch block can also be present corresponding to a single try block. The throw statement is only used to communicate between try and catch block and transfer the exception from try block to catch block. Throw statement has two forms:-

(1)

Throw
18

AKS-1666

PAGE-

19

(2)

Throw( )
The first form of throw is only use to transfer the control from try

block to catch block when an exception is arise. The second form of throw can take argument according to the types of exception and throw the exception to the catch block as well as transfer to the catch block. Generally a particular throw statement can active a particular catch block but it is possible to active a particular catch block corresponding to more then one throw statement. The catch block is used to take appropriate action on the exception thrown by the throw statement from the try block. The catch block can not correct the exception only appropriate message is displayed in the catch block and control transferred to the end of the program.

Generic programming is those types of programming concept in which type


independent code for all the type which maintain the same algorithm. That means, generic programming is those type of programming concept which can create generalized code which can be executed according to the type supplied by the user. Using this concept user can create a general functions which are type independent and can be executed for all the types which maintains same logic and algorithm. This type of programming concept is called function templates and the function is called generic function. For example:- the swap function to interchange two numeric value is different from other numeric type only in the type of the variable. Using function template user can create a single code for all the numeric types to interchange two numeric values this type of function is called tem plated function and programming concept is called function template. It is also possible to generalized single class for all the types. This maintains same function and different types by using the same algorithm. This type of programming concept is called a class template and the class is called tem plated class. When a class is a tem plated class then all the member function of the class are also tem plated function.

19

AKS-1666

PAGE-

20

Generic programming is those type of programming concept by which we can create type independent code for all the types, which maintains the same algorithm. That means, using the concept of generic programming a general code can be created which can be executed commonly for all the types maintains same algorithm to perform the task. This type of programming concept is called template. Using this programming concept a particular function like swap ( ) can interchange. Any numeric type of value as well as any user defines types of value.

Template is divided in to two categories:-

(1) Function template. (2) Class templates.


Function template is those type of programming concept in which a general function can handle all types of variable which maintains same algorithm. That means using the concept of function template we can create type independent function definition, and the function can handle all the types of variable which maintains same algorithm. Finally we can say that function template is style to create general function for all the types. Class template is that type of programming concept by which we can create a general class. For all the general class and for all the types of function which can be handle using the same logic. If a class is tem plated then all the member functions are automatically becomes a generic function. Using the concept of class template, a single program can handle all the types of variable which maintain same logic. In C++ system defined tem plated classes are present to handle different types of variable and functions. In C++ standard template library is present which is introduced by MENG LEE and ALEXZANDOR STEPANOV. In the template library the following templates classes are present:List, Vector, Queue, Map, Multi map, Set, Multi set, Stack, Array etc the tem plated class contains a tem plated function. The list class contains the following popular function:20

All

AKS-1666

PAGE-

21

Clear, Back, Begin, Erase, End, Reverse, Insert, Sort etc.

One

of the most important features of Object Oriented Programming is message passing. We know that member functions can access the data members only which are present inside the class. So it is not possible to interlink two or more then two different classes using member function. But situation are arise in which interlinking between different classes are necessary to create a complete system. Message passing is those type of programming concept in which different classes and objects can exchange information between them to achieve the concept of message passing the concept of friend function is arise. Friend function is not a member function of a particular class. The keyword friend is used to declare friend function before the normal declaration of the function. Friend function is not a member function of a particular class, so it has no visibility. That means, friend function can be declare in the public section or protected section or private section. Friend function is defined in the out side of the class as same as C function but not inside in the class. Friend function is not a member function of a particular class so it is called as same as C function not as the member function. Friend function can not be inherited in the derived classes. Using friend function exchange of information between different classes is possible. Friend function must be declare in all the classes between exchanging of information is performed.

21

Das könnte Ihnen auch gefallen