CPSC 427: Object-Oriented Programming
Michael J. Fischer
Overview of PS5
Challenges PS5 is to add a second agent type to the simulated population.
This creates several challenges.
Experiments and Observations
Once your code is running, use it to get some understanding for how the
number of steps to reach consensus depends on the parameters.
Particularly interesting is to see the effect of adding a small percentage of Crowd agents to a population consisting primarily of Fickle agents. The difference should become obvious in a population of size 10,000 or so.
Move Demo
Special member functions demo Recall the six so-called special member functions:
These are automatically defined if you do nothing, but defining some of
them inhibit the automatic definition of others.
Automatic definitions can be enabled by explicitly writing =default or disabled by writing =delete.
Special member functions demo
The demo 17-SpecialMbrFcns defines all six special functions and shows
how they can be invoked.
It defines a class T with two private data members: an integer x and an integer pointer a.
Default constructor and destructor
This uses a ctor to initialize the two data members to 0 and nullptr, respectively. It then announces itself.
This deleted the dynamic extension a and announces itself.
Additional constructor
This initializes x using a ctor. a is initialized using the initializer = new int[3] defined in the class. The keyword explicit inhibits it from being used implicitly to convert an int to a T.
Copy constructor and move constructor
Uses ctor to initialize x and a from corresponding members of rhs.
Same as copy constructor but prevents automatic deletion of the dynamic extension in rhs by setting a to nullptr.
Copy assignment
Uses operator=() to assign x and a from the corresponding members
of rhs. Returns a reference to the left-hand side in keeping with other
assignment operators.
Move assignment
Similar to copy assignment, but:
Invoking the special functions
The main program in demo 17-SpecialMbrFcns prints a C++ statement along with output showing what happened.
Invoking the special functions
Invoking the special functions
Bells and Whistles
Optional
parameters
The same name can be used to name several different member functions
if the signatures (types and/or number of parameters) are diffent. This is
called overloading.
Optional parameters are a shorthand way to declare overloading.
Example
int myfun( double x, int n=1 ) { ... }
This in effect declares and defines two methods:
int myfun( double x ) {int n=1; ...}
int myfun( double x, int n ) {...}
The body of the definition of both is the same.
If called with one argument, the second parameter is set to 1.
const
const declares a variable (L-value) to be readonly.
const implicit argument
const should be used for member functions that do not change data members.
Operator extensions
Operators are shorthand for functions.
Example: <= refers to the function operator <=().
Operators can be overloaded just like functions.
Now can write
if (a <= b) ...