Calculate and store value in data member

I wrote a program. It should calculate the VAT and the net cost of the bills.
Anyone can tell me how can I calculate the net and the VAT and to store its value in each data member?

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

#include <iostream>
#include <string>

class ElectricBill{
public:
   ElectricBill(double accountBill)
    : amountPaid {accountBill} {
        if (amountPaid < 0){
          amountPaid = 0;
        }
    }

void setAmount (double billAmount){
    amountPaid = billAmount;
}

double getAmount() const{
    return amountPaid;
}

void setVAT(){
    VAT = amountPaid * 0.18;
}

double getVAT() const{
    return VAT;
}

void setNet(){ 
    net = amountPaid - VAT;
}

double getNet() const {
    return net;
}
private:
double amountPaid;
double VAT;
double net;
};

int main(){
ElectricBill bill1{300};

std::cout << "Amount of the bill: "
 << bill1.getAmount() << 
 "\nthe VAT is: " << bill1.getVAT() 
 << "\nnet cost is: " << bill1.getNet();
}
Last edited on
You have two options:
* call bill1.setVAT() and bill1.setNET() before you call the bill1.get*
* calculate the VAT and net in the methods that modify the amountPaid

The latter makes more sense, because it keeps the Bill consistent.
Topic archived. No new replies allowed.