Tuesday, May 31, 2011

Inheritance


Introduction
Inheritance is the process of creating new classes, called derived class, from existing or base classes. The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own. The base class is unchanged by this process.
Inheritance permits code reusability. Once a base class is written and debugged, it need not be touched again but can nevertheless be adapted to work in different situations. Reusing existing code saves time and money and increases a program’s reliability. Inheritance can also help in the original conceptualization of a programming problem, and in the overall design of the program. It is also help to add some enhancements to the base class. One result of reusability is the ease of distributing class libraries. A programmer can use a class created by another persons or company, and without modifying it, derived other classes for it that are suited to particular situations.

Saturday, April 30, 2011

Routine in destination object


Routine in destination object
When the conversion routine is in the destination class, we use a one-argument constructor. However, things are complicated by the fact that the constructor in the destination class must be able to access the data in source class to perform the conversion. The polar data – radius & angle –are private, so we must provide special function to allow direct access it.

#include<iostream.h>
#include<conio.h>
#include <math.h>
class polar
{
      double radius,angle;          //x and y cordinate
   public:
      polar()        //constructor
      { radius=0.0;
            angle=0.0; }

    polar(double r, double a)
      {   radius=r; angle=a;   }

   void display()
   { cout<<"(" <<radius<<","<<angle<<")"; }

      double getr()
      {  return radius;   }

      double geta()
      {   return angle;   }
};

class rectangular
{
      double xco,yco;          //x and y cordinate
   public:
      rectangular()          //constructor
      {   xco=0.0; yco=0.0;   }

      rectangular(double x, double y)
      {   xco=x; yco=y;   }

      rectangular (polar p)
      {
             double r=p.getr();
             double a=p.geta();
             xco=r * cos(a);
             yco=a * sin(a);
      }

      void display()
      {    cout<<"(" <<xco<<","<<yco<<")";  }
};

void main()
{
   rectangular rec;         //rectangular using constructor1
   polar pol(10.0,0.785398);     //polar using constructor1
   rec=pol;        //convert polar to rectangular using conversion function
   clrscr();
   cout<<"\nPolar Values=";
   pol.display();
   cout<<"\nrectangular Values=";
   rec.display();
   getch();
}

Conversion between objects of Different Classes


Conversion between objects of Different Classes

The same two methods as same as conversion between basic types and user-defined types also apply to conversions between two user-defined types. That is, you can use a one-argument constructor, or you can use a conversion function. The choice depends on whether you want to put the conversion routine in the class specifier of the source object or of the destination object.

Routine in source object
When the conversion routine is in the source class, it is commonly accomplished using a conversion function. The two classes used in the next program are rectangular and polar class. The rectangular class is similar in that its objects are points in a two dimensional plane. However, it uses a rectangular coordinate system, where the location of each point is specified by x and y coordinates. Its member functions are similar to those for polar but are adapted to rectangular coordinates.
An example:
#include <math.h>
class rectangular
{
   double xco,yco;          //x and y cordinate
   public:
      rectangular()        //constructor
      {   xco=0.0; yco=0.0;   }
      rectangular(double x, double y)
      {   xco=x; yco=y;   }
      void display()
      {    cout<<"(" <<xco<<","<<yco<<")";     }
};

class polar
      {
      double radius,angle;          //x and y cordinate
     public:
      polar()       //constructor
      {   radius=0.0; angle=0.0;   }
      polar(double r, double a)
      {   radius=r; angle=a;   }
      void display()
      {    cout<<"(" <<radius<<","<<angle<<")";      }
      operator rectangular()     //casting operator function
            { double x=radius * cos(angle);
               double y=radius * sin(angle);
               return rectangular(x,y);   }  //temporary object
};
void main()
{
   rectangular rec;
   polar pol(10.0,0.785398);
   rec=pol;  //or rec=rectangular(pol);
                 // It is equivalent to pol.operator rectangular()
   cout<<"\nPolar Values=";
   pol.display();
   cout<<"\nRec Values=";
   rec.display();
   }

Monday, February 21, 2011

Inheritance: Base classes & Derived Classes


Inheritance: Base classes & Derived Classes
The existing class from which another class class is derived is known as base class and the new created created is derived class.  A base class may be a direct base class of a derived class, or  an indirect base class of a derived class. A direct base class of a derived class is explicitly listed in that derived class’ header with the colon (:) notation when that derived class is declared. An indirect base class is not explicitly listed in the derived class’ header; rather the indirect base class is inherited from two or more levels up the class hierarchy.  A derived class can be defined by specifying its relationship with the base class in addition to its own details. The general forms of defining a derived class is:

class derived-classname : visibility-mode base-class-name
{
             ……. // member of derived class
};

e.g.
    class ABC           //base class
             {        ……..//members of class ABC             }


class XYZ : public ABC
         {
           ……//members of class XYZ
           }


Then, a derive class XYZ can be created as following:

class XYZ : private ABC
         {
             ……//members of class XYZ
           }








The colon indicates that the derived-class-name is derived from the base-class-name. The visibility-mode is optional and if present may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived. When a base class is privately inherited by a derived class, ‘public members’ of the base class become private members of derived class and therefore they are accessible only within derived class. When a base class is publicly inherited, ‘public members’ of the base class becomes ‘public members’ of the derived class. In both cases, the private members of base class are not inherited and therefore, the private members of a base class will never become the members of its derived class.