the thing is that these variables are declared in a .h file, and i have given you the .cpp of the .h file . So i cant pass the char as a parameter.
.h file
Code:
//File: Microwave.h
/* The purpose of the Microwave class is to abstract a simple Microwave. It provides
functions to start the microwave, stop it and check the status of operations. It
takes care of making sure that the microwave is not started when it still running. */
#include<time.h>
class Microwave
{
public:
Microwave();
~Microwave();
bool start(double);
//Purpose:To start the microwave.
//Requirement: In order for the microwave to start, it shouldnt be running already.
//Promise: To start the microwave for the number of seconds requested by the user.
//Exception: returns a true if all went well and microwave was started, else
// returns a false if microwave is still running.
void stop(void);
//Purpose: To stop the microwave
//Requirement:-
//Promise: Will stop the microwave.
//Exception:-
const char* status(void);
//Purpose:Returns the current status of the microwave.
//Requirement:-
//Promise:- Returns "Microwave not in use" if microwave is not running.
// Returns "Microwave in use. "Number of seconds" left where
// Number of seconds is time left before microwave stops if
// the microwave is still running.
private:
double seconds;//keeps the number of seconds inputted by user.
time_t starts;//captures time when microwave was started.
time_t end;//captures time when status is called.
char *temp;
double difference;//calculates the difference between starts and end in seconds
//and is used to determine status.
};
.cpp file
Code:
//File: Microwave.cpp
#include "Microwave.h"
#include<time.h>
#include<stdio.h>
#include<iostream>
using namespace std;
Microwave::Microwave()
{
seconds=0;
}
bool Microwave::start(double set)
{
//if seconds is not 0, that means it is running
//and thus false is returned
if(seconds!=0)return false;
//else all things run and the microwave is started
else
{
seconds=set;
//time start is captured
time(&starts);
return true;
}
}
void Microwave::stop(void)
{
//all is set to 0, indicating that microwave is off
seconds=0;
difference=0;
}
const char* Microwave::status(void)
{
temp=new char[80];
time(&end);//captures time again
//calculates the difference in seconds,
difference=difftime(end,starts);
//if difference is less than seconds its running
if(difference<seconds)
{
//a static array to store the message returned
sprintf(temp,"Microwave in use. %.0f seconds left",seconds-difference);
return temp;
}
//if difference is greater than the number of seconds, its off
//if seconds is 0 then its off
if(difference>seconds || seconds==0)
{
seconds=0;
sprintf(temp,"Microwave not in use");
return temp;
}
//a default return incase none of the conditions are met
return "";
}
Microwave::~Microwave()
{
cout<<"called";
delete[] temp;
}
the main file
Code:
// The output generated by this program should be:
/*
Started Microwave, response is success
Microwave in use. 90 seconds left
Microwave not in use
Started Microwave, response is success
Microwave in use. 20 seconds left
Microwave not in use
Started Microwave, response is success
Microwave in use. 40 seconds left
Started Microwave, response is failure
*/
#include <iostream>
#include <windows.h>
#include "Microwave.h"
using namespace std;
void printStatus(Microwave& m)
{
cout << m.status() << endl;
}
inline const char* printBool(bool result)
{
return result ? "success" : "failure";
}
void main()
{
Microwave aMicrowave;
bool result;
result = aMicrowave.start(90);
cout << "Started Microwave, response is " << printBool(result) << endl;
printStatus(aMicrowave);
Sleep(10000);
aMicrowave.stop();
printStatus(aMicrowave);
result = aMicrowave.start(30);
cout << "Started Microwave, response is " << printBool(result) << endl;
Sleep(10000);
printStatus(aMicrowave);
Sleep(21000);
printStatus(aMicrowave);
result = aMicrowave.start(40);
cout << "Started Microwave, response is " << printBool(result) << endl;
printStatus(aMicrowave);
Sleep(10000);
result = aMicrowave.start(10);
cout << "Started Microwave, response is " << printBool(result) << endl;
}