BankAccount - need help with constructor and bool

do i need 2 constructors?
and can a boolean function return both true and another variable?

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

using namespace std;

class BankAccount
{
public:
    BankAccount();
    void readData();
    friend ostream& operator <<(ostream & out, BankAccount  BA );
    double update();
    bool withdraw(double Balance);
    int getNumber();
private:
    string Name;
    int Number;
    double Balance;
    double interestRate;
};
BankAccount::BankAccount()
{
    
}


void BankAccount::readData()
{
    cin >> Name >> Number >> Balance >> interestRate;
}

ostream& operator <<(ostream & out, BankAccount  BA )
{
        out << "Customer name: " << BA.Name << endl;
        out << "Customer number: " << BA.Number << endl;
        out << "Balance: " << BA.Balance << endl;
        out << "Interest rate: " << BA.interestRate << endl;
        out << endl;
        return out;
}

double BankAccount::update()
{
    Balance=Balance*interestRate/100+Balance;

    return Balance;
}

bool BankAccount::withdraw()
{

}

int BankAccount::getNumber()
{
    return Number;
}

int main()
{
	BankAccount account;
	account.readData();
	cout << account;
	account.update();
	cout << account;

	if (account.withdraw(32.5)) {
		cout << "Amount withdrawn for account " << account.getNumber() << endl;
	}
	else {
		cout << "Unable to withdraw from account " << account.getNumber() << endl;
	}

    cout << endl << account;

    return 0;
}
can a boolean function return both true and another variable?

you cant return a separate variable in any function.
boolean function only returns true(value 1) or false(value 0)

do i need 2 constructors?

why would you need? i cannot answer this
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>

using namespace std;
class Time
{
    public:
        Time();
        Time(int s, int m, int h);

        friend istream & operator >>(istream & in, Time & t1 );
        friend ostream & operator <<(ostream & out, Time  t1 );
        friend Time operator + (Time t1, Time t2 );
        friend Time operator - (Time t1, Time t2 );
        friend bool operator<(const Time& t1, const Time& t2);
    private:
        int seconds, minutes, hours;
        void normalize();
};

Time::Time()
{
    hours = minutes = seconds = 0;
    normalize();

}

Time::Time(int s, int m, int h)
{
    this->hours = s;
    this->minutes = m;
    this->seconds = h;



    normalize();
}

istream & operator >> (istream & in, Time & t1 )
{
    in >> t1.hours >> t1.minutes >> t1.seconds;
    t1.normalize();
    return in;
}

ostream & operator <<(ostream & out, Time  t1 )
{
    if(t1.hours == 24){
        t1.hours = 0;
        }
    if (t1.hours < 10 && t1.minutes > 9 && t1.seconds >9){
    out <<"0"<< t1.hours << ":" << t1.minutes << ":" << t1.seconds << endl;
    }

    else if(t1.hours < 10 && t1.minutes < 10 && t1.seconds >9){
    out <<"0"<< t1.hours << ":0" << t1.minutes << ":" << t1.seconds << endl;
    }

    else if(t1.hours < 10 && t1.minutes < 10 && t1.seconds < 10){
    out <<"0"<< t1.hours << ":0" << t1.minutes << ":0" << t1.seconds << endl;
    }

    else if(t1.hours < 10 && t1.minutes > 9 && t1.seconds < 10){
    out <<"0"<< t1.hours << ":" << t1.minutes << ":0" << t1.seconds << endl;
    }

    else if(t1.hours >9 && t1.minutes < 10 && t1.seconds >9){
    out << t1.hours << ":0" << t1.minutes << ":" << t1.seconds << endl;
    }

    else if(t1.hours >9 && t1.minutes < 10 && t1.seconds < 10){
    out << t1.hours << ":0" << t1.minutes << ":0" << t1.seconds << endl;
    }

    else if(t1.hours > 9 && t1.minutes > 9 && t1.seconds < 10){
    out << t1.hours << ":" << t1.minutes << ":0" << t1.seconds << endl;
    }

    else {
        out << t1.hours << ":" << t1.minutes << ":" << t1.seconds << endl;
    }

    return out;
}

Time operator + (Time t1, Time t2 )
{

    Time t3;
    t3.hours=t1.hours+t2.hours;
    t3.minutes=t1.minutes+t2.minutes;
    t3.seconds=t1.seconds+t2.seconds;
    return t3;
}

Time operator - (Time t1, Time t2 )
{
     Time t4;
     t4.hours=t1.hours-t2.hours;
     t4.minutes=t1.minutes-t2.minutes;
     t4.seconds=t1.seconds-t2.seconds;
     t4.normalize();
     return t4;
}

bool operator<(const Time& t1, const Time& t2) {
    if(t1.hours < t2.hours) {
        return true;
    }
    else if(t1.hours > t2.hours) {
        return false;
    }
    else {
        if(t1.minutes < t2.minutes) {
            return true;
        }
        else if(t1.minutes > t2.minutes) {
            return false;
        }
        else {
            if(t1.seconds < t2.seconds) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}

void Time::normalize()
{
    int s = seconds;
    int m = minutes;
    int h = hours;

    while(s < 0)
    {
        s += 60;
        m--;
    }

    while(m < 0)
    {
        m += 60;
        h--;
    }

    while(h < 0)
    {
        h = h + 24;
    }

    seconds = s % 60;
    minutes = (m + s/60) % 60;
    hours = (h + m/60 + s/3600) % 24;
}

int main()
{
    Time t1, t2, t3, t4;
    cin >> t1;
    cin >> t2;
    cin >> t3;

    cout << "Time1: " << t1;
    cout << "Time2: " << t2;
    cout << "Time3: " << t3;

    t4 = t1 + t2;
    cout << "Time4: " << t4;

    t1 = t3 - t4;
    cout << "Time1: " << t1;

    if (t1 < t3)
        cout << "Time1 < Time3" << endl;
    else
        cout << "Time3 >= Time1" << endl;

    Time t5 = t2 + Time(0,0,1);
    if (t5 < t2)
        cout << "Time5 < Time2" << endl;
    else
        cout << "Time5 >= Time2" << endl;


    cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;

    return 0;
}


Last edited on
1
2
3
4
5
6
Time::Time()
{
    hours = minutes = seconds = 0;
    normalize();

}

it doesnt make sense to call normalize here.

by the way, its up to you if you want to be able to have a costumize time the moment the time object is instantiated
Topic archived. No new replies allowed.