Write down the questions,
doubts, and surprises that you have on a piece of paper.
Error in Book page 219
Dynamic Binding
is done after the code is compiled, and when the
program is running.
Error in Book Page 330
In the box it should say
Include your printed name! Include a page number!
Quick Exercise
Examples: [ poly1.cpp ] [ poly2.cpp ] [ poly3.cpp ] [ poly4.cpp ] [ poly5.cpp ] and [ polystud.cpp ] [ polystud2.cpp ] [ polystud3.cpp ]
Also [ 07ex2.cpp ]
In the UML everything is polymorphic and all operations are "virtual".
Type identifiers and dynamic casting
Useful when you are given general objects (or pointers and references
to general objects) but you need a object in one of the special kinds of
class.
Destructors should be virtual
Do it!
Abstract classes
Crazy notation? I think so... but this dates back
to when virtual functions were implemented by the compiler
as a pointer to the code, and 0 indicates the NULL pointer!
UML notation: Italics. Or write "{abstract}" in the class.
Notes:
UML lollipop notation for interfaces.
Multiple inheritance of several interfaces is a safe and powerful
technique used a lot in Java.
Virtual operators.... tricky
Multiple Inheritance
Avoid!
Exception: OK to inherit and so implement any number of interfaces. Example if time.
Why make a function virtual
If you do then your pointers and references will work like the
objects they point at, rather than the class they appear to point at.
The classic structure has a class
class Derived : public Base {....};
and the program has a pointer to the Base:
Base * pb;which is then attached to a Derived object:
pb = new Derived (....);Now if 'v()' is a virtual function
pb->v();
But if "f()" is not virtual then the command executes "Base::f()".
As a rule we nearly always need pointer to work like the things they point at. (Think demo with magic wand in class). SO virtual gives fewer surprises.
In my opinion you should make all functions virtual (except in toy programs). My evidence -- Java.
Notice the above example of the
If a function is virtual in a base class must it be declared as virtual in a derived class
No.
What is the difference between static and dynamic binding
Binding attaches meaning to identifiers.... if done before the program
starts to run, and never changed, then we say the binding is "static".
Otherwise we say it is "dynamic".
For example the value of a variable has dynamic binding. A constant has a static bound value. The length of an array is statically bound. Data members in a class are statically bound. And the type of a variable is bound by the compiler -- statically.
Virtual functions are bound by the running program.
As a rule dynamic binding is slower, more powerful, and more popular.
How can you declare a nested class inside another
Very easily in C++. Indeed we will have a classic example
where we define a nested class and make it private as a helper
to a main class:
class List{
private:
class Node { public: int data; Node * next };
...
};//end ListOutside (if it was public) it would be called "List::Node".
By the way -- Java 5.0 does have nested classes!
What are static members
A static member belongs to a class, not to any of the objects
in the class. They all share the static member.
By the way -- the word "static" is abused in C++.
A classic example is keeping a count of all the objects that are constructed. The static data member
static int count;is declared and each constructor includes "count++" and each destructor "count --". After the class declare
int Class::count = 0;and the class will know how many instances have been created..
I had another variation when I wanted to be sure that every object had a unique identifier. I used a static int to keep track of the last identifier I had used....
Static functions can be useful when we don't know what object to work with, or when the object has to be constructed. In fact every constructive in a class is static already. But I had a couple of example recently -- what object should handle a Student login? Problem I can't give this responsibility to a Student until I find the Student concerned. So I decided to make the class as a whole search the students for the correct student Id.:
static Student * find ( string name );So I could have code like this:
string name, passwd;
cout << ....
cin >> name >> passwd;
Student * student = find(name);
if( !student) error("no such student");
else {
.... * student ...
}
. . . . . . . . . ( end of section Questions) <<Contents | End>>
Exercises
Lab
[ lab04.html ]
TBA
where you will create a safe array class.
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 07, Inheritance) <<Contents | End>>
(Next): Object Oriented Modeling and a quiz on
C++/UML classes, see
[ 08.html ]
for details.
Prepare for the Quiz
Review this page plus
[ inheritance.html ]
[ polymorphism.html ]
[ abstraction.html ]
[ 08.html ]
Abreviations