Type Conversion and Advice

Hello all. I am trying to practice C++, wrote a simple class, but encountered many problems with type conversions. Fortunately, I have solved these problems by adding many variable casts. However, I am wondering whether there exist a more elegant way of writing all this without encountering conversion problems.

Therefore, I will greatly appreciate (generally any) advice from a more experienced programmers, which can ease my life in the following. In other words, how would you write the following piece of code:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

const double mnum = 60.0, hnum = 3600.0, Dnum = 24.0 * 3600.0, 
Mnum = 30.0 * 24.0 * 3600.0, Ynum = 360.0 * 24.0 * 3600.0;
class DateTime {

public:
	//Variables
	int s, m, h, D, M, Y;
	double sign;

	//Constructors and destructors
	DateTime(int nh=0, int nm = 0, int ns = 0, int nD = 0, int nM = 0, int nY = 0) 
        { sign = 1.0;  h = nh; m = nm; s = ns; D = nD; M = nM; Y = nY; }
	~DateTime() {}

	//Functions
	void set_date(int nD=0, int nM=0, int nY=0) { D = nD; M = nM; Y = nY; }
	void set_time(int nh=0, int nm=0, int ns=0) { s = ns; m = nm; h = nh; }
	void print_all() const;

	//Conversions
	double seconds() const; void iseconds(double);
	double minutes() const { return (this->seconds()) / mnum; };
	double hours() const { return (this->seconds()) / hnum; };
	double days() const { return (this->seconds()) / Dnum; };
	double months() const { return (this->seconds()) / Mnum; };
	double years() const { return (this->seconds()) / Ynum; };

	//Operations
	DateTime operator+ (const DateTime& dt);
	DateTime operator- (const DateTime& dt);

};
void DateTime::iseconds(double ovs) {
	if (ovs < 0) { sign = -1.0; ovs = ovs*sign; }
	double nY, nM, nD, nh, nm, ns;
	nY = floor(ovs / Ynum); ovs = ovs - nY*Ynum;
	nM = floor(ovs / Mnum); ovs = ovs - nM*Mnum;
	nD = floor(ovs / Dnum); ovs = ovs - nD*Dnum;
	nh = floor(ovs / hnum); ovs = ovs - nh*hnum;
	nm = floor(ovs / mnum); ovs = ovs - nm*mnum;
	Y = (int)nY; M = (int)nM + 1; D = (int)nD + 1; h = (int)nh; m = (int)nm; s = (int)ovs;
}
double DateTime::seconds() const {
	return (double) (Y*(int)Ynum+(M-1)*(int)Mnum+(D-1)*(int)Dnum+h*(int)hnum+m*(int)mnum+s);
}
DateTime DateTime::operator+ (const DateTime& dt) {
	DateTime ndt;
	double s1 = (this->sign)*(this->seconds()), s2 = (dt.sign)*(dt.seconds());
	ndt.iseconds(s1 + s2);
	return ndt;
}
DateTime DateTime::operator- (const DateTime& dt) {
	DateTime ndt;
	double s1 = (this->sign)*(this->seconds()), s2 = (dt.sign)*(dt.seconds());
	ndt.iseconds(s1 - s2);
	return ndt;
}
void DateTime::print_all() const {
	string str0 = "", str1 = "", str2 = "", str3 = "";
	if (sign < 0) str0 = "- ";
	if (h < 10) str1 = "0";
	if (m < 10) str2 = "0";
	if (s < 10) str3 = "0";
	
	string month = "Jan";
	if (M == 2) month = "Feb";
	if (M == 3) month = "Mar";
	if (M == 4) month = "Apr";
	if (M == 5) month = "May";
	if (M == 6) month = "Jun";
	if (M == 7) month = "Jul";
	if (M == 8) month = "Aug";
	if (M == 9) month = "Sep";
	if (M == 10) month = "Oct";
	if (M == 11) month = "Nov";
	if (M == 12) month = "Dec";
	cout << str0 << str1 << h << ":" << str2 << m << ":" << str3 << s 
             << ", " << D << " " << month << " " << Y;
}
I feel something strange about the method inside DateTime
why would anyone need a float or double to determine days when everyone will know that double precision couldn't be trusted...
knowing it's been 3.4252359 years isn't really a useful information IMO.
Multiplying it with 360 might lead to the wrong date because of double precision limitation.

Anyway I didn't really year your code just skim through them. I don't what each variable are and doesn't really want to know. Perhaps just add more constant because the one your casting is constant and it should result a constant too.

A few note :
put your variable in private or protected not public
Your code is confusing to read.

Line 7-8: To the casual reader, the difference between mnum and Mnum is not immediately apparent. Use meaningful variable names. Mnum and mnum are not meaningful and easily confused. e.g.
1
2
const double min_per_hr = 60.0;
const double secs_per_mon = 30.0 * 24.0 * 3600.0;

I don't see any allowance in your code for months that have 31 days, February, or leap year.

Line 83: str1, str2, str3 are unnecessary. Use <iomanip>
http://www.cplusplus.com/reference/iomanip/
Last edited on
Thank you very much, rmxhaha and AbstractionAnon, for your advices! Will try to follow them while writing new code!
P.S. Yes, there are no allowance for 31 days months and for leap years, I just wrote the simplest class to practice:) But maybe will include these possibilities to practice more. Any advice what other tasks can I do for learning C++ and for gaining useful experience?
Topic archived. No new replies allowed.