lecture-17 - C+ Inheritance and Polymorphism Part - 2...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Object Oriented Programming C++ Inheritance and Polymorphism Part - 2 R.B. Lenin Winter 2006 DA-IICT C++ Inheritance and Polymorphism Part - 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Object Oriented Programming C++ Inheritance and Polymorphism Part - 2 R.B. Lenin Winter 2006 DA-IICT • Polymorphism is supported by C++ both at compile time and run time – Compile-time polymorphism is achieved by overloading functions and operators – Run-time polymorphism is accomplished by using inheritance and virtual functions
Background image of page 2
3 Object Oriented Programming C++ Inheritance and Polymorphism Part - 2 R.B. Lenin Winter 2006 DA-IICT Pointers to Derived Types • A pointer of one object cannot point to an object of another class • But base class pointers and derived objects are exceptions to this rule B B_ob, *p; //B is the base class D D_ob; //D is the derived class p = &B_ob; //valid statement p = &D_ob; //valid statement
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Object Oriented Programming C++ Inheritance and Polymorphism Part - 2 R.B. Lenin Winter 2006 DA-IICT Pointers to Derived Types • A base pointer can be used to – access only those parts of a derived object that were inherited from the base class. • Elements specific to D_ob cannot be accessed through p , unless a type cast is employed. – point to a derived object, the reverse is not true • The above flexibility in pointers is crucial to the way C++ implements runtime polymorphism
Background image of page 4
5 Object Oriented Programming C++ Inheritance and Polymorphism Part - 2 R.B. Lenin Winter 2006 DA-IICT Virtual Functions •A virtual function is a function that is declared as virtual in a base class and redefined in one or more derived classes. • To create a virtual function, precede the function’s declaration in the base class with the keyword virtual .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Object Oriented Programming C++ Inheritance and Polymorphism Part - 2 R.B. Lenin Winter 2006 DA-IICT Virtual Functions • When a class containing a virtual function is inherited, the derived class redefines the virtual function to fit its own needs. • When accessed normally, virtual functions
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/15/2010 for the course CSE IT325 taught by Professor Maniklaldas during the Spring '09 term at Punjab Engineering College.

Page1 / 31

lecture-17 - C+ Inheritance and Polymorphism Part - 2...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online