Basic Inheritance¶
the following sample program shows how to use inheritance to help create a new class
Point
represents a 2D (x, y) point
Color_point
represents a 2D (x, y) point, plus an RGB color (red, green,
blue)
here, Color_point
derives from Point
, and inherits all the public
variables and methods in Point
constructors are not inherited; instead, they are called in the
initialization lists of the Color_point
constructors
since Color_point
is derived from Point
, Color_point
objects are
of type Color_point
and also of type Point
, e.g.:
Color_point p; // p is of type Color_point
// p is also of type Point
// you can use p anywhere you need an object of type Point
a Color_point
object can always be used in any case where a Point
object is needed
be careful, it doesn’t go the other way: you can’t use a Point
in a case
where you need a Color_point
// colorpoint.cpp
#include "error.h"
#include <iostream>
using namespace std;
// Everything is public in Point. While we could have used setter and getter
// functions to access x and y, in most cases those functions wouldn't provide
// any functionality different than accessing x and y directly.
class Point {
public:
double x;
double y;
// default constructor
Point() : x(0), y(0) { }
// copy constructor
Point(const Point& other) : x(other.x), y(other.y) { }
Point(double a, double b) : x(a), y(b) { }
void print() const {
cout << '(' << x << ", " << y << ')';
}
void println() const {
print();
cout << "\n";
}
Point& operator=(const Point& other) {
if (&other == this) {
// do nothing
} else {
// &other != this is true
this->x = other.x;
this->y = other.y;
} // if
return *this;
}
}; // class Point
bool operator==(const Point& a, const Point& b) {
return a.x == b.x && a.y == b.y;
}
bool operator!=(const Point& a, const Point& b) {
return !(a == b);
}
class Color_point : public Point {
private:
int red, green, blue;
public:
Color_point()
: Point(), red(0), green(0), blue(0)
{ }
Color_point(int r, int g, int b)
: Point(), red(r), green(g), blue(b)
{ }
Color_point(double x, double y)
: Point(x, y), red(0), green(0), blue(0)
{ }
Color_point(double x, double y, int r, int g, int b)
: Point(x, y), red(r), green(g), blue(b)
{ }
Color_point(const Color_point& other)
: Point(other), red(other.red), green(other.green), blue(other.blue)
{ }
void print() const {
Point::print();
cout << ":<" << red << ", " << green << ", " << blue << ">";
}
void println() const {
print();
cout << "\n";
}
Color_point& operator=(const Color_point& other) {
if (&other == this) {
// do nothing
} else {
// &other != this is true
this->x = other.x;
this->y = other.y;
this->red = other.red;
this->green = other.green;
this->blue = other.blue;
} // if
return *this;
}
// this gives operator== access to Color_point's private variables
friend bool operator==(const Color_point&, const Color_point&);
}; // class Color_point
bool operator==(const Color_point& a, const Color_point& b) {
return a.x == b.x && a.y == b.y
&& a.red == b.red && a.green == b.green && a.blue == b.blue;
}
bool operator!=(const Color_point& a, const Color_point& b) {
return !(a == b);
}
int main() {
Point a(1, 2);
a.println();
Point b = a;
b.println();
Color_point c(6, 4);
c.println();
Color_point d(c);
d.println();
if (d != c) {
cout << "oops: d and c are different!\n";
} else {
cout << "ok: d and c are equal\n";
}
Color_point e = d;
e.println();
} // main