Solved

Operator overloading

Posted on 2004-04-06
7
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10767459
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
ID: 10767507
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
ID: 10767530
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
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!

 
LVL 44

Expert Comment

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

Expert Comment

by:Karl Heinz Kremer
ID: 10767561
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
ID: 10767736
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
ID: 10768542
************* 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to clear a vector as well as how to detect empty vectors in C++.

719 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