Use of Properties
Explore how to use properties in D programming to encapsulate member variables, ensuring controlled access and modification. Understand why properties improve code maintenance by restricting direct variable access and fostering better design in object-oriented structures.
We'll cover the following...
We'll cover the following...
Properties are not absolutely necessary
We saw in the previous lesson how Rectangle can be used as if it has a third member variable. However, regular member functions could also be used instead of properties:
Further, as we already know, these two functions could even have the same names:
double area() const {
// ...
}
void area(double newArea) {
// ...
}
When to use properties
It may not be easy to choose between regular member functions and properties. Sometimes, regular member functions feel more natural, and sometimes properties do.
However, as you have seen in the ...