Thursday, 7 November 2013

Inheritance

A programming technique that is used to reuse an existing class to build a new class is known inheritance .The new class inheritance.The new class inherits all the all the behavior of the original class.  The existing class that is reuse  to create a new class is known as super class,base class.The existing class that is reused to create a new class is called super class.
Inheritance is one of the most powerful features of object oriented programming .The basic principle of inheritance is that sub each class share common properties with class class form which it is derived .The child class inherit all capability of th parent class and can add its own capability.

Example
Suppose we have a class name vehicle.The subclass of this may share similar properties such as wheels and motor etc.A subclass may have its own particular characteristics.For example, a subclass bus may have seas for people but another subclass Truck may have space to carry goods.
A class of animal can be dividing into subclass like mammals,amphibians,insect,reptiles.
                      Advantages of Inheritance

  • Reuse Ability
                                  inheritance allow the developer to reuse existing code in situation

  • Save Time and Effort
                                              Inheritance saves a lot of time and effort write the same class again.The reuse ability of existing classes allows the program to work only on new classes.

  •  Increases Program Structure and Reuse Ability
                                                                              A super class is already compiled and tested properly.This class can used in a new application without compiling it again .The use of exist in class increases program reliability.
                                            Categories of Inheritance

  • Single Inheritance
                                        A type of inheritance in which child class is derived single parent class is known as single inheritance.The child in this inheritance inherits all data member and member function parent class.It can also add further capability of its  own.

  • Multiple Inheritance
                                            A type of inheritance in which a child class is derived from multiple parent classes is known multiple inheritance.The child class in this inheritance inherits all data member and function of  all parent classes.It can also add further capability of its own
                                             Protected Access Specifier
The private access specifier of a class are only accessible in the class in which they are declared.The public data member are accessible from any in the program.The protected access specifier is different from private and public access specifier.It is specially used inheritance.It allow a protected data member to be accessed from all  derived  classes but not form anywhere else in the program.It means that child class can access all protected member of its parent member.
     
                                         Accessing Member of Parent Class
An important issue inheritance in the accessibility of base class member the object class from derived.It is known as accessibility  .
                        Accessing Constructors of Parent Class
The object write class can access certain constructors of the parent class.It will use as appropriate constructor from parent class if no constructor is declared in derived class.

                                             Function Overriding
The process of declaring  member function in derived class  with same name  and same signature as in parent class is known as function overriding.
Function overriding allow the user to use same name to calling the member function of different class .When a member function is overridden int he derived class the object derived class cannot access the function of parent class However, the  function parent class can access by using scope operator.

                                                  Types of Inheritance
A parent can be inherited  using private,protected or private type inheritance.The type of inheritance define the access status of parent class member int he derived class .

                                                   Public Inheritance
In public inheritance ,the access status of parent class member  in the derived class remain the same.The public member parent class become public member of derived class.The protected member of parent class become protected member of derived class .The private member of parent class become private member of derived class.
 SYNTAX

                            class child_class : public parent_class
                              {
                                       body of the class
                                    }
                                 

                                        Protected Inheritance
In public inheritance the access status of parent class members in the derived class is restricted.The public of parent  class become protected member of derived class .The protected member of parent class becomes protected members of  derived class.The private data member class become protected class.

SYNTAX
                       class child_class : protected parent_class
                            {
                                 body of the class
                            }

                                  Private Inheritance
In public inheritance the access status of parent class members in the derived class is restricted.The private ,protected  and public members of parent class all become the private member of derived class

SYNTAX
                       class child_class : private parent_class
                            {
                                 body of the class
                            }

                       Multilevel Inheritance with Parameters
The member function in multilevel inheritance can the value to the member function parent class .When a function is overridden,the member function in child class calls the member function in the parent class .It can also send parameters value to parent class function.

                                             Multilevel Inheritance 
A type of inheritance in which a derived class inherit multiple base classes is known as multiple inheritance..In multiple inheritance , the derived class combines the member of all base classes.
                 
                                                         Constructors in   Multilevel  Inheritance 
 In multiple inheritance the constructors in base classes child classes are executed when object of derived class is created is created .The constructors may accept parameters or they can be without parameters.



Wednesday, 6 November 2013

Polymorphism

The word polymorphism is a combination f two word and morphism . Poly means many and morphism means form.In object-oriented programming, polymorphism is the ability of object different types to respond to function of the same name. The user dos not have to know the exact type of of the object in advance.The behavior of the object can be implemented at run time. It is called late binding or dynamic binding.Polymorphism is implemented b using virtual functions.
                                           Pointer to objects
A ointer can also refer to an object of a class.The member of an object can be accessed through pointer by using he symbol->.The symbol is known as member access operator.

  • Syntax
                             The syntax of referencing an object with pointer is as follows:
                         
                                     ptr->member
                                      ptr                  It is the name of the pointer that reference an object.
                                      ->                   It is the member access operator that is used to access                                                                                    a member of object.
                                     member            It is the name of the class member to be  accessed.



                                                               Array of Pointers to objects
An can store same type of data.An array of pointers can store memory address of the object of same class.It allow the to create a large number of objects in memory using new operator and process them easily using loops.Each element of array will refer in a different object tn the memory.
                                                                Pointers and inheritance
Pointers have very important capability of storing the address of different objects.A pointer can store the of object whose type is same as the type pointer.Suppose there are three classes A,A and C. The class A is he parent class and class B is the child and class C is the child .A pointer ptr of class class A can store the address all of a object of A as well B and C.
           
                                                                  Virtual Functions
Virtual maens exiting in effect but not in reality .A type of function that appears to exist in some part of program but dose not exist really is called  virtual function.Virtual function are used to implement polymorphism.They enable the user to execute completely different function by the same function call.

                                                                   Early Binding
The assignment of type to variable and experssion at compilation time is known as early binding.It is also called static binding.The early binding occurs every thing required to call a function is known as compile time.Early binding enable the compiler to know executly which function will be called when a certain statement is executed.
When a program is compiled,the the compiler check the function calls and decides which which function to be executed.This process takes during compilation process in normal program with function.

                                                                     Late Binding
The assignment of type to variables and expressions at execution time is known as late binding.It is also called dynamic binding.The late binding occurs when some information to call a function  is decided execution time .The compiler dose not known at compile time which function will be executed.It provide more flexibility.
The use of virtual function to implement  polymorphism is an example of late binding.In virtual function, the compiler dose not know at compile time which object is referred  by the pointer.The compiler executes the function depending on the contents of the pointer rather than type of compiler.
                                                         Pure Virtual Function
A type of  virtual function that has no body is known as pure virtual function . A function can be declared as pure virtual function by adding two things.

  • The Keyword virtual at the start function declared.
  • The=0 at the end of function declared.
The pure virtual function are used int he classes which are not use directly.The user can inherit the class and then override the pure virtual function in the child class.
                                                            Abstract Classes
A type of class that contains any pure virtual function is called abstract class. An abstract class cannot u be used directly.It means the no object of an abstract class can be created .However, a child class inherit an abstract class and use it overriding its pure virtual function.
The  abstract classes are used to create a model class.Any user who creates a new class by inheriting a new abstract class has to override the pure virtual functions.The child also become a abstract class if any of the pure virtual function is not overridden in child class.
Syntax
The abstract  of declared pure virtual function ia as follows


                                 Virtual return-type function-name()=0;

                                                   virtual Base Classes
A class that  is niherit by child class using virtual keyword is called virtual base class.The virtual base  classes are necessary  in a situation where the member of base class is duplicated in multilevel inheritance.