Rewrite the class Person as the composition a Person 'has-an' Account including the following: 1. encapsulation 2. const member functions 3. all functions required for the main function to compile class Account { private: float balance; public: Account(): balance(0.0) {} Account(float f): balance(f) {} float getBalance() const { return balance; } void setBalance(float f) { balance = f; } }; class Person { Account savings; string name; Person(); }; int main() { Account a(15000); Person p1(); Person p2("John", 10000); Person p3("Kim", a); p2.setName("Joe"); cout <
//Code
class Account {
private:
float balance;
public:
Account(): balance(0.0) {}
Account(float f): balance(f) {}
float getBalance() const { return balance; }
void setBalance(float f) { balance = f; }
};
class Person {
private:
Account savings;
string name;
public:
Person(): name("") {}
Person(string n, float f): name(n) {
savings.setBalance(f);
}
Person(string n, Account a): name(n), savings(a) {}
void setName(string n) { name = n; }
string getName() const { return name; }
float getBalance() const { return savings.getBalance(); }
void print() const {
cout << name << ": " << savings.getBalance() << "\n";
}
};
int main() {
Account a(15000);
Person p1;
Person p2("John", 10000);
Person p3("Kim", a);
p2.setName("Joe");
cout << p3.getName() << ": " << p3.getBalance() << "\n";
p3.print();
return 0;
}
Step by step
Solved in 2 steps