Hello Kevin,
that is a very interesting question. I have two answers for you.
1) When thinking in databases, one person can have 0,1 or more band accounts. So the class BankAccount
can not have a member variable Person bankMember. Otherwise, for more than one bank account, it result
in duplicate and redundant data.
In terms of databases, the bank account should have a personID. The code would look like this:
persons[bankaccont1.personID].firstName;
persons[bankaccont2.personID].firstName;
2) Over the years I created my own design philosophy: "Create once, never change." It is not the OO like thinking from school with all the setter/getter functions. It is OO without setter.
The name and birth date of a person never change, once you created it.
(Okay, okay they does change. But how often in your life do you change your name or birth date? Almost never. )
So, create and access an object should be simple, change it data should be hard.
Same for bank accounts. Create a bank account with one person. But the owner of a bank account does not change.
You would create a new bank account and transfer the money...
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
|
#include <iostream>
#include <string>
#include <vector>
class Date {
struct Data {
int day;
int month;
int year;
};
Data d;
public:
Date(int day, int month, int year)
: d{day, month, year}
{
}
int day() const {
return d.day;
}
int month() const {
return d.month;
}
int year() const {
return d.year;
}
Data& change() {
return d;
}
};
class Person {
struct Data {
std::string firstName;
std::string lastName;
Date birthday;
};
Data d;
public:
Person(std::string firstName, std::string lastName, Date birthday)
: d{firstName, lastName, birthday}
{
}
std::string firstName() const {
return d.firstName;
}
std::string lastName() const {
return d.lastName;
}
Date birthday() const {
return d.birthday;
}
Data& change() {
return d;
}
};
int main() {
// easy to create the datum
Person person("Thomas", "Huxhorn", Date{19,04,1984});
// easy to access the datum
std::cout << person.firstName() << " " << person.birthday().day();
// hard the change
person.change().birthday.change().day = 18;
// person.birthday().day() = 18; // does not compile
}
|