Function Hiding

If you have a function with the same name (even with different parameters) in a derived class, it will hide the function in the base class (without the virtual keyword).


Example hiding :

//hiding5.cpp

#include <iostream>

class Base {
public:
	virtual void fun(int i, int j) const { std::cout << "Base " << i << " " << j << std::endl; }
};

class Derived: public Base {
public:
	void fun(char c) const {std::cout << "Derived" << i << " " << j << endl;}
};

int main() {
	Derived d;
	d.fun(76, 77); // Derived 76 77
	d.Base::fun(76, 77) // Base 76 77
	((Base&) d).fun(76, 77) // Derived 76 77
	((Base) d).fun(76, 77) // Base 76 77
}

Pure virtual functions

class Shape {
public:
	virtual double size() const = 0;
	...
};


When we declare a pure virtual function:

Abstract Classes

A class is abstract if it has at least one pure virtual function or has only non-public constructor.


Another way to make a class abstract is give it only non-public constructors.


Example:

class Piece {
public:
	...
protected:
	// this is the only constructor
	Piece (bool is_white): is_white(is_white) { }
	...
private:
	bool is_white;
};

class Queen: public Piece {
	...
	// Queen constructor calls Piece constructor
	// OK because it's protected
	Queen(bool is_white): Piece (is_white) { } 
	...
};