How to handle pointer variables inside a class member function

Thanks
Last edited on
Balance and interestRate shouldn't be pointers. They should be doubles themselves. There are several other problems in this code but I think you'll find them when you test it.

In deposit() and withdraw() I've used the += and -= equal operators.
1
2
balance += dollars; // same as balance = balance + dollars;
balance -= dollars; // same as balance = balance - dollars; 

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
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H

#include <iostream>
//#include <cstdlib>
using namespace std;


class BankAccount
{
private:

    double balance;
    double interestRate;
    static int count;
    double fraction(double percent);
    
public:
    BankAccount();
    BankAccount(int dollars, int cents, double rate);
    ~BankAccount();
    static int getCount();
    void deposit(double);
    void withdraw(double);
    static int getAccount();
    void set(int dollars, int cents, double rate);

    void set(int dollars, double rate);
   

    void update();
    double getBalance() const;
    double getRate() const;  
    void output(ostream& outs) const;
    
};
#endif



#include <iostream>
#include <cstdlib>
// #include "Bank_Account.h"

using namespace std;

int BankAccount::count = 0;

BankAccount::BankAccount() {
    balance = 0;
    interestRate = 0;
    count++;
    cout << "Constructor called" << endl;
}

BankAccount::BankAccount(int dollars, int cents, double rate) {
    if ((dollars < 0) || (cents < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
        exit(1);
    }
    // here is where I dont know how to handle my pointers when i have
    // to do some sort of function and assign it to my pointer
    balance = dollars + 0.01 * cents;
    interestRate = rate;
    count++;
    cout << "Constructor called" << endl;

}

int BankAccount ::getCount()
{
    return count;

}
void BankAccount::set(int dollars, int cents, double rate)
{

    if ((dollars < 0) || (cents < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
        exit(1);
    }

    balance = dollars + cents/100.0 + fraction(cents %100);
    interestRate = fraction(rate);
}

void BankAccount::set(int dollars, double rate)
{
    if ((dollars < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
        exit(1);
    }

    balance = dollars;
    interestRate = rate;
}

void BankAccount::update()
{
    balance += balance + interestRate;
}

double BankAccount::fraction(double percentValue)
{
    return (percentValue / 100.0);
}



double BankAccount::getBalance() const
{
    return balance;
}

double BankAccount::getRate() const
{
    return interestRate + 100;
}

//Uses iostream:
void BankAccount::output(ostream& outs) const
{
    outs.setf(ios::fixed); //to display trailing zeros
    outs.setf(ios::showpoint); //to display . for decimal point
    outs.precision(2);		 //how many decimal places after .
    outs << "Account balance $" << balance << endl;
    outs << "Interest rate " << interestRate << "%" << endl;
}

//Destructor
BankAccount::~BankAccount() {
    cout << "Destructor called" << endl;
}

//also not sure how to implement these functions 
void BankAccount::deposit(double dollars) { balance += dollars; }
void BankAccount::withdraw(double dollars){ balance -= dollars; }
static int getAccount(){}

The requirements are to have balance and interestRate as double * Could you please use double * balance, interestRate . That is the whole point of my question. I dont know how to use the pointers and ignoring them is not answering my question. Sorry
ignoring them is not answering my question

It's OK not to like the answer, denying it is a different kettle of fish altogether.
FWIW a well established C++ teacher, Savitch, Ch10 where this problem and code originates, doesn't use pointers.

Cheggs seems to, maybe ask them.
The requirements are to...


Could you post the complete requirements of your homework? It may assist to help you better.
Using pointers as class data members add a lot of complexity to the class that makes it harder to work with and potentially be prone to errors.
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
#include <iostream>

class Test
{
private:
   double* value;

public:
    Test() { value = new(double)(0); } // create on the heap and set to zero
   ~Test() { delete value; }           // heap memory must be cleaned up

   void AddFive() { *value += 5; }

   double GetValue() const { return *value; }
};

int main()
{
   Test t1;

   std::cout << t1.GetValue() << '\n';

   t1.AddFive();

   std::cout << t1.GetValue() << '\n';

   Test* t2 = new(Test);

   std::cout << t2->GetValue() << '\n';

   t2->AddFive();

   std::cout << t2->GetValue() << '\n';

   delete t2;
}

AFAIK the proper way to have a data member that is a pointer is to point the member to an address on the heap when the class is constructed. That requires the allocated memory to be cleaned up in the destructor.
AFAIK the proper way to have a data member that is a pointer is to point the member to an address on the heap when the class is constructed. That requires the allocated memory to be cleaned up in the destructor.


The 'proper' C++ way is to use smart pointers - unique_ptr or shared_ptr. See https://en.cppreference.com/w/cpp/memory
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <memory>

class Test
{
private:
	std::unique_ptr<double> value {std::make_unique<double>(0)};

public:
	void AddFive() { *value += 5; }

	double GetValue() const { return *value; }
};

int main()
{
	Test t1;

	std::cout << t1.GetValue() << '\n';

	t1.AddFive();

	std::cout << t1.GetValue() << '\n';
}

@rnima Please DON'T delete your question once you've gotten an answer. It makes the thread useless as a learning resource for other readers. It's selfish, and is an abuse of this forum.
@MikeyBoy, the OP has done this before. We have another oxygen thief wanting us to do his homework.
Topic archived. No new replies allowed.