Constructor Example



Constructor Program Example


1. Create a class called Mountain with data members name, height and location.

 Use constructor that initialize the members to the values passed to it as parameters. 

A function cmpHeight() to compare height of two objects and function displayInfo() to display

 information of mountain. In main create two objects of the class mountain and display the

 information of mountain with greatest height.

   #include<iostream.h>

#include<conio.h>

#include<string.h>

class Mountain

 {

 private:

        char name[100];

        char location[100];

        int height;

    public:

           Mountain()

            {  }

            Mountain(char n[],char l[],int h)

             {

              strcpy(name,n);

              strcpy(location,l);

              height=h;

               }

               void display();

               void compht(Mountain,Mountain);

                };

                 void Mountain::display()

                     {

                      cout<<"Name of mountain:"<<name;

                      cout<<endl;

                      cout<<"Location of mountain:"<<location;

                      cout<<endl;

                      cout<<"Height of mountain:"<<height;

                      cout<<endl;

                         }

            void Mountain:: compht(Mountain m1,Mountain m2)

             {

               if(m1.height>m2.height)

                 {

                  m1.display();

                     }

                  else if (m2.height>m1.height)

                     {

                      m2.display();

                        }

                   else

                  {

                   cout<<"Both mountains are of equal height";

                      }

                         }

             void main()

               {

                 Mountain m, m1("Everest","Nepal",8848),m2("K2","Pakistan",8000);

                 cout<<endl;

                 m1.display();

                 cout<<endl;

                 m2.display();

                 cout<<endl;

                 cout<<"The details of highest mountain:"<<endl;

                 m.compht(m1,m2);

                 getch();

                  }

Output:


2.Program to compare height using constructor and friend function.

#include <iostream.h>

#include <conio.h>

 class mountain

 {

  private:

          char name[20];

          char location[20];

          int height;

  public:

         void getdetails();

         void display();

         friend void compht( mountain m1,mountain m2);

            };

  void mountain::getdetails()

   {

     cout<<"Enter the name of mountain:";

     cin>>name;

     cout<<"Enter the location of mountain:";

     cin>>location;

     cout<<"Enter the height of mountain:";

     cin>>height;

         }

   void mountain::display()

     {

       cout<<"Name of mountain:"<<name;

       cout<<endl;

       cout<<"Location of mountain:"<<location;

       cout<<endl;

       cout<<"Height of mountain:"<<height;

       cout<<endl;

         }

  void compht(mountain m1,mountain m2)

   {

    if(m1.height>m2.height)

      {

       m1.display();

         }

    else if (m2.height>m1.height)

     {

      m2.display();

        }

      else

        {

         cout<<"Both mountains are of equal height";

            }

              }

  void main()

   {

     mountain M1,M2;

     M1.getdetails();

     cout<<endl;

     M2.getdetails();

     cout<<endl;

     M1.display();

     cout<<endl;

     M2.display();

     cout<<endl;

     cout<<"The details of highest mountain:"<<endl;

     compht(M1,M2);

      getch();

       }


Output:


3. Use contstructor to input the number and  display the number as rational number.

 

#include<iostream.h>

#include<conio.h>

class rational

 {

 private:

         int num,den;

 public:

        rational();

        rational(int n,int d);

        void input();

        void display();

           };

  rational::rational()

  {

   num=0;

   den=0;

    }

   rational::rational(int n,int d)

    {

     num=n;

     den=d;

       }

  void rational::input()

 {

  cout<<"Enter value of numerator:";

  cin>>num;

  cout<<"Enter the value of denumerator:";

  cin>>den;

    }

  void rational::display()

   {

    cout<<"Rational Number is: "<<num<<"/"<<den;

    cout<<endl;

      }

 void main()

  {

   rational r;

   r.input();

   cout<<endl;

   r.display();

   getch();

     }

Output:



4  Program to add two complex number using constructor.


#include<iostream.h>

#include<conio.h>

class Complex

{

private:

        int real,img;

public:

       Complex();

       Complex(int r,int i);

       void display();

       void addComplex(Complex C1,Complex C2);

         };

 Complex::Complex()

  {

    real=0;

    img=0;

     }

  Complex::Complex(int r,int i)

    {

     real=r;

     img=i;

        }

  void Complex::display()

  {

   if(img>0)

     {

      cout<<real<<"+j"<<img;

        }

     else

      {

       cout<<real<<"-j"<<(-1)*img;

         }

          }

 void Complex ::addComplex(Complex C1,Complex C2)

  {

    real=C1.real+C2.real;

    img=C1.img+C2.img;

     }

  void main()

  {

    Complex C1(5,2),C2(2,3),C3;

    cout<<"The first complex number:";

    C1.display();

    cout<<endl;

    cout<<"The second complex number:";

    C2.display();

    cout<<endl;

    C3.addComplex(C1,C2);

    cout<<endl;

    cout<<"Addition of two complex number:";

    C3.display();

    getch();

     }

Output:



5.Program to add two complex number using friend function.Use constructor to initialize the object.


#include<iostream.h>

#include<conio.h>

class Complex

{

private:

        int real,img;

public:

       Complex();

       Complex(int r,int i);

       void display();

       friend void addComplex(Complex C1,Complex C2);

         };

 Complex::Complex()

  {

    real=0;

    img=0;

     }

  Complex::Complex(int r,int i)

    {

     real=r;

     img=i;

        }

  void Complex::display()

  {

   if(img>0)

     {

      cout<<real<<"+j"<<img;

        }

     else

      {

       cout<<real<<"-j"<<(-1)*img;

         }

          }

 void addComplex(Complex C1,Complex C2)

  {

  Complex C3;

    C3.real=C1.real+C2.real;

    C3.img=C1.img+C2.img;

    C3.display();

     }

  void main()

  {

    Complex C1(5,2),C2(2,3);

    cout<<"The first complex number:";

    C1.display();

    cout<<endl;

    cout<<"The second complex number:";

    C2.display();

    cout<<endl;

    cout<<"Addition of two complex number:";

    addComplex(C1,C2);

    getch();

     }

Output:

6. Program to add two complex number using friend function and return it.Use constructor to initialize it.


#include<iostream.h>
#include<conio.h>
class Complex
{
private:
        int real,img;
public:
       Complex();
       Complex(int r,int i);
       void display();
       friend Complex addComplex(Complex C1,Complex C2);

         };
 Complex::Complex()
  {
    real=0;
    img=0;
     }
  Complex::Complex(int r,int i)
    {
     real=r;
     img=i;
        }
  void Complex::display()
  {
   if(img>0)
     {
      cout<<real<<"+j"<<img;
        }
     else
      {
       cout<<real<<"-j"<<(-1)*img;
         }
          }
 Complex addComplex(Complex C1,Complex C2)
  {
  Complex C3;
    C3.real=C1.real+C2.real;
    C3.img=C1.img+C2.img;
    C3.display();
     }
  void main()
  {
    Complex C1(5,2),C2(2,3);
    cout<<"The first complex number:";
    C1.display();
    cout<<endl;
    cout<<"The second complex number:";
    C2.display();
    cout<<endl;
    cout<<"Addition of two complex number:";
    addComplex(C1,C2);
    getch();
     }
Output:/

/* 

#include<iostream.h>
#include<conio.h>
class Complex
{
private:
        int real,img;
public:
       Complex();
       Complex(int r,int i);
       void display();
       Complex addComplex(Complex C1,Complex C2);

         };
 Complex::Complex()
  {
    real=0;
    img=0;
     }
  Complex::Complex(int r,int i)
    {
     real=r;
     img=i;
        }
  void Complex::display()
  {
   if(img>0)
     {
      cout<<real<<"+j"<<img;
        }
     else
      {
       cout<<real<<"-j"<<(-1)*img;
         }
          }
 Complex Complex:: addComplex(Complex C1,Complex C2)
  {
  Complex C3;
    C3.real=C1.real+C2.real;
    C3.img=C1.img+C2.img;
    return C3;
     }
  void main()
  {
    Complex C1(5,2),C2(2,3),C3;
    cout<<"The first complex number:";
    C1.display();
    cout<<endl;
    cout<<"The second complex number:";
    C2.display();
    cout<<endl;
    cout<<"Addition of two complex number:";
    C3=C3.addComplex(C1,C2);
    C3.display();
    getch();
     }

*/
7.Program to add two time that have hour,minute and second using constructor.

#include<iostream.h>
#include<conio.h>
class Time
{
 private:
        int hr,min,sec;
 public:
        Time();
        Time(int h,int m,int s);
        void display();
        void addTime(Time ,Time );
           };
 Time::Time()
  {
   hr=0;
   min=0;
   sec=0;
    }
 Time::Time(int h,int m,int s)
 {
  hr=h;
  min=m;
  sec=s;
   }
 void Time::display()
 {
  cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
   }
 void Time:: addTime(Time t1,Time t2)
 {
  sec=t1.sec+t2.sec;
  min=sec/60;
  sec=sec%60;
  min=min+t1.min+t2.min;
  hr=min/60;
  min=min%60;
  hr=hr+t1.hr+t2.hr;
    }
    void main()
    {
     Time T1(2,4,5),T2(4,50,4),T3;
     cout<<"The given first time is :"<<endl;
     T1.display();
     cout<<endl;
     cout<<" The given second time is :"<<endl;
     T2.display();
     cout<<endl;
     cout<<endl;
     T3.addTime(T1,T2);
     cout<<"The resultant time after addition is :"<<endl;
     T3.display();
      getch();
       }

Output:



8. Program to add two time using friend function.Use constructor to initialize the object.

 #include<iostream.h>
#include<conio.h>
class Time
{
 private:
        int hr,min,sec;
 public:
        Time();
        Time(int h,int m,int s);
        void display();
        friend void addTime(Time ,Time );
           };
 Time::Time()
  {
   hr=0;
   min=0;
   sec=0;
    }
 Time::Time(int h,int m,int s)
 {
  hr=h;
  min=m;
  sec=s;
   }
 void Time::display()
 {
  cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
   }
 void addTime(Time t1,Time t2)
 {
 Time t3;
  t3.sec=t1.sec+t2.sec;
  t3.min=t3.sec/60;
  t3.sec=t3.sec%60;
  t3.min=t3.min+t1.min+t2.min;
  t3.hr=t3.min/60;
  t3.min=t3.min%60;
  t3.hr=t3.hr+t1.hr+t2.hr;
  cout<<"Time after additon is:";
  t3.display();
    }
    void main()
    {
     Time t1(2,4,5),t2(4,50,4);
     cout<<"The given first time is :"<<endl;
     t1.display();
     cout<<endl;
     cout<<" The given second time is :"<<endl;
     t2.display();
     cout<<endl;
     cout<<endl;
     addTime(t1,t2);

      getch();
       }
Output:



9.Program to add two distance having feet and inches using constructor.

#include<iostream.h>
#include<conio.h>

class Distance
{
private:
         int feet,inches;
public:
       Distance();
       Distance(int f,int i);
       void display();
       void add(Distance d1,Distance d2);
        };
 Distance::Distance()
 {
  feet=0;
  inches=0;
   }
   Distance::Distance(int f,int i)
    {
     feet=f;
     inches=i;
     }
  void Distance::display()
   {
    cout<<feet<<"-feet "<<inches<<"-inches"<<endl;
       }
 void Distance:: add(Distance d1,Distance d2)
 {
   int inches1;
   inches=d1.inches + d2.inches;
   inches1=inches/12;
   inches=inches%12;
   feet=inches1+ d1.feet + d2.feet;
      }
  void main()
  {
   Distance D1(2,6),D2(6,8),D3;
   cout<<"The first value of distance is:"<<endl;
   D1.display();
   cout<<endl;
   cout<<"The second value of distance is:"<<endl;
   D2.display();
   cout<<endl;
   D3.add(D1,D2);
   cout<<"The sum of distance is:";
   cout<<endl;
   D3.display();
   getch();
    }
Output:


10. Program to add two distance having feet and inches using friend function.Use constructor to initialize the object.


#include<iostream.h>
#include<conio.h>

class Distance
{
private:
         int feet,inches;
public:
       Distance();
       Distance(int f,int i);
       void display();
       friend void add(Distance d1,Distance d2);
        };
 Distance::Distance()
 {
  feet=0;
  inches=0;
   }
   Distance::Distance(int f,int i)
    {
     feet=f;
     inches=i;
     }
  void Distance::display()
   {
    cout<<feet<<"-feet "<<inches<<"-inches"<<endl;
       }
  void add(Distance d1,Distance d2)
 {
   int inches1;
   Distance d3;

   d3.inches=d1.inches + d2.inches;
   inches1=d3.inches/12;
   d3.inches=d3.inches%12;
   d3.feet=inches1+ d1.feet + d2.feet;
   cout<<"Sum of two distance is:";
   cout<<endl;S
   d3.display();
      }
  void main()
  {
   Distance D1(7,6),D2(3,3);
   cout<<"The first value of distance is:"<<endl;
   D1.display();
   cout<<endl;
   cout<<"The second value of distance is:"<<endl;
   D2.display();
   cout<<endl;
   add(D1,D2);
   getch();
    }
Output:

.

Comments