3. Syntax #

Created Sunday 26 April 2020

Syntax for inheritance #

Example of syntax

class Camera{ /*code*/ };
class Telephone{ /*code*/ };

class Smartphone : Camera, private Telephone
{ /*code*/ };

Rule for access specifier #

Selective Inheritance #

What if we want to inherit as public, but keep some inherited members private? C++ allows selective inheritance. Just use the using keyword. Example

#include <iostream>
using namespace std;
class A
{
	public:
    	void f(){}
    	void g();
    	void h();
};
// we want to inhertic h as public, rest as private
class B : private A
{
	public:
    	using A::f; // tagged as public
    	void f()
    	{
        	cout << "B::f called\n";
    	}
};
int main()
{
    B obj;
    obj.f(); // OK
}