I have to implement a class time that contains a header file, a file where the function occurs and file where it is tested. here is what I have done
this is m time.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
using std::cout;
using std:: endl;
#include<iomanip>
class Time {
public:
Time (); //constructor time
Time( int, int, int);
void setTime (int, int, int);//hour,minute and second
void printTime (); //time is on screen
private:
int hour; //hour
int minute; //minutes
int second; //seconds
};
#include <iostream>
#include "time.h"
Time::Time()
{
hour =0;//variable hour tintialized
minute=0;//variable minute intialized
second=0;//variable second initialized
}
void Time::setTime (int h,int m, int s)
{
hour= (h>=0 && h<24) ?h:0 ;//seting valid values hor h
minute= (m>=0 && m<60) ? m:0;//seting valid values for m
second= (s>=0 && s<60) ? s:0;//seting valid values for s
}
void Time::printTime()//printing time
{ //function printime is initialized
std::cout<<hour<<":"<<minute<<":"<<second;
}
and this is my testime.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
#include"time.h"
#include"time.cpp"
using std::cout;
using std::endl;
int main ()
{
Time t;
cout <<"the initial time is";
t.printTime();
t.setTime (8, 54, 35);//seting time
cout<<"the new time is";
t.printTime();
return 0;
}[code]
but it is not running and I don't what should I do to make it run.I am using visual studio 2012.
First, don't use using declarations within header files, as the end user may not want to have those constants impinged upon their namespace. Instead, always fully qualify the statements within a header file. Also, in your time.h header file, the include directives are not required at all, so they should probably be removed.
Apart from that, as @eyenrique suggested, remove the #include "time.cpp" statement from the testtime.cpp file, as that can cause linker errors. Also, when the program comes, does it just flash the window up on the screen and close it again quickly? If so, either run it from the command line, set up your IDE on how to pause the program at the end of the program (I don't know how to do this is VS2012), or add std::cin.sync(); std::cin.get() to the end of your code, to pause the console until a linefeed is received.