overload operator ++ in a vector

Pages: 12
Jun 1, 2011 at 10:36pm
Hi guys, today I was trying to make to make it, but I make more and more mistakes.So I decided to ask you for ideas.So I have a vector with three integers-day, month, and year.I want to ask you, how can I overload it with prefix and postfix, so I can increment the day, month or the year separately?I am using classes (this is just apart of my code).So here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int main()
{
    dates Newdate;
    vector <int> dayss;
    int day,month,year;
    int d=3;
    dates v1(d);
 
    
 cout<<"Day : ";
 cin>>day;
 if (day>=32)
{
 cout<<"Invalid input.Please try again\n";
 cout<<"Day : ";
 cin>>day;   
}
    cout<<"Month : ";
    cin>>month;
  if(month>=13)
{
  cout<<"Invalid input.Please try again\n";
  cout<<"Month : ";
  cin>>month;  
  }
    
    cout<<"Year : ";
 cin>>year;
  if (year<=0)
{
  cout<<"Invalid input.Please try again\n";
  cout<<"Year : ";
  cin>>year;   
}
   dayss.push_back(day);
   dayss.push_back(month);
   dayss.push_back(year);

return 0;
}
Jun 1, 2011 at 10:49pm
You would need to put this in a class, rather than in a vector. You could then overload the ++ operator and make it work however you want:

1
2
3
4
5
6
7
8
9
10
11
12
class Date
{
private:
  int day;
  int month;
  int year;

public:
  // ... various member functions to manipulate the date

  // .. overload the ++ operator here to increment the date
};
Jun 1, 2011 at 11:26pm
Mm,ok thats a good idea, thank you.I know how to use classes, but I dont know how to overload it.Can you be more specific?
Jun 1, 2011 at 11:37pm
Overloading an operator is just like writing a member function for a class:

1
2
3
4
5
6
7
8
9
10
class Date
{
public:
  Date& operator ++ ()
  {
    // do whatever you want to increment the date here

    return *this;
  }
};


Then you can do this:

1
2
3
Date mydate = /*whatever*/

++mydate; // increments the date 
Jun 1, 2011 at 11:56pm
Ok, I think I got your idea.But I got a few questions.First, what do you mean by "Date mydate = /*whatever*/".My idea is to use set and get member functions, to store the date.Is that the way to do the overloading?And with what should I fill this field :
Date& operator ++ ()
1
2
3
4
5
  {
    // do whatever you want to increment the date here

    return *this;
  }

with something like this
int i+=1?
Last edited on Jun 1, 2011 at 11:57pm
Jun 2, 2011 at 12:05am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
struct Date: std::vector <int>
  {

  Date& operator ++ ()  // prefix
    {
    ...
    }

  Date  operator ++ ( int )  // postfix
    {
    ...
    }
  };

Frankly, you don't really need a vector. Create a class of your own design.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Date
  {
  int year;
  int month;
  int day;

  Date& operator ++ ()  // prefix
    {
    ...
    }

  Date  operator ++ ( int )  // postfix
    {
    ...
    }

  private:
    bool is_leap;
  };

Hope this helps.
Jun 2, 2011 at 12:11am
With what should I fill this one:
1
2
3
4
Date& operator ++ ()  // prefix
    {
    ...
    }

?
And why should I use bool is_leap;?
Jun 2, 2011 at 12:36am
Ok, I came to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
class Date
{
    public:
   
void operator++() { }

        
private:
   int day;
  int month;
  int year;
};

Date Date::operator++()    
{
  int i+=1;           
  return *this;       
}

int main;
int d;
cout <<"Enter a day"<<"\n";
cin>>d;
d++;
system ("pause");
return 0;
}

Its not compiling, but do you mean it should look like it?
Jun 2, 2011 at 12:47am
int i+=1;
What did you hope to accomplish with that line?

And in line 7 you need a declaration, not a definition with an empty body.
Jun 2, 2011 at 12:49am
So with what should I replace int i +=1?
Jun 2, 2011 at 12:54am
So with what should I replace int i +=1?


You do whatever you would want the ++ operator to do.

So what do you want to have happen when you put ++mydate;?

Answer that question, then write the code to perform that action in your ++ operator.
Jun 2, 2011 at 12:56am
I want to increment the day +1 day.So if I have 01.02 it should become 02.02.Thats why I tried with int i +=1, but obviously I am not right.
Jun 2, 2011 at 1:05am
Am I close to the truth?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
class Date
{
    public:
   
void operator++();

    private:
   int day;
  int month;
  int year;
};

void Date& Date::operator++() 
{
   Date i =i+1;           
  return *this;       
}

int main(){
int d;
cout <<"Enter a day"<<"\n";
cin>>d;
d++;
system ("pause");
return 0;
}


Error line 15- expected initializer before '&' token
Last edited on Jun 2, 2011 at 1:06am
Jun 2, 2011 at 1:07am
You're almost right. int i += 1 creates a NEW integer called i, then increments it's value. You should have day += 1 ('day' being the member variable) so that the Date's day will be incremented.
Also as Athar said on line 7 seven of your above code you have an empty definition for the operator++, then you've REdefined it on line 16. You should remove the { } at the end of line 7 and place a semicolon.
Jun 2, 2011 at 1:08am
I want to increment the day +1 day.


If you want to increment 'day', why are you creating a separate variable named 'i'?

Why not just increment 'day'?

Error line 15- expected initializer before '&' token


Pick a return type and stick with it. Either have the operator return a void, or have it return a Date&. You can't have it return both.

In your class (line 7) you have it returning void. So change line 15 and get rid of that 'Date&' and just have it return a void as well.
Jun 2, 2011 at 1:09am
Ok I did that, but when you say "You should have day += 1 ('day' being the member variable) so that the Date's day will be incremented. " do you mean what I worte above - Date i =i+1; ?
And what about the error that it gives me?
Jun 2, 2011 at 1:14am
(should have efreshed befor posting, mybad)

now on line 7 you have void operator++();, this is saying you'll have a function (operator++) that accepts no arguments and returns nothing. Then line 15 void Date& Date::operator++() is saying it's a function that takes no arguments and returns a nothing reference to a date... That doesn't make sence.

Line 7 should be
Date& operator++();
So it declares a funtion to take no arguments and returns a reference to a Date

Line 15 should be
Date& Date::operator++()
so you can define that function.
Jun 2, 2011 at 1:17am
(and again)
Date i =i+1;
creates a new date called i and increments it by 1.
This is not what you want to do, you want to simply increment the current dates day by 1.
so it should be day += 1
Jun 2, 2011 at 1:20am
removed message.Everything works
Last edited on Jun 2, 2011 at 1:29am
Jun 2, 2011 at 1:25am
Thank you gyus a lot.You saved me several hours of reading, writing and making mistakes.Again thank you a lot.Now I will continue writing my code.
Good night :)
Oh yeah, one fast question.For postfix I should just put "int" between the parenthesis, to show the compiler its a postfix, right?
Last edited on Jun 2, 2011 at 1:31am
Pages: 12