My assignment asks me to create my own class, the class must be immutable. So I wrote up everything how I thought it should work, however, I've run into some issues. In my implementation file under "int Time::getTotalSeconds() const" the "totalSeconds" says "Error: expression must be a modifiable value". This happens several other places, basically anytime I use one of my variables from the class.
I tried removing all my uses of const, which let me run the program, however this would ruin the whole immutable thing.
What steps to do need to take to maintain immutability and still accomplish my goals?
//***************************************
// SPECIFICATION FILE (Time.h)
//This file gives the specificaiton of a
//Time abstract data type.
//***************************************
#include "RelationType.h"
class Time
{
public:
//Constructors
Time();
//Post: minutes and seconds have been set to 0
Time (int inputMinutes, int inputSeconds);
//Post: Time is set accorind to incoming parameters
int getMinutes() const;
//Returns minutes
int getSeconds() const;
//Returns seconds
int getTotalSeconds() const;
//Returns time converted to seconds
void AddTime(Time otherTime) const;
//Adds two times together
void SubtractTime(Time otherTime) const;
//Subtracts two times
RelationType ComparedTo(Time otherTime);
//Compares one time to another time
private:
int minutes;
int seconds;
int totalSeconds;
int subtractSeconds;
};
//*****************************************************************************************
//This program creats two time functions and tests them with the get and compare functions.
//*****************************************************************************************
#include <iostream>
#include "Time.h" //For Time class
usingnamespace std;
int main ()
{
Time time1(4, 47);
Time time2;
int result;
cout << "time1: " << time1.getMinutes() << ':'
<< time1.getSeconds() << endl;
cout << "time2: " << time2.getMinutes() << ':'
<< time2.getSeconds() << endl;
switch (time1.ComparedTo(time2))
{
case BEFORE: cout << "First time comes before second time."
<< endl;
case SAME: cout << "Times are both the same."
<< endl;
case AFTER: cout << "First time comes after second time."
<< endl;
break;
}
time1.AddTime(time2);
time1.SubtractTime(time2);
system("pause");
return 0;
}
The whole point of declaring member functions as const is so you aren't able to modify any of the member variables in the class which you are trying to do in places like this.
In this specific case I would just make "totalSeconds" a local variable with a different name and return that as there is no reason to access the member variable there.
As for functions like "AddTime" you will have a hard time trying to keep that a const function and keeping the entire class immutable as you really need to access a member variable there. Do you have to use this example for a class? There are probably better examples you could use.
The program runs now, but I don't have the knowledge or experience to see if this is really doing what I'm wanting. Is this going to work or am I missing something?