• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

Operator overloading

how to overload  >> operator to accept an date object created by me.
I did
inline ostream& operator << (ostream& out, Date& DD){return DD.Print (out); to overload << to give an output. At the same time please give me an idea how to add two date objects. THE date class is as below
class Date {
      private:
         int day;                                                // to hold the date
         int month;                                          // to hold the month
         int year;                                          // to hold the year
         friend class Reservation;                  // Reservation class
         //a private function to validate if the month, day and year
         //entered are valid numbers
            bool vali_date (int mm, int dd, int yy);
            void increment(int);                              //utility function to add days
            
      public:
            Date();                                                //default constructor
            Date(istream& indate);                        //one parameter constructor with open input stream
            Date(char* str_date);                        //one parameter constructor to read from file
                        
            ~Date() { cerr <<"  Deleting date object" <<endl;}            // destructor
            Date& operator+=(int);                              //to modify object by adding days
            void Print()
            {
                  //cout<<"Hello i am in date class";

            }
            ostream& Print (ostream& out){
                  if(month==-1 || day ==-1 || year ==-1)
                        out <<month << " "<<day <<" " <<year;
                  else
                        out <<monthName[month] <<" "<<day <<", " <<year;
                  return out;
                  }
};
0
rmvprasad
Asked:
rmvprasad
1 Solution
 
lakshman_ceCommented:
For adding two Data objects try as seen in the following code
// vectors: overloading operators example
#include <iostream.h>

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int,int);
    CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
  x = a;
  y = b;
}

CVector CVector::operator+ (CVector param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return (temp);
}

int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;
  cout << c.x << "," << c.y;
  return 0;
}

-Lakshman
0
 
rmvprasadAuthor Commented:
how to overload >> operator to accept the date object I did the followinng way inline istream&

inline istream& operator >> (Date& DD); //declaration done in .h file

inline istream& operator >> (Date& DD)  //code given in .cpp file
{
      int d1,m1,y1;
      d1=DD.day;
      m1=DD.month;
      y1=DD.year;
}

I am getting the following errors

error C2805: binary 'operator >>' has too few parameters
C:\WINNT\Profiles\cs-student\Desktop\date\date.cpp(159) : error C2805: binary 'operator >>' has too few parameters
C:\WINNT\Profiles\cs-student\Desktop\date\date.cpp(161) : error C2248: 'day' : cannot access private member declared in class 'Date'
        c:\winnt\profiles\cs-student\desktop\date\date.h(15) : see declaration of 'day'
C:\WINNT\Profiles\cs-student\Desktop\date\date.cpp(162) : error C2248: 'month' : cannot access private member declared in class 'Date'
        c:\winnt\profiles\cs-student\desktop\date\date.h(16) : see declaration of 'month'
C:\WINNT\Profiles\cs-student\Desktop\date\date.cpp(163) : error C2248: 'year' : cannot access private member declared in class 'Date'
        c:\winnt\profiles\cs-student\desktop\date\date.h(17) : see declaration of 'year'
Error executing cl.exe.

0
 
Karl Heinz KremerCommented:
Only you can write the operator>> - I have no idea what the date format is that you want to read. You need something like this:

istream operator>>(istream & input, Date & date)
{
    // read one character at a time until you have a complete date string
    // convert the string to your Date format
}

How would you add two date objects in the real life? What is Oct 3, 1983 plus Feb 14, 2003 ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Karl Heinz KremerCommented:
Don't forget to return the istream input in your operator>>
0
 
Karl Heinz KremerCommented:
If you want to access private members of your Date class, you need to make operator>> a friend of Date.
0
 
rmvprasadAuthor Commented:
could you please tell me how to make an operator >> as a friend. I dont know how to declare an operator as a friend
0
 
PerryDKCommented:
************* PROGRAM OUTPUT**************
Vector a = (3, 1)
Vecotr b = (1, 2)
Vector c = a + b = (4, 4)


Enter a vector in the format (x, y)
(
10
,
9
)
Vector d = (10, 9)
************End Program Output***************

you can alter the << and >> operators so that they don't require the usage of parenthesis and commas just did that so output would be nice.  Makes inputting of vectors a little bit more difficult though.

In general your >> operator should be the exact opposition of the << operator

Here is actual code let me know if you have questions about it.

#include <iostream.h>

class CVector
{
public:
  CVector (const int x = 0, const int y = 0);
  CVector operator + (const CVector& v) const;
  CVector operator - (const CVector& v) const;
  CVector& operator = (const CVector& v);
  CVector& operator += (const CVector& v);
  CVector& operator -= (const CVector& v);

  friend ostream& operator << (ostream& out, const CVector& v);
  friend istream& operator >> (istream& in, CVector& v);

private:
   int x;
   int y;
};



CVector::CVector(const int x, const int y)
{
  this->x = x;
  this->y = y;
}

CVector CVector::operator + (const CVector& v) const
{
  return CVector(x + v.x, y + v.y);
}

CVector CVector::operator - (const CVector& v) const
{
  return CVector(x - v.x, y - v.y);
}

CVector& CVector::operator = (const CVector& v)
{
  x = v.x;
  y = v.x;
  return *this;
}

CVector& CVector::operator += (const CVector& v)
{
  *this = *this + v;
  return *this;
}

CVector& CVector::operator -= (const CVector& v)
{
  *this = *this - v;
  return *this;
}

ostream& operator << (ostream& out, const CVector& v)
{
  out << "(" << v.x << ", " << v.y << ")";
  return out;
}

istream& operator >> (istream& in, CVector& v)
{
  string str;
  //read off the "("
  in >> str;

  in >> v.x;

  //read off the ", "
  in >> str;

  in >> v.y;

  //read off the ")"
  in >> str;

  return in;
}

int main ()
{
  CVector a (3,1);
  CVector b (1,2);

  cout << "Vector a = " << a << endl;
  cout << "Vecotr b = " << b << endl;

  CVector c;
  c = a + b;

  cout << "Vector c = a + b = " << c << endl;

  cout << endl << endl;
  CVector d;
  cout << "Enter a vector in the format (x, y)" << endl;
  cin >> d;
  cout << "Vector d = " << d;

  return 0;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now