(Latest Revision: Mon Sep 28 12:01 PDT 2015 )
X
\
\
Y
A class Y that has been derived from a class X is all that X was,
and more. Y has additional data and/or function members.
Sphere
\
\
SoccerBall
and both classes have member functions (methods)
called drawSelf then if I have a variable
mySoccerBall of type SoccerBall and
I make a call to
mySoccerBall.drawSelf(), it will cause the version of drawSelf
belonging to the SoccerBall class to be executed , and not the
version of drawSelf derived from the Sphere class. (Presumably
the result will be that the drawing will look more like a soccer
ball and not like a plain sphere.) //THIS IS THE BASE CLASS (SPHERE)
class sphereClass
{
private:
double TheRadius; // the sphere's radius
public:
/* constructors. */
sphereClass();
sphereClass(double InitialRadius);
/* copy constructor and destructor will be
supplied by the compiler */
// sphere operations:
void SetRadius(double NewRadius);
double Radius() const;
double Diameter() const;
double Circumference() const;
double Area() const;
double Volume() const;
void DisplayStatistics() const;
}; // end class
Here is how the derived class is declared -- less work to do here
because we get lots of "declarations" from the base class "for
free"
const int MAX_STRING = 15;
class ballClass: public sphereClass
/* The line above says that we want ballClass to be
derived from sphereClass (publicly)*/
{
private:
/* this data member is a new one -- in addition to
TheRadius, which we get from the parent class */
char TheName[MAX_STRING+1]; // the ball's name
public:
ballClass(); // default constructor
/* Another constructor: Creates a ball with radius
InitialRadius and name InitialName. */
ballClass(double InitialRadius, const char InitialName[]);
/* copy constructor and destructor supplied
by the compiler */
/* It's important to notice what is NOT here:
all the member functions of sphereClass
-- no need to re-declare -- we get all that
automatically. For example users will be able
to make calls like: ball.Volume() when ball
is an instance of ballClass (a variable of
type ballClass). */
/* additional or revised operations: */
/* this operation is new in the derived class
Determines the name of a ball. */
void GetName(char CurrentName[]) const;
/* this operation is new in the derived class
Sets (alters) the name of an existing ball. */
void SetName(const char NewName[]);
/* this operation is new in the derived class Sets
(alters) the radius and name of an existing ball
to NewRadius and NewName, respectively. */
void ResetBall(double NewRadius, const char NewName[]);
/* this operation is re-defined in the derived
class. It displays the statistics of a
ball. */
void DisplayStatistics() const;
}; // end class
Important characteristics:
/* New constructors make use of the constructor(s) of
the base class. Note initializer syntax using
constructor of sphereClass. But note that the name
of the constructor (and class) is used here -- not
the name of a variable -- so this initializer form
is different from the form use to initialize data
members of ballClass. */
ballClass::ballClass() : sphereClass()
{
SetName("");
} // end default constructor
/* Again use of initializer syntax and sphereClass
constructor call */
ballClass::ballClass(double InitialRadius,
const char InitialName[])
: sphereClass(InitialRadius)
{
SetName(InitialName);
} // end constructor
/* This new function's definition is typical of many --
no need to reference the sphereClass in any way
here. */
void ballClass::GetName(char CurrentName[]) const
{
strcpy(CurrentName, TheName);
} // end GetName
void ballClass::SetName(const char NewName[])
{
strcpy(TheName, NewName);
} // end SetName
void ballClass::ResetBall(double NewRadius,
const char NewName[])
{
/* calling a member function of the base class --
sphereClass */
SetRadius(NewRadius);
/* calling a member function of the derived class --
ballClass */
SetName(NewName);
} // end ResetBall
void ballClass::DisplayStatistics() const
{
cout << "Statistics for a " << TheName << ":";
/* calling a member function of the base class
-- sphereClass Here the programmer had to use
the scope resolution operator to resolve the
ambiguity caused because ballClass has its own
version of void DisplayStatistics() const. */
sphereClass::DisplayStatistics();
} // end DisplayStatistics
ballClass ballis seen in a program, first the constructor of sphereClass runs, then the constructor of ballClass.
base class A
\
\
derived class B (not affected by its inheritance type)
\
\
derived class C (affected by B's inheritance type)
base class A
\
\
derived class B (not affected by its inheritance type)
\
\
instance b of B (affected by B's inheritance type)