Operator Overloading

operator++() and operator--()

Page 1 of 1

9 Replies - 929 Views - Last Post: 09 March 2010 - 07:14 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Operator Overloading

Posted 09 March 2010 - 02:47 PM

We have been designated to modify a class and add overloaded operators, the postifx and prefix of operator++() and operator--()...

Now in my book it says that you need to do something like:
void operator++(const className &add); 
void operator--(const className &minus); //the add and minus are my make ups, but not the &



Now I don't know how to implement these into usage... if I wanted to use them to increment a member by one and decrement a member by one... Could I just do...

className--
className++
++className
--className


??

Is This A Good Question/Topic? 0
  • +

Replies To: Operator Overloading

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Operator Overloading

Posted 09 March 2010 - 02:52 PM

Off the top of my head I would say that would only work for prefix. I think you need something special for postfix.

Yep, look at this:
http://www.parashift....html#faq-13.14

This post has been edited by eker676: 09 March 2010 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Operator Overloading

Posted 09 March 2010 - 03:01 PM

Yeah I was reading that site previously to me posting.

I just don't understand how to use them and using their example I did this:
dayOfYear::operator++() {
	dayOfYear Z
	Z.userDay++;
	return *this;
}

dayOfYear::operator++(int) 
{
	++(*this);
}



which doesn't work at all... Real sorry, I hear that operator overloading is supposed to be pretty easy, and im sure if it is explained well, it is, but it's just not making sense...
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Operator Overloading

Posted 09 March 2010 - 03:14 PM

You have to do this:
dayOfYear& dayOfYear::operator++() {
        this->userDay++; // increment the day then return
        return *this;
}

void dayOfYear::operator++(int) 
{
        ++(*this);
}


In your example you are making a useless copy and returning the same thing.

This post has been edited by eker676: 09 March 2010 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Operator Overloading

Posted 09 March 2010 - 03:23 PM

And if I wanted to print use them with-in my program I would:
...
dayOfYear D
...
++D;
printC(D);


?
Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Operator Overloading

Posted 09 March 2010 - 03:25 PM

You have to have the printC() function defined correctly so it takes the data from within the class and prints it. Therefore, printC() would have to be a friend of that class in order to access it's private/protected data.

You can overload the ostream operator as well and specialize how cout << acts on your class. Just trying to do this:
cout << myClass << endl;


without first overloading the ostream isn't going to work.

This post has been edited by eker676: 09 March 2010 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#7 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Operator Overloading

Posted 09 March 2010 - 03:30 PM

Yeah, IDK... Overloaded operators seem to be a lot more trouble than they are really worth... But I must learn them...

here are the errors I get with-in my program...

1>h:\assignment 5\assignment 5\11-3hmain.h(17) : error C2556: 'dayOfYear &dayOfYear::operator ++(void)' : overloaded function differs only by return type from 'dayOfYear dayOfYear::operator ++(void)'
1> h:\assignment 5\assignment 5\head.h(14) : see declaration of 'dayOfYear::operator ++'
1>h:\assignment 5\assignment 5\11-3hmain.h(17) : error C2040: 'dayOfYear::operator ++' : 'dayOfYear &(void)' differs in levels of indirection from 'dayOfYear (void)'
1>h:\assignment 5\assignment 5\11-3hmain.h(23) : error C2556: 'int dayOfYear::operator ++(int)' : overloaded function differs only by return type from 'dayOfYear dayOfYear::operator ++(int)'
1> h:\assignment 5\assignment 5\head.h(15) : see declaration of 'dayOfYear::operator ++'
1>h:\assignment 5\assignment 5\11-3hmain.h(23) : error C2371: 'dayOfYear::operator ++' : redefinition; different basic types
1> h:\assignment 5\assignment 5\head.h(15) : see declaration of 'dayOfYear::operator ++'
1>h:\assignment 5\assignment 5\11-3hmain.h(24) : error C2264: 'dayOfYear::operator ++' : error in function definition or declaration; function not called
1>h:\assignment 5\assignment 5\11-3hmain.h(24) : error C2088: '++' : illegal for class
1>h:\assignment 5\assignment 5\11-3hmain.h(74) : error C2264: 'dayOfYear::operator ++' : error in function definition or declaration; function not called
1>h:\assignment 5\assignment 5\11-3hmain.h(74) : error C2088: '++' : illegal for class


and her is my code
//11-3Hmain.h

#include <iostream>
#include <string>
#include "Head.h"
using namespace std;


dayOfYear::dayOfYear() : userDay(0) { }		//Constructor initializing userDay to 0, no other initializations.

void dayOfYear::setMonth(string month) { this -> month = month; }

string dayOfYear::getMonth() const { return this -> month; }

void dayOfYear::setDay(int userDay) { this -> userDay = userDay; }              // Setting user input to userDay 

int dayOfYear::getDay() const { return this -> userDay; }               // Return this->userDay to calling function 

dayOfYear& dayOfYear::operator++() {		//Prefix operator
	this -> userDay++;
	return *this;
}

void dayOfYear::operator++(int)			// Postfix operator
{
	++(*this);
}

string dayOfYear::months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 

int dayOfYear::days[12] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 


void dayOfYear::print() const  // Print out all relevant information
{		


	
	//dayInfo = (userDay - dayInfo) ;		// User defined day, - the preceding months high number gives day of corresponding month
	//cout << months[monthInfo] << " " << dayInfo << endl;	// Print it out =)
}


string getUserMonth() 
{
	string month;
	cout << "Enter a month: ";
	cin >> month;
	return month;
}


//Exactly as it sounds, get the user input of the day
int getUserDay() 
{
	int number;
	string month;
	
	while(true) {
		cout << "Please enter the day of the month:  ";
		cin >> number;
		cin.ignore();
		if(number > 0 && number < 366) {break; }
		cout << "You're input " << month << " or " << number << " was incorrect.\n Please try again: ";
	}
	return number;
}


// Pass the class dayOfYear as printC() argument so it can access the members
void printC(dayOfYear &D) 
{
	cout << endl;
	cout << D.getMonth() << " " << D.getDay() << " is equal to -> " ;	// Get the day the user defined in the beginning
	D.print();		// Print all the lovely information for the user =)
	cout << "\n\n\n\n";
}



//Head.h 

#ifndef HEAD_H
#define HEAD_H
#include <iostream>

class dayOfYear 
{
private:
		static std::string months[12];
		static int days[12];
		int userDay;	// private class member to hold user input
		std::string month;
public:
		dayOfYear();	//Constructor
		dayOfYear operator++();
		dayOfYear operator++(int);
		dayOfYear operator--();
		dayOfYear operator--(int);
		void setMonth(std::string);
		std::string getMonth() const;
		void setDay(int);	// Function to set day
		int getDay()const;	// Function to recieve day for later functions
		void print() const;	// Print() function for printing the month-day reps
};

#endif



//main.cpp

#include <string>
#include "Head.h"
#include "11-3HMain.h"
using namespace std;

int main() 
{
	dayOfYear D;		// D represents dayOfYear class
	
	D.setMonth(getUserMonth());
	D.setDay(getUserDay());	// Set the day by calling getUserDay, than transferring to setDay to do so.
	printC(D);	// printC is passing the class dayOfYear which is D as it's argument so it can access the elements in the class
        ++D;
        printC(D);
	return 0;
}



This post has been edited by IngeniousHax: 09 March 2010 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Operator Overloading

Posted 09 March 2010 - 03:39 PM

Okay, your function prototypes are incorrect:

head.h
//Head.h 

#ifndef HEAD_H
#define HEAD_H
#include <iostream>

class dayOfYear 
{
private:
                static std::string months[12];
                static int days[12];
                int userDay;    // private class member to hold user input
                std::string month;
public:
                dayOfYear();    //Constructor
                dayOfYear& operator++(); // <----here
                void operator++(int); // <---- here
                dayOfYear operator--(); // <--- these as well but you didnt code
                dayOfYear operator--(int);     // the implementation yet so i didn't change them
                void setMonth(std::string);
                std::string getMonth() const;
                void setDay(int);       // Function to set day
                int getDay()const;      // Function to recieve day for later functions
                void print() const;     // Print() function for printing the month-day reps
};

#endif

Was This Post Helpful? 0
  • +
  • -

#9 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Operator Overloading

Posted 09 March 2010 - 03:46 PM

Ok, so that got rid of a few errors, but it says
void dayOfYear& operator++(int);
...
void dayOfYear::operator++(int)			// Postfix operator
{
	++(*this);
}


differ by return type... I tried changing it to an int, but it still says the same thing..

Actual Errors
1>h:\assignment 5\assignment 5\head.h(15) : error C2380: type(s) preceding 'dayOfYear' (constructor with return type, or illegal redefinition of current class-name?)
1>h:\assignment 5\assignment 5\11-3hmain.h(23) : error C2556: 'void dayOfYear::operator ++(int)' : overloaded function differs only by return type from 'dayOfYear &dayOfYear::operator ++(int)'
1> h:\assignment 5\assignment 5\head.h(15) : see declaration of 'dayOfYear::operator ++'
1>h:\assignment 5\assignment 5\11-3hmain.h(23) : error C2040: 'dayOfYear::operator ++' : 'void (int)' differs in levels of indirection from 'dayOfYear &(int)'
Was This Post Helpful? 0
  • +
  • -

#10 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Operator Overloading

Posted 09 March 2010 - 07:14 PM

Did you look at the header I posted?

void dayOfYear& operator++(int);

Two return types. You want:
dayOfYear& operator++(int)

That is postfix.

The prefix increment is defined like this:
void operator++() // not int
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1