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.

Inheritance


Inheritance
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.

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();
Text Box: Output of the program
Polar Values=(10,0.785398)
Rec Values=(7.071069,0.55536)


   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
Text Box: Output of the program
Polar Values=(10,0.785398)
Rec Values=(7.071069,7.071067)

};
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();
   }

Data Conversion


Data Conversion

Conversion between Basic Types:
      See standard conversions in chapter 2 for this.

Conversion between objects and Basic Types:
a)      Conversion From Basic to User defined data type:
A constructor can be used to covert basic data type to user defined data type. It may be important to know that an object can be initialized using one argument constructor as
     Square s(10); ß---à Square s=10;  // Assume that Square is class name and it has one constructor Square(int).
                
           An example:
const float MTF=3.280833;
 class distance
    {
      int feet;
      float inches;
     public:
        distance()  //constructor
             {   feet=0;   inches=0.0;   }

   distance(float meters)  //constructor with one argument
     { float floatfeet= MTF * meters;
        feet=int(floatfeet);
        inches=12*(floatfeet-feet); }

     void showdist()                    //display distances
      { cout<<feet<<"\' - "<<inches<<'\"'; }
   };


void main()
   {
      distance dist1=1.144,dist2; // uses 1 argument constructor to   
                                                   //convert meter to distance
                                                 //same as dist1(1.144)
     dist2=3.5;   // uses 1 argument constructor i.e. same as dist2(3.5).
Text Box: Output of the program:
Dist1 = 3' - 9.039279"
Dist2 = 11' - 5.794991"





















































































































      clrscr();
      cout<<"\nDist1 = ";
      dist1.showdist();
      cout<<"\nDist2 = ";
      dist2.showdist();
      getch();
   }

b)     Conversion From User defined to Basic data type:
C++ allows us to define an overloaded casting operator that could be used to covert a class type data to a basic type. Syntax for an overloaded casting operator function is:
              operator type_name()
                   {……….. }
This function converts a class type (of which it is member) data to type_name. The casting operator function should satisfy following conditions.
Ø  It must be a class member
Ø  It must not specify a return type
Ø  It must not have any arguments
        
        An example:
const float MTF=3.280833;            //meter to feet
class distance
  {
      int feet;
      float inches;
   public:
      distance()          //constructor
         {   feet=0;   inches=0.0;   }

     distance(int ft, float in)  //constructor with two arguments
      {            feet=ft;
         inches=in; }

     operator float()    //operator function
Text Box: Output of the program:
Dist1 = 1 meters
Dist2=1.666244meters





































































































































       { float fracfeet=inches/12;
         fracfeet+=float(feet);
         return (fracfeet/MTF);
       }
   };   


void main()
   {
      distance dist1(3,3.37), dist2(5,5.6);
      float mtrs=float(dist1); /*Uses conversion function to convert
                       distance to meter & equivalent to dist1.operator float()*/
      cout<<"\nDist1 = "<<mtrs<<" meters";
      mtrs=dist2;   // dist2.operator float();
      cout<<"\nDist2="<<mtrs<<"meters"; 
 }