Aug 15, 2011 at 4:56pm Aug 15, 2011 at 4:56pm UTC
here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
using namespace std;
class Ctime{
int h,m,s;
public :
Ctime();
void settime(int hour,int min,int sec);
void military();
};
Ctime::Ctime()
{
h=m=s=0;
}
void Ctime::settime(int hour,int min,int sec)
{
h=hour>0&&hour<24 ? hour: 0;
m=min>0&&min<60 ? min : 0;
s= sec>0 && sec<60 ? sec : 0;
}
void Ctime::military()
{
cout<<( h<10 ? "0" : "" )<<h<<":" ;
cout<<( m<10 ? "0" : "" )<<m<<":" ;
cout<<(s<10 ? "0" : "" )<<s;
cout<<endl;
}
int main()
{
Ctime x;
x.settime(20,12,3);
x.military();
cout<<endl<<endl<<endl;
system("PAUSE" );
return 0;
}
1 error: In function settime(int h,int m,int s) your had a conflict between class variables and local variables.
2 error: You were confusing the compiler using the word time to name the class. I guess time is a function in one of the std libraries
Last edited on Aug 15, 2011 at 4:57pm Aug 15, 2011 at 4:57pm UTC
Aug 15, 2011 at 5:06pm Aug 15, 2011 at 5:06pm UTC
saman1989..
I made a few small changes to your program, and now it writes the military time on screen. You can see for yourself the changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// Digital Clock.cpp : main project file.
#include "stdafx.h"
#include <iostream>
using namespace std;
class time
{
int h,m,s;
public :
time();
void settime(int ,int ,int );
void military();
};
time::time()
{
h=m=s=0;
}
void time::settime(int hour,int minute,int second)
{
h = hour > 0 && hour < 24 ? hour: 0;
m = minute >0 && minute <60 ? minute : 0;
s = second > 0 && second <60 ? second : 0;
}
void time::military(){
cout << ( h<10 ? "0" : "" ) << h << ":" ;
cout << ( m<10 ? "0" : "" ) << m << ":" ;
cout << (s<10 ? "0" : "" ) << s;
cout<<endl;
}
int main()
{
time x;
x.settime(20,12,3);
x.military();
cout<<endl<<endl<<endl;
return 0;
}
EDIT :: Sorry. When I posted this, gaorozcoo' reply was not showing on the page. I guess I should first refresh the pages before posting.
Last edited on Aug 15, 2011 at 5:40pm Aug 15, 2011 at 5:40pm UTC
Aug 16, 2011 at 6:25am Aug 16, 2011 at 6:25am UTC
Quick question, are variables in a class when not declared private, public, or protected, are they private?
Aug 16, 2011 at 7:28am Aug 16, 2011 at 7:28am UTC
Yes, classes are defaultly private.