A little problem

This program is designed to show the time,with the user inputs hours,min,sec.
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
#include<iostream>
using namespace std;

class Time{
public:
void set(int,int,int);
void copy(Time source) {*this=source;}
void show() {cout<<hour<<":"<<min<<":"<<sec;}
private:
int hour,min,sec;
};

void Time::set(int hour,int min,int sec)
{
this->hour=hour;
this->min=min;
this->sec=sec;
}

int main()
{
Time t1, t2;
t1.set(12,15,30);
t2.copy(t1);
t1.show();
cout<<endl;
system("pause");
} 


I want to ask why when I change the name "Time" into "time"(t no longer the capital letter), why there is an error?
Have you changed all the places where you say "Time" to "time"?
That's a fantastic question. I don't know!

One thing you can do, though, is declare t1 and t2 along with the class.

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
#include<iostream>

class time
{
public:
	void set(int,int,int);
	void copy(time source) {*this=source;}
	void show() {std::cout<<hour<<":"<<min<<":"<<sec;}
private:
	int hour,min,sec;
}t1, t2;

void time::set(int hour,int min,int sec)
{
	this->hour=hour;
	this->min=min;
	this->sec=sec;
}

int main()
{
	t1.set(12,15,30);
	t2.copy(t1);
	t1.show();
	std::cout<<std::endl;
	std::cin.get();
	return 0;
}
This code works fine.
Note that I took out using namespace std; to make sure that std::time wasn't in use... but I guess it's not. You can add that back in, just be sure to take std:: off of the couts and endl.
I also changed your system("pause"); to a cin.get() for reasons of personal preference.

I get the feeling I'm missing something really obvious, but right now I'm fairly stumped :)
That's a fantastic question. I don't know!

I just love that - It made me LOL.

As an engineer, I normally bluff when I'm unsure - but the the next time
a customer asks me what's wrong - I'll look them straight in the eye and
say that !!! and see what they say :-)
@Timaster:
I get the feeling I'm missing something really obvious

I think u missed answering the question.

@kg123: Beside what firedraco said, if u were asking in general in C/C++ there is difference between upper and lower letters
int a=10,A=1;
cout<<a<<" "<<A;
@Timaster: I strongly discourage moving the variables to be declared globally. That's not an acceptable practice when developing professional applications and violates one of the 1 main concepts of OOD.

It's also difficult to determine when the constructor is called, and when the destructor is going to be called so having dynamic memory and references to other objects would be difficult.

The OP's original concept of having them declared locally within the main function is the correct for.

And as for the namespace question. I would use
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using std::cout;
using std::endl;

int main() {

 cout << "something" << endl;
 return 0;

}


As this doesn't require the inclusion of the entire namespace, and you don't have to type the extra std:: everywhere, thus having cleaner code.
@guestgulkan
I was taken aback by the question, because I was sure it was a problem with capitalization, but then I tried running the code, and it didn't work! What else could I say? :)

@Smok006
I made it clear I don't know the answer, or I would have answered :P
It seemed like kg123 is already aware that changing cases is important, so I didn't bother mentioning that.

@Zaita
Yeah, you're right. I was mainly pointing out that there was a way to make it errorless using the word "time" instead of "Time", but that's certainly not a good way to leave the class.
And good call on the namespaces :)
Last edited on
Topic archived. No new replies allowed.