3 Replies - 1616 Views - Last Post: 30 January 2011 - 04:58 PM Rate Topic: -----

#1 llundberg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 11

nonmember functions

Posted 30 January 2011 - 12:16 PM

The increment and relational operators in the class clockType are overloaded as member functions. Rewrite the definitions of the class clockType so that the operators are overloaded as nonmember functions, also overload the post-increment operator for the class clockType as a nonmember. Here is what I have so far, can you tell me if i am on the write track? Thanks

//Header file newClockTypeOprOvrNonmemberFunc.h
   
#ifndef H_newClockTypeOprOvrNonmemberFunc
#define H_newClockTypeOprOvrNonmemberFunc

#include <iostream>
#include <ctime>
#include "newClock.h"

using namespace std;

class clockType
{
    friend ostream& operator<<(ostream&, const clockType&);
    friend istream& operator>>(istream&, clockType&);

public:
   void setTime(int hours, int minutes, int seconds);
      //Function to set the member variables hr, min, and sec.  
      //Postcondition: hr = hours; min = minutes; sec = seconds

   void getTime(int& hours, int& minutes, int& seconds) const;
      //Function to return the time.
      //Postcondition: hours = hr; minutes = min; seconds = sec

   friend clockType operator++(clockType&);    
      //Overload the pre-increment operator.
      //Postcondition: The time is incremented by one second.
      
   friend clockType operator++(clockType&, int);
      //Overload the post-increment operator.
      //Postcondition: The time is incremented by one second.

   friend bool operator==(const clockType& otherClock) const; 
      //Overload the equality operator.
      //Postcondition: Returns true if the time of this clock 
      //               is equal to the time of otherClock,
      //               otherwise it returns false.

   friend bool operator!=(const clockType& otherClock) const; 
      //Overload the not equal operator.
      //Postcondition: Returns true if the time of this clock 
      //               is not equal to the time of otherClock,
      //               otherwise it returns false.

   friend bool operator<=(const clockType& otherClock) const;
      //Overload the less than or equal to operator.
      //Postcondition: Returns true if the time of this clock
      //               is less than or equal to the time of 
      //               otherClock, otherwise it returns false.

    friend bool operator<(const clockType& otherClock) const;
      //Overload the less than operator.
      //Postcondition: Returns true if the time of this clock
      //               is less than the time of otherClock,
      //               otherwise it returns false.

    friend bool operator>=(const clockType& otherClock) const;
      //Overload the greater than or equal to operator.
      //Postcondition: Returns true if the time of this clock
      //               is greater than or equal to the time of 
      //               otherClock, otherwise it returns false.

    friend bool operator>(const clockType& otherClock) const;
      //Overload the greater than operator.
      //Postcondition: Returns true if the time of this clock
      //               is greater than the time of otherClock,
      //               otherwise it returns false.

    clockType(int hours = 0, int minutes = 0, int seconds = 0);  
      //Constructor to initialize the object with the values 
      //specified by the user. If no values are specified,
      //the default values are assumed.
      //Postcondition: hr = hours; min = minutes; 
      //               sec = seconds;


private: 
    int hr;  //variable to store the hours
    int min; //variable to store the minutes
    int sec; //variable to store the seconds
};

#endif



//Implementation file newClockTypeOprOvrNonmemberFunc.cpp

#include <iostream>
#include <ctime>
#include "newClock.h"
#include "newClockTypeOprOvrNonmemberFunc.h"
  
using namespace std;

    //Overload the pre-increment operator.
clockType clockType::operator++()
{
    sec++;                  
    
    if (sec > 59)          
    {
        sec = 0;            
        min++;              

        if (min > 59)       
        {
            min = 0;        
            hr++;           

            if (hr > 23)    
                hr = 0;     
        }
    }
   
    return *this;          
}

//Overload the post-increment operator.
clockType clockType::operator++(int u)
{

  ++sec;                  
    
    if (sec > 59)          
    {
        sec = 0;            
        ++min;              

        if (min > 59)       
        {
            min = 0;        
            ++hr;           

            if (hr > 23)    
                hr = 0;     
        }
    }
   
    return *this;          
}

    //Overload the equality operator.
bool clockType::operator==(const clockType& otherClock) const
{
    return (hr == otherClock.hr && min == otherClock.min
            && sec == otherClock.sec);
}

    //Overload the not equal operator.
bool clockType::operator!=(const clockType& otherClock) const
{
    return (hr != otherClock.hr || min != otherClock.min
            || sec != otherClock.sec);
}

    //Overload the less than or equal to operator.
bool clockType::operator<=(const clockType& otherClock) const
{
    return ((hr < otherClock.hr) ||
            (hr == otherClock.hr && min < otherClock.min) ||
            (hr == otherClock.hr && min == otherClock.min &&
             sec <= otherClock.sec));
}

    //Overload the less than operator.
bool clockType::operator<(const clockType& otherClock) const
{
    return ((hr < otherClock.hr) ||
            (hr == otherClock.hr && min < otherClock.min) ||
            (hr == otherClock.hr && min == otherClock.min &&
             sec < otherClock.sec));
}

    //Overload the greater than or equal to operator.
bool clockType::operator>=(const clockType& otherClock) const
{
    return ((hr > otherClock.hr) ||
            (hr == otherClock.hr && min > otherClock.min) ||
            (hr == otherClock.hr && min == otherClock.min &&
             sec >= otherClock.sec));
}

    //Overload the greater than operator.
bool clockType::operator>(const clockType& otherClock) const
{
    return ((hr > otherClock.hr) ||
            (hr == otherClock.hr && min > otherClock.min) ||
            (hr == otherClock.hr && min == otherClock.min &&
             sec > otherClock.sec));
}

void clockType::setTime(int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24)
        hr = hours;
    else 
        hr = 0;

    if (0 <= minutes && minutes < 60)
        min = minutes;
    else 
        min = 0;

    if (0 <= seconds && seconds < 60)
        sec = seconds;
    else 
    sec = 0;
}

void clockType::getTime(int& hours, int& minutes, 
                        int& seconds) const
{
    hours = hr;
    minutes = min;
    seconds = sec;
}

    //Constructor
clockType::clockType(int hours, int minutes, int seconds)
{
    setTime(hours, minutes, seconds);
}

   //Overload the stream insertion operator.
ostream& operator<<(ostream& osObject, const clockType& timeOut)
{
    if (timeOut.hr < 10)
        osObject << '0';
    osObject << timeOut.hr << ':';
	
    if (timeOut.min < 10)
        osObject << '0';
    osObject << timeOut.min << ':';
	
    if (timeOut.sec < 10)
        osObject << '0';
    osObject << timeOut.sec;

    return osObject;  //return the ostream object
}

     //overload the stream extraction operator
istream& operator>> (istream& is, clockType& timeIn)
{
    char ch;

    is >> timeIn.hr;                         

    if (timeIn.hr < 0 || timeIn.hr >= 24)     
        timeIn.hr = 0;
	
    is.get(ch);           

    is >> timeIn.min;                         

    if (timeIn.min < 0 || timeIn.min >= 60)   
        timeIn.min = 0;

    is.get(ch);          

    is >> timeIn.sec;                        

    if (timeIn.sec < 0 || timeIn.sec >= 60)   
        timeIn.sec = 0;

    return is;                                
}



when i compile I get errors with my pre and post increments saying 35 F:\Programming II class Winter Qtr\unit 4\newClockTypeOprOvrNonmemberFuncImp.cpp prototype for `clockType clockType::operator++(int)' does not match any in class `clockType'

13 F:\Programming II class Winter Qtr\unit 4\newClockTypeOprOvrNonmemberFunc.h redefinition of `class clockType'

11 F:\Programming II class Winter Qtr\unit 4\newClock.h previous definition of `class clockType'

6 F:\Programming II class Winter Qtr\unit 4\newClockTypeOprOvrNonmemberFuncImp.cpp In file included from newClockTypeOprOvrNonmemberFuncImp.cpp

is someone able to get me in the right direction? I am not sure what I am doing wrong, I am just learning this stuff so I am very new to this. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: nonmember functions

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: nonmember functions

Posted 30 January 2011 - 03:39 PM

You declare the functions as "friend" functions and then implement them as member functions.

clockType clockType::operator++() -- should not be a member function (no member function is defined) -- so this would be:

clockType operator++(clockType& obj)

etc.

you will need to make the correction for each operator that you made a friend function
Was This Post Helpful? 0
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: nonmember functions

Posted 30 January 2011 - 04:26 PM

Also all Friend functions should be declared within the class at the top before any member function prototypes are declared. Also NickDMax is 100% correct that they are non members subsequently they will not be found as member functions of your clockType class.
Was This Post Helpful? 0
  • +
  • -

#4 llundberg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 11

Re: nonmember functions

Posted 30 January 2011 - 04:58 PM

thanks I will make those changes, thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1