Unit Iv
Unit Iv
Inheritance
Virtual Functions
C++ matches a function call with the correct function definition at compile time
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
similar to Java interfaces cannot instantiate from abstract classes inherited classes must define implementation
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; } };
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 { ... };
void f(Base *p) { const typeinfo& a = typeid(p); // Type information for Base * const typeinfo& a = typeid(*p); // Actual run time type of *p }
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 cast.it 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 succeeds.The casting from base pointer to derived can be succeed only if base is actually pointing to an object of derived one.
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; }