Monday 6 April 2015

Inheritance in c++

Inheritance:one of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class
Base & Derived Classes:
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
Type of Inheritance:
We hardly use protected or private inheritance but public inheritance is commonly used. While using different type of inheritance, following rules are applied:

1.Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the publicand protected members of the base class.

2.Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

3.Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class

Consider a base class Shape and its derived class Rectangle as follows:
#include <iostream.h>
class Shape  // Base class
    {
public:
voidsetWidth(int w)
          {
width = w;
           }
voidsetHeight(int h)
          {
height = h;
          }
protected:
int width;
int height;
      };
                                                // Derived class
class Rectangle: public Shape
{
public:
intgetArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout<< "Total area: " <<Rect.getArea() <<endl;
return 0;
}















Virtual Function:A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.
What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.
It's possible that you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.
class Shape {
protected:
int width, height;
public:
Shape(int a=0, int b=0)
{
width = a;
height = b;
}
// pure virtual function
virtualint area() = 0;
};

The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function

No comments:

Post a Comment