class myOldClass
{
// Contains variables + functions
...
}
class myNewClass public : public myOldClass
{
// Inherits all variables and functions
// in "myOldClass"
new variables + new functions
...
}
|
The old class:
class myOldClass
{
public:
int i1;
void func1(int i)
{
i1 = i1 + i;
}
};
|
The new class:
class myNewClass : public myOldClass
{
public:
int i2;
void func2(int i)
{
i2 = i2 - i;
}
};
|
The new class has the following member variables and functions:
|
It is as though we have defined myNewClass as follows:
class myNewClass
{
public:
int i1;
int i2;
void func1(int i)
{
i1 = i1 + i;
}
void func2(int i)
{
i2 = i2 - i;
}
};
|
int main(int argc, char *argv[])
{
myNewClass B;
B.i1 = 0;
B.i2 = 0;
B.func1(1);
cout << B.i1 << "\t" << B.i2 << "\n";
B.func2(9);
cout << B.i1 << "\t" << B.i2 << "\n";
}
|
|
|
|
|
It provides
the ability to change a function
depending on the
type of the variable
(See: click here)
With the programming mechanism called polymorphism, we can change the function call operator*() depending on the type of the variable passed to the function.
|
Before we can understand how such program can be written, I need to discuss function polymorphism first.