Defining and Using const Member Functions - C++ Class

C++ examples for Class:Member Function

Description

Defining and Using const Member Functions

Demo Code

                                                                                                                                                  
#include <iomanip>
#include <iostream>
#include <stdexcept>
                                                                                                                                                     
class Time {/* w w w  .j  a  v a2  s  . com*/
public:
  Time(int = 0, int = 0, int = 0);
                                                                                                                                                     
                                                                                                                                                     
  // SETTERS
  void setTime(int, int, int);
  void setHour(int);
  void setMinute(int);
  void setSecond(int);
                                                                                                                                                     
  // GETTERS
  int getHour() const;
  int getMinute() const;
  int getSecond() const;
                                                                                                                                                     
  // print functions
  void printUniversal() const;
  void printStandard() const;
                                                                                                                                                     
private:
  int hour;
  int minute;
  int second;
};
Time::Time(int hour, int minute, int second) { setTime(hour, minute, second); }
// SETTERS
void Time::setTime(int hour, int minute, int second) {
  setHour(hour);
  setMinute(minute);
  setSecond(second);
}
void Time::setHour(int h) {
  if (h >= 0 && h < 24) {
    hour = h;
  }
  else {
    throw std::invalid_argument("hour must be 0-23");
  }
}
void Time::setMinute(int m) {
  if (m >= 0 && m < 60) {
    minute = m;
  }
  else {
    throw std::invalid_argument("minute must be 0-59");
  }
}
void Time::setSecond(int s) {
  if (s >= 0 && s < 60) {
    second = s;
  }
  else {
    throw std::invalid_argument("second must be 0-59");
  }
}
// GETTERS
int Time::getHour() const { return hour; }
int Time::getMinute() const { return minute; }
int Time::getSecond() const { return second; }
// print time in universal time format HH:HH:SS
void Time::printUniversal() const {
  std::cout << std::setfill('0') << std::setw(2) << hour << ":"
    << std::setw(2) << minute << ":" << std::setw(2) << second;
}
// print time in standard time HH:MM:SS AM/PM
void Time::printStandard() const {
  std::cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":"
    << std::setfill('0') << std::setw(2) << minute << ":"
    << std::setw(2) << second << (hour < 12 ? " AM" : " PM");
}
                                                                                                                                                     
int main(int argc, const char *argv[]) {
  Time wakeUp(6, 45, 0);      // non-constant object
   const Time noon(12, 0, 0);  // constant object
                                                                                                                                                     
                // OBJECT       // MEMBER FUNCTION
  wakeUp.setHour(18);  // non-const    non-const
                                                                                                                                                     
  //noon.setHour(12);  // const        non-const
                                                                                                                                                     
  wakeUp.getHour();  // non-const    const
                                                                                                                                                     
  noon.getMinute();       // const        const
  noon.printUniversal();  // const        const
                                                                                                                                                     
  noon.printStandard();  // const        non-const
  return 0;
}

Result


Related Tutorials