Solved

Operator overloading

Posted on 2004-04-06
7
488 Views
Last Modified: 2010-04-01
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
Comment
Question by:rmvprasad
7 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
Comment Utility
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
 

Author Comment

by:rmvprasad
Comment Utility
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
Don't forget to return the istream input in your operator>>
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
If you want to access private members of your Date class, you need to make operator>> a friend of Date.
0
 

Author Comment

by:rmvprasad
Comment Utility
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
 
LVL 4

Accepted Solution

by:
PerryDK earned 30 total points
Comment Utility
************* 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now