Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: • encapsulation - a language mechanism for restricting access to some of the object's components. • inheritance • polymorphism which can improve the design, structure and reusability of code. Inheritance • Allows a new class to be based on an existing class. • The new class inherits all the member variables and functions (except constructors and destructors) Inheritance C++ class hierarchy is based upon the principle of increased specialization. • The base class carries attributes that are common to all classes and virtual functions that may or may not be overridden. • Attributes that are esoteric to a particular entity plane/normal or sphere/radius are not defined • in the base class • The derived class inherits the attributes of classes above it in the class hierarchy • The specialization can continue over multiple levels Inheritance Protection attributes: Base class member access specifiers • public - Any holder of a pointer to an instance of the class may invoke any public method or modify any public data item. • private – methods/data members may be accessed only by methods belonging to the class • protected - methods/data members may be access by derived classes but not others Inheritance Base class access specifier public protected private Type of inheritance public protected public in derived class. protected in derived class. Can be accessed directly by member functions, friend functions, and nonmember functions. Can be accessed directly by member functions and friend functions. protected in derived class. protected in derived class. Can be accessed directly by member functions and friend functions. Can be accessed directly by member functions and friend functions. Hidden in derived class. Hidden in derived class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. private private in derived class. Can be accessed directly by member functions and friend functions. private in derived class. Can be accessed directly by member functions and friend functions. Hidden in derived class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. Late Binding / Dynamic Binding • technique of waiting until runtime to determine the implementation of a function. • The decision of which version of a member function is appropriate is decided at runtime. • Polymorphism is another word for late binding. Polymorphism • Enables us to write programs that process objects of classes that are part of the same class hierarchy as if they were all objects of the hierarchy’s base class. Virtual Functions (C++ only) • By default, C++ matches a function call with the correct function definition at compile time. This is called static binding. • You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding. • You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function. Virtual Functions (C++ only) • The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference, but by the type of object that the reference refers to. • Therefore, a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object. Virtual Functions (C++ only) • A class that declares or inherits a virtual function is called a polymorphic class. • You redefine a virtual member function, like any member function, in any derived class.