Constructor and Objects Explanation!

I have the following code, and I do not understand how we can give an object a parameter of type int? Also can some one please explain to me what the constructor does for the following code, and why it has arguments?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

class Time{
public:
	Time(int=0,int=0,int=0);
	void setTime(int,int,int);
	void printMilitary();
	void printStandard();
private:
	int hour;
	int minute;
	int second;
};

Time::Time(int h, int m,int s){
	hour = (h>=0 && h<24)?h:0;
	minute = (m>=0 && m<60 )? m:0;
	second = (s>=0 && s<60 ) ?s:0;

}

void Time::printMilitary(){

	cout<< (hour<10 ? "0":"")<<hour<<":"<<(minute <10 ? "0":"")<<minute;
}
void Time::printStandard(){

	cout<<((hour==0 ||hour ==12) ? 12:hour%12 )<<":"<<(minute <10 ? "0" : "")<<minute<<":"<<(second <10?"0":"")<<second<<(hour<12? "AM":"PM");
}

int main(){

	Time t,t1(15),t2(16,20),t3(17,25,30),t4(50,70,90);

	t.printMilitary();
		t.printStandard();

		cout<<endl;

		t1.printMilitary();
		cout<<endl;
		t1.printStandard();

		cout<<endl;

		t2.printMilitary();
		cout<<endl;
		t2.printStandard();

		cout<<endl;

		t3.printMilitary();
		cout<<endl;
		t3.printStandard();

		cout<<endl;

		t4.printMilitary();
		cout<<endl;
		t4.printStandard();

		cout<<endl;

		return 0;
}
sakonpure6 wrote:
I do not understand how we can give an object a parameter of type int?
Could you specify which line you are referring to?
sakonpure6 wrote:
Also can some one please explain to me what the constructor does for the following code, and why it has arguments?
What do you think it does? Your answer affects mine.
@ L B, thank you for the reply.

For Question #1, at line 34.

For Question #2, I know that a constructor initializes data members and is the first function to operate on an object , however I do not understand the purpose of line 6 , and does that relate to Question #1, seeing how an object can have 3 arguments?

I think I get it, the object passes 3 or less variables to the constructor where it validates the values passed (Checks if statement then initializes the private variables accordingly) , and the parameter values for the constructor : h , m and s are all equal to 0 thanks to line 6? Is this correct?
sakonpure6 wrote:
For Question #1, at line 34.
Those integer arguments are being passed to the constructor for the class, initializing the objects appropriately.
sakonpure6 wrote:
For Question #2, I know that a constructor initializes data members and is the first function to operate on an object , however I do not understand the purpose of line 6 , and does that relate to Question #1, seeing how an object can have 3 arguments?
Line 6 declares that there is a constructor which takes three arguments, but it also gives default values for all three parameters, so this constructor also counts as the default constructor since it may be invoked with no explicit arguments. The compiler just passes in the default arguments for you.
sakonpure6 wrote:
I think I get it, the object passes 3 or less variables to the constructor where it validates the values passed (Checks if statement then initializes the private variables accordingly) , and the parameter values for the constructor : h , m and s are all equal to 0 thanks to line 6? Is this correct?
The = 0 on line 6 are just the default values, and if you pass a value for that parameter the = 0 is ignored.
Thank you very much!
Topic archived. No new replies allowed.