Inheritance and type casting
Inheritance
It is a mechanism of creating a new class
from an already defined class The new class contains all attributes of the old class in addition to some of its own attributes. (REFER Ist UNIT PPT FOR INHERITANCE AND POLYMORPHISM CONCEPTS)
Virtual Functions
C++ matches a function call with the correct function definition at compile time
known as static binding
the compiler can match a function call with the correct function definition at run time
known as dynamic binding. declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.
class A { public: virtual void f() { cout << "Class A" << endl; } }; class B: public A { public: void f(int) { cout << "Class B" << endl; } }; class C: public B { public: void f() { cout << "Class C" << endl; } };
Example
Purely Virtual
a virtual function declared with no definition
base class contains no implementation at all
class containing a pure virtual function is an abstract class
similar to Java interfaces cannot instantiate from abstract classes inherited classes must define implementation
enforces a design through inheritance hierarchy
Example
class A { public: virtual void f() = 0; // pure virtual }; class B: public A { public: void f() { cout << "Class B" << endl; } }; class C: public B { public: void f() { cout << "Class C" << endl; } };
Run Time Type Information (RTTI)
Always exists in OOP: a prerequisite for dynamic binding Accessible to programmer?
Not necessarily in statically typed languages
Many things can be done without it!
Almost always in dynamically typed languages
Without it, it is impossible to be sure that an object will recognize a message! In LST, RTTI is the information accessible from the instance_of pointer
RTTI in C++
class typeinfo { public: virtual ~typeinfo(void); bool operator==(const typeinfo&) const; bool operator!=(const typeinfo&) const; bool before(const typeinfo&) const; const char *name(void) const; private: typeinfo(const typeinfo&); typeinfo& operator= (const typeinfo&); //.. Implementation dependent fields }; class Base { ... };
No RTTI in early versions of the language.
No feature should incur a cost if not
void f(Base *p) { const typeinfo& a = typeid(p); // Type information for Base * const typeinfo& a = typeid(*p); // Actual run time type of *p }
Dynamic binding and casting
Dynamic Typing: no constraints on the
};
values stored in a variable. Usually implies reference semantics Runtime type information: dynamic type is associated with the value.
Dynamic casting
Casting operator is for polymorphic object casting ,so that it can cast from one object to another object. Dynamic cast is also called as safe [Link] succeeds only when the pointer or reference being cast is an object of the target type or derived type from it. The syntax is written as dynamic cast<ToobjectptrOr ref>(FromobjectPtrOrRef) If we have a base class and a derived class,casting from derived pointer to base pointer always [Link] casting from base pointer to derived can be succeed only if base is actually pointing to an object of derived one.
Rtti and templates
If we want to test the type of the actual
variable and try to provide validations according to the type we can use RTTI for that.
Cross casting
It refers to casting from derived to proper base class when there are multiple base classes in case of multiple inheritance. The dynamic_cast feature of C++ affords another kind of solution -- cross casting. Consider the following code. class A {public: virtual ~A();}; class B {public: virtual ~B();}; class C : public A, public B {}; A* ap = new C; B* bp = dynamic_cast<B*>(ap); Notice that classes A and B are completely unrelated. Now when we create an instance of C we can safely upcast it to an A* . However, we can now take that pointer to A and cross cast it to a pointer to a B. This works because the A pointer ap really points at a C object; and C derives from B . Thus, we have cast accross the inheritance hierarchy between completely unrelated classes. It should be noted that this will not
Down casting
rectangle::rectangle(float h, float w, int c, int l):pr(c, l) { height = h; width = w; xpos = 0; ypos = 0; };
void main() { rectangle rc(3.0, 2.0, 1, 3); C++ statements; }