How do I change the output for a class if I do work in the main function?

And this program is about bank accounts with checking account/saving accounts using inheritance and such.
This is my main function:
Problem is lines 38-whenever ifelse statements end
The problem is I'm only supposed to deduct service charge when minimum balance is less than $1000 but I don't know how to go into my class since ca1-ca4 is undeclared so I can't print out something to change it.
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
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

int main() {

	Person jack("Jack");
	Person lisa("Lisa");
	Person omar("Omar");
	Person rita("Rita");

	double minBalance = 1000;
	double charge = 40;

	CheckingAccount ca1(1000, 1050, jack, minBalance, charge);// Don't take service charge yet until called for.
	CheckingAccount ca2(1001, 450, lisa, minBalance, charge);
	CheckingAccount ca3(1002, 930, omar, minBalance, charge);
	CheckingAccount ca4(1003, 32, rita, minBalance, charge);

	cout<<"Minimum balance should be $1000 dollars and service charge is $40."<<endl<<endl;

	SavingsAccount sa1(2000, 1000, jack, .04);//created Savings Account
	SavingsAccount sa2(2001, 2000, lisa, .07);
	SavingsAccount sa3(2002, 1600, omar, .05);
	SavingsAccount sa4(2003, 500, rita, .03);

	ca1.withdrawMoney(120);
	sa4.withdrawMoney(90);
	ca2.withdrawMoney(120);
	ca3.depositMoney(290);

	sa1.postInterest();
	sa2.postInterest();
	sa3.postInterest();
	sa4.postInterest();

	cout<<"Verify whether the balance is less than the minimum balance of $1000 or not.  If less, deduct $40"<<endl<<endl;
	cout<<ca1.getBalance()<<endl;
	cout<<ca2.getBalance()<<endl;
	cout<<ca3.getBalance()<<endl;
	cout<<ca4.getBalance()<<endl;

		if(ca1.getBalance() < minBalance){charge = 40; cout<<ca1.getBalance() - charge<<endl;}//this is the part I need to change but my output is always $40 when it isn't supposed to be. How do I change that to not post $40 when it isn't supposed to?
	else charge = 0;
		if(ca2.getBalance() < minBalance){charge = 40; cout<<ca2.getBalance() - charge<<endl;}
	else charge = 0;
		if(ca3.getBalance() < minBalance){charge = 40; cout<<ca3.getBalance() - charge<<endl;}
	else charge = 0;
		if(ca4.getBalance() < minBalance){charge = 40; cout<<ca4.getBalance() - charge<<endl;}
	else charge = 0;

	ca1.depositMoney(1000);
	sa2.depositMoney(2300);
	ca3.depositMoney(800);
	sa4.depositMoney(500);

	cout << endl << "Checking Account Information"<<endl;

	ca1.printInfo();
	ca2.printInfo();
	ca3.printInfo();
	ca4.printInfo();

	/*sa1.setInterestEarned(sa1.getInterestRate());
	sa2.setInterestEarned(sa2.getInterestRate());
	sa3.setInterestEarned(sa3.getInterestRate());
	sa4.setInterestEarned(sa4.getInterestRate());

	cout<<endl<<"Savings Account Information"<<endl;

	sa1.printInfo();
	sa2.printInfo();
	sa3.printInfo();
	sa4.printInfo();*/

	system("pause");
	return 0;
}


Checking account cpp
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
#include "CheckingAccount.h"

CheckingAccount::CheckingAccount(int accountNumber, double accountBalance, Person name, double minBalance, double serviceCharge) 
	: BankAccount(accountNumber, accountBalance, name) {
		setMinBalance(minBalance);
		setServiceCharges(serviceCharge);
}

void CheckingAccount::setMinBalance(double minBalance){
	this->minBalance = minBalance;
}

double CheckingAccount::getMinBalance(){
return minBalance;
}

void CheckingAccount::setServiceCharges(double serviceCharge){
	this->serviceCharges = serviceCharge;
}

double CheckingAccount::getServiceCharges(){
	return serviceCharges;
}

void CheckingAccount::printInfo() {
	cout << "Name: "<< this->getPerson().getName();
	cout << " - Account Number: " << this->getAccountNumber();
	cout << " - Balance: $" << this->getBalance();
	cout << " - Service Charges: $" << this->getServiceCharges() << endl;
}
Last edited on
I tried doing this:
ca1.getBalance() = ca1.getBalance() - ca1.getServiceCharges();
but I have an error saying ca1 must be a modifiable value :\

then I tried == and it worked but I'm not sure if anything changed
Last edited on
Consider this:
1
2
3
4
5
int getNumber()
{
    static int num = 4;
    return num;
}
1
2
getNumber() = 3;
//wait, what the heck does THAT do? 
1
2
3
4
5
int &getNumber()
{
    static int num = 4;
    return num;
}
1
2
getNumber() = 3;
//ah, better, but why are we SETting from a GETter? 


In C++ it is common to see this in classes that do not follow the 'tell, don't ask' system:
1
2
int ThatNumber(){ return num; }
void ThatNumber(int Num){ num = Num; /*maybe validates Num*/ }

Aka, a 'getter' and a 'setter'.
Last edited on
Okay, so you get number 4 but you set it to 3?... Oh wait... You're saying I'm supposed to be using my set function......?
Yes, either use references (not very good practice) or use getter/setter system (better practice).

You want to SET the value, not change what you get.
(I'm still new in the get/set methods)

but How do I know how to format it?

Like,
ca1.setAccountBalance() is it = or == ? I don't remember when you use == or =
== sets them equal to each other I think x_o .....
= is for assigment, or setting a value. It is NOT a setter function, and it's NOT what you should be using for this.
== is for equality comparison, it's a check that returns true if they are equal and false if they aren't. It's also NOT what you should be using for this.
If what you're saying is I shouldn't use = or == at all then how do I subtract service charge..?

ca1.setAccountBalance() but how would you set if you can't do any arithmetic.
Last edited on
MyObjectA.SetSomething(MyObjectB.GetSomething() - MyObjectC.GetSomething());
oh... you're allowed to do that? .... -_________________________________-
I love you. T_T Life saver.

Sorry for my stupidity, I had a tutor who just taught me get set and I guess I didn't know how to use them as I thought x_o

Btw, it works now (: . code now matches up with my class mates :D

Last edited on
You can use expressions anywhere in C++. Once you know that you can use expressions anywhere, things will start to open up in your mind ;)
yeah, hopefully I can remember this :D .

I can't tell you how happy/excited I am that it works ^-^

But quick question you don't use assignment operators for get/set functions in main?
No, you can't readily assign to a temporary variable.

Allow me to explain;
1
2
3
4
5
6
7
8
class Explain
{
    Explain(){ cout << "Default Constructor" << endl; }
    Explain(const Explain &from){ cout << "Copy Constructor" << endl; }
    Explain &operator=(const Explain &from){ cout << "Assignment" << endl; return*this; }

    ~Explain(){ cout << "Destructor" << endl; }
};
With this class we can see what is happening. Don't worry if you don't understand every line, you will in a moment.
1
2
3
4
5
6
int main()
{
    Explain a; //prints "Default Constructor"
    Explain b (a); //prints "Copy Constructor"
    a = b; //prints "Assignment"
} //prints "Destructor" twice, once for b then for a 
1
2
3
4
5
6
7
8
9
10
Explain Func()
{
    Explain e; //prints "Default Constructor"
    return e; //Makes a copy of e and prints "Copy Constructor", then destroys the original e and prints "Destructor"
}
int main()
{
    Func(); //prints "Default Constructor", "Copy Constructor", "Desructor" (for e), "Destructor" (for the copy)
    Explain a (Func()); //Same as above, but before printing "Destructor" the second time, prints "Copy Constructor" for 'a'
}
I hope this shows that, because you're trying to assign to a temporary, it just won't work. ;)
Thanks so much (: !

Funny thing, I just noticed my prof went over this in class today. lol
Last edited on
Topic archived. No new replies allowed.