You can declare that a member function does not change its object by using the word const in the declaration or definition:
type name (parameters) const { body }
type name (parameters) const ;The result is called an accessor. In the UML this is called a query.... and in DIA they use the C++ notation not the on in the UML standard.
using namespace std::rel_ops;to make all the comparison operators work.
8.4.4 Assignment operator
These take great care to program correctly -- leave until CSCI202!
8.4.5 The Indexing operator
How to make your own array work just like the real things!
Notice -- sometimes we need two versions. One with const and one without.
. . . . . . . . . ( end of section 8.4 Operators) <<Contents | End>>
. . . . . . . . . ( end of section Reading) <<Contents | End>>
Glossary
cout << 8*sizeof(int) <<endl;to find out the answer.
In the UML a private member is marked with a minus and a public one with a plus sign.
The tradition is to make all data private and most functions public. You only allow other classes, functions, and the main program to use the private data in public ways. This makes it harder for programs to do something unexpected.
The only way to change or get the value of a private int is to use a prewritten public operator/function to do it.
What are the friends of a class
A class may declare another class to be a friend. It can also declare a function
to be a friend.
What do friends do
The have access to the private data and functions of a class.
Avoid them.
What does this do
The keyword
this
in a member function in a class is a pointer variable with
a predefined value. It points at the current object.
It's main purpose in C++ is to let you send it to another object:
somebody.pleaseCallMeBack(this);It is like sending your phone number so people know where to contact you.
What does the double colon do
It separates a class name from the name of a data field or function in that class.
It makes a complex name out of simple ones. You can use it to make one
part of a program refer to a part of a different class -- as long as that is public.
Give an example of a constant object
Constant numbers like π are obviously useful. It is rare to need a constant
object for a class.
However it is very wise to pass parameters that refer to objects as constant
references. That way the function can see the value of the object but
not change it.
Why do we have to define our own comparison operators for an Array
C++ doesn't define one as standard -- so when we have a class of objects that
we want to compare then we have to tell C++ how we want it done.
What is an assignment operator
The main assignment operator is '='. It is used like this
variable = expression;However there are half-a-dozen others: +=, -=, *=, /=, <<=, >>=, ...
For example
variable += expression;means
variable = variable + expression;
What are unary operators
The following symbols can be used on numbers as unary operators: - ++ -- ! not * ~.
You can make them work, with the same syntax, on any class of object, if you need to, by defining operations like: operator !.
Details are in the text book and are not part of this class.
Quiz 9 will be all about the relationship between UML and C++ code.
Lab10 TBA