Construct a class named Savings containing three floating-point data members named balance, rate, and interest and a constructor that initializes each data member to 0. Include a member function that inputs a balance and rate and then calculates an interest. The rate should be stored as a percent, such as 6.5 for 6.5%, and the interest is computed as interest = (balance)(rate / 100). Add a member function to display all data member values.
时间: 2023-07-02 14:11:47 浏览: 52
Sure! Here's an implementation of the Savings class you described in C++:
```c++
#include <iostream>
class Savings {
private:
float balance;
float rate;
float interest;
public:
Savings(): balance(0), rate(0), interest(0) {}
void inputBalanceAndRate(float b, float r) {
balance = b;
rate = r;
interest = balance * (rate / 100);
}
void display() {
std::cout << "Balance: $" << balance << std::endl;
std::cout << "Rate: " << rate << "%" << std::endl;
std::cout << "Interest: $" << interest << std::endl;
}
};
```
You can create an instance of the `Savings` class and call its member functions like this:
```c++
int main() {
Savings mySavings;
mySavings.inputBalanceAndRate(1000, 5.5);
mySavings.display();
return 0;
}
```
This will output:
```
Balance: $1000
Rate: 5.5%
Interest: $55
```
I hope this helps you! Let me know if you have any other questions.