Showing posts with label Overloading. Show all posts
Showing posts with label Overloading. Show all posts

Constructor Overloading Code for C++

This program show how to create an Overloaded Constructor in a C++ program. The Constructor is declared in the class date and later defined outside the class. The class date consists of three Integer variables which contain the day, month and year of the date wished to be stored. There are two constructors defined with different Signatures i.e. one is using String and the other using the three Integer values, one for each of month, date and year.

Constructor Overloading using Rectangles in C++


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

class CRectangle
{
    int width, height;
  public:
    void set_values (int, int);
    int area (void) {return (width * height);
      }
};

void CRectangle::set_values (int a, int b)
{
  width = a;
  height = b;
}

int main ()
{
  CRectangle a, *b, *c;
  CRectangle * d = new CRectangle[2];
  b= new CRectangle;
  c= &a;
  a.set_values (1,2);
  b->set_values (3,4);
  d->set_values (5,6);
  d[1].set_values (7,8);
  cout << "a area: " << a.area() << endl;
  cout << "*b area: " << b->area() << endl;
  cout << "*c area: " << c->area() << endl;
  cout << "d[0] area: " << d[0].area() << endl;
  cout << "d[1] area: " << d[1].area() << endl;
  return 0;
}

Constructor Overloading


//constructor overloading II
//econstr4.cpp to demo multiple constructors.

#include<iostream.h>
#include<conio.h>
class time
   { int   hrs;
     int mins;
     int secs;
     public:
          time()   {hrs=0;mins=0;secs=0;}
          time(int h,int m,int s);
          time(long h) ;
          void gettime();
          void showtime();
    };
main()
{
      clrscr();
      cout<< "econstr4.cpp start" <<endl;
      time t1;                                          //----1----
      cout<<"showing t1:";
      t1.showtime();
      time t2(9,15,0);                                  //----2----
      cout<<  "college opens        time  t2";
      t2.showtime();
      time t3;                                          //----3----
      cout<<"lanch breack starts    time  t3";
      t3.showtime();
      time t4(51300);                                   //----4----
      cout<<"lunch breack ends      time  t4";
      t4.showtime();
      time t5 = time(17,15,0);                         //----5----
      cout<<"the day ends           time  t5";
      t5.showtime();
      getch();
      return 0;
}
  time :: time(int h,int m,int s)
    {
            hrs = h; mins = m; secs = s;
    }
  time ::  time(long h)
    {
            long temp;
            secs = h % 60;
            temp =h/ 60;
            mins = temp % 60;
            hrs = temp / 60;
    }

  void time::gettime()
     {
             cout<<"enter time as hh mm ss:";

         cin>> hrs >> mins >>secs;
     }

  void time :: showtime()
     {
             cout<< hrs << ':' << mins <<':' << secs <<endl;
     }

Overloading Basic Programming Concept in C++


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

class PERIOD
{
    int days, months,years;
  public:
    void show();
    PERIOD(int d,int m,int y);
};

void PERIOD::show
{
    cout<<days<<months<<years<<endl;
}

PERIOD::PERIOD(int d,int m,int y)
{
    days=d;months=m;years=y;
}

int main () 
{
  clrscr();
  PERIOD p1(10,6,100);
  p1.show();
  PERIOD p2(p1);
  p2.show();
  getch();
  return 0;
}

Custom Search