Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions 02cpp1/sec03ObjectOrientedProgramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Ball
std::array<double, 3> position;
double radius;
double mass;
}
};
```

These fields define the sphere well, but physically the behaviour of the sphere in the fluid will depend on its _density_. So perhaps we want to write a member function `double density(double radius, double mass)` which calculates the density of the sphere. But this would mean we need to call the density function and re-calculate it when we want to use it, which isn't ideal. So instead, we can add density to our list of fields,
Expand All @@ -179,7 +179,7 @@ class Ball
double radius;
double mass;
double density;
}
};
```

and then we can call the density directly without another calculation. The problem that we now have is that in order for our data to be self-consistent, **a relationship between the radius, mass, and density must be satisfied**.
Expand Down Expand Up @@ -207,7 +207,7 @@ class Ball
double radius;
double mass;
double density;
}
};
```

Now we can even make our code **more flexible without sacrificing safety**. Let's say the ball can change _mass_ or _radius_. We can't just make these variables public and change them independently, because then the _density_ will no longer be consistent with the new mass / radius. We need to add **setter** functions which **maintain the integrity of the object**:
Expand Down Expand Up @@ -246,7 +246,7 @@ class Ball
double radius;
double mass;
double density;
}
};
```
We now have a ball class that can be instantiated with any mass and radius, and can have its mass or radius changed, but **always satisfies the property that the density field is correct for the given radius and mass of the object**. Being able to guarantee properties of objects of a given type makes the type system far more powerful and gives users the opportunity to use objects in more efficient ways without having to check for conditions that are already guaranteed by the object's design.

Expand Down Expand Up @@ -298,7 +298,7 @@ class Ball
double radius;
double mass;
double density;
}
};
```
**In `ball.cpp`:**
```cpp
Expand All @@ -311,7 +311,7 @@ Ball::Ball(std::array<double, 3> p, double r, double m): position(p), radius(r),

// Again, Ball:: tells us that this function is part of the Ball class definition
// Because this is a member function, it has access to all the data members of this class.
Ball::setDensity()
void Ball::setDensity()
{
density = 3 * mass / (4 * M_PI * pow(radius, 3));
}
Expand Down
Loading