Topics

6/recent/ticker-posts

Object Oriented programming language about Constructors (Part 2)

Constructors Part 2: 

3. Overloaded Constructor: The process of using 2 different types of constructor in a single program is called as overloaded constructor.

Program to understand Overloaded Constructor:

 #include<iostream>

 using namespace std;

   class simple

    {

   private :

   float p,n,r,s;

 public :

simple( )

    {

   p = 2000;

   n = 2.5;

   r = 3.2;

}

simple(float x,float y,float z)

   {

   p = x;

   n = y;

    r = z;

     }

   void show( )

    {

    cout<<"p : "<<p<<endl;

   cout<<"n : "<<n<<endl;

   cout<<"r : "<<r<<endl;

    }

   void process( )

    {

    s = (p * n * r)/100;

    }

   void output( )

    {

   cout<<"Simple Interest : "<<s<<endl;

    }

    };


int main( )

{

simple s,x(3000,2,5);

s.show( );

s.process( );

s.output( );

x.show( );

x.process( );

x.output( );

return 0;

}

4.Copy constructor:  It is used to declare and initialise the object from another object.

Program to understand Copy Constructor:

  #include<iostream.h>

  using namespace std;

  class copy

  {

  private :

  int a;

  public :

 copy(int x)

  {

  a = x;

  }

  void show( )

   {

   cout<<"The value of a : "<<a<<endl;

   }

   };

   int main( )

      {

   copy c(10);

    copy y(c);

    c.show( );

  y.show( );

  return 0;


}

Note: The programs given here are pre compiled and pre runned and if the user want to run the programs again they are free to use the compiler or the button given after each program




Post a Comment

0 Comments