Ad

Saturday, October 06, 2007

Overloading the Parenthesis () Operator

First, I want to apologize to my readers for not being able to post lately, partly due to me being very busy these days;-)

As we all know, there are certain ways by which we can pass data to objects (of class). We can pass values during the time of construction as below:

  class-name ob-name(values);

Or we may define a member function to accept data which can be called as below:

  class-name ob-name;
  ob-name.set(values);  

Where set is the member function of the class.

But actually there is one more way to do so, yeah you guessed it right!, by overloading the parenthesis () operator.

Parenthesis () operator like other operators is overloaded with the following prototype:

  ret-type operator()(arg1, arg2,...);

It is a unary operator hence the only argument passed to this function (implicitly) is this pointer. The argument list may contain any number of arguments as you wish to pass.

The following example program illustrates the overloading of parenthesis () operator in C++.


  // Overloading Parenthesis ()
  // Operator
  #include <iostream.h>

  class myclass
  {
    int a,b;

  public:
    myclass(){}
    myclass(int,int);
    myclass operator()(int,int);
    void show();
  };

  // ------------------------
  // --- MEMBER FUNCTIONS ---
  myclass::myclass(int x, int y)
  {
    a=x;
    b=y;
  }

  myclass myclass::operator()(int x, int y)
  {
    a=x;
    b=y;

    return *this;
  }

  void myclass::show()
  {
    cout<<a<<endl<<b<<endl;
  }
  // --- MEMBER FUNCTIONS ---
  // ------------------------

  void main()
  {
    myclass ob1(10,20);
    myclass ob2;

    ob1.show();

    // it's a nice way to pass
    // values, otherwise we
    // would have to define and
    // call a member/friend function
    // to do so
    ob2(100,200);
    ob2.show();
  }

Related Articles:

2 comments:

  1. uuuThis post is for ure appreciation.

    u are doing very good work man providing error free programing examples with core description.

    i am an Amature in programing but ure EFFORT FOR PROGRAMMING IS ADMIRABLE. ITs true man u r truly a core master.

    as an appreciation here is a program that inputs year and DISplays it calender.



    /*This program prints a calendar for a year specified. The user enters a
    year for the calendar and the programs automatically prints the calendar
    in text format.
    The codes are: day_code (0 = Sun, 1 = Mon, etc.)
    leap_year (0 = no leap year, 1 = leap year) */


    #include stdio.h
    #include stdlib.h
    #define TRUE 1
    #define FALSE 0
    int get_day_code (int year);
    int get_leap_year (int year);
    void print_calendar (FILE *fout, int year, int day_code, int leap_year);
    int get_year (void);
    main()
    {

    int year, day_code, leap_year;

    FILE *fout;

    fout = fopen ("calendar.txt", "w");

    year = get_year();

    day_code = get_day_code (year);

    leap_year = get_leap_year (year);

    print_calendar(fout, year, day_code, leap_year);

    printf("Open up \'calendar.txt\' to see your calendar...\n");

    system("pause");
    return 0;
    }

    int get_year (void)
    {
    int year;
    printf ("Enter a year: ");
    scanf ("%d", &year);
    return year;
    }
    int get_day_code (int year)
    {
    int day_code;
    int x1, x2, x3;
    x1 = (year - 1.)/ 4.0;
    x2 = (year - 1.)/ 100.;
    x3 = (year - 1.)/ 400.;
    day_code = (year + x1 - x2 + x3) %7;
    return day_code;
    }
    int get_leap_year (int year)
    {

    //if((year% 4) == 0 );
    if(year% 4==0 && year%100 != 0 || year%400==0)
    return TRUE;
    else return FALSE;

    }
    void print_calendar (FILE *fout, int year, int day_code, int leap_year) //function header
    {
    int days_in_month, /* number of days in month currently
    being printed */
    day, /* counter for day of month */
    month; /* month = 1 is Jan, month = 2 is Feb, etc. */
    fprintf (fout," %d", year);
    for ( month = 1; month <= 12; month++ ) {
    switch ( month ) { /* print name and set days_in_month */
    case 1:
    fprintf(fout,"\n\nJanuary" );
    days_in_month = 31;
    break;
    case 2:
    fprintf(fout,"\n\nFebruary" );
    days_in_month = leap_year ? 29 : 28;
    break;
    case 3:
    fprintf(fout, "\n\nMarch" );
    days_in_month = 31;
    break;
    case 4:
    fprintf(fout,"\n\nApril" );
    days_in_month = 30;
    break;
    case 5:
    fprintf(fout,"\n\nMay" );
    days_in_month = 31;
    break;
    case 6:
    fprintf(fout,"\n\nJune" );
    days_in_month = 30;
    break;
    case 7:
    fprintf(fout,"\n\nJuly" );
    days_in_month = 31;
    break;
    case 8:
    fprintf(fout,"\n\nAugust" );
    days_in_month = 31;
    break;
    case 9:
    fprintf(fout,"\n\nSeptember" );
    days_in_month = 30;
    break;
    case 10:
    fprintf(fout,"\n\nOctober" );
    days_in_month = 31;
    break;
    case 11:
    fprintf(fout,"\n\nNovember" );
    days_in_month = 30;
    break;
    case 12:
    fprintf(fout,"\n\nDecember" );
    days_in_month = 31;
    break;
    }
    fprintf(fout,"\n\nSun Mon Tue Wed Thu Fri Sat\n" );
    /* advance printer to correct position for first date */
    for ( day = 1; day <= 1 + day_code * 5; day++ )
    fprintf(fout," " );
    /* print the dates for one month */
    for ( day = 1; day <= days_in_month; day++ ) {
    fprintf(fout,"%2d", day );
    if ( ( day + day_code ) % 7 > 0 ) /* before Sat? */
    /* move to next day in same week */
    fprintf(fout," " );
    else /* skip to next line to start with Sun */
    fprintf(fout, "\n " );
    }
    /* set day_code for next month to begin */
    day_code = ( day_code + days_in_month ) % 7;
    }
    }


    Kindly if u post latest trick in c c++ or Java then kindly send them to skkracherr@yahoo.com

    For ure info iam student of BS(CS)-Networking

    BYE

    CONTINUE TO DO URE GOOD WORK MATE.

    ReplyDelete
  2. Oh, great @skkracher.

    Thank you so much for adding your program.

    For nice peoples like you, we have Let Us Grow Our Community, in case you didn't know.

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.