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:
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
};
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;
}
#include <iostream>
usingnamespace std;
class Date
{
public:
voidoperator++() { }
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;
}
#include <iostream>
usingnamespace std;
class Date
{
public:
voidoperator++();
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;
}
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.
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?
now on line 7 you have voidoperator++();, 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.
(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
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?