Inheritance
Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification.
- Process of deriving new classes based on existing proven classes.
- It allows the programmer to use existing classes as a base for the development of new ones. Which have common features with those that have already been developed.
- The already developed classes are called “super classes” or “base classes”, the classes which inherits the properties are called “child classes”.
- Software reusability is possible through inheritance
Example
class Point
{
public:
Point ();
Point(double xval, double yval);
void move (double dx, double dy);
double getX() const;
double getY () const;
private:
double x;
double y;
};
class Point3D: public Point {
int _z;
public:
Point3D() {
setX(0);
setY(0);
_z = 0;
}
Point3D(const int x, const int y, const int z) {
setX(x);
setY(y);
_z = z;
}
~Point3D() { /* Nothing to do */ }
int getZ() { return _z; }
void setZ(const int val) { _z = val; }
};
|