Lesson 18 of 24

Virtual Functions ಮತ್ತು Polymorphism

11 min 35 XP

Base-class pointer ಅಥವಾ reference ಮೂಲಕ virtual function run time ನಲ್ಲಿ ಸರಿಯಾದ derived version ಓಡಿಸುತ್ತದೆ (runtime polymorphism). Pure virtual function (= 0) ಇರುವ class abstract, ಅದರ object ರಚಿಸಲಾಗದು.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Base class ಗೆ virtual destructor ಕೊಡಿ, ಇಲ್ಲದಿದ್ದರೆ base pointer ಮೂಲಕ delete ಮಾಡಿದಾಗ derived ಭಾಗ ಸ್ವಚ್ಛವಾಗದು. vtable ಹೇಗೆ ಕೆಲಸ ಮಾಡುತ್ತದೆ ಎಂದೂ interviewers ಕೇಳುತ್ತಾರೆ.

Challenge

+35 XP

Pure virtual double fee(double amount) const ಇರುವ abstract class Payment, ಮತ್ತು Upi (fee 0), Card (2%) ಹಾಗೂ NetBanking (flat 10) ಬರೆಯಿರಿ. main() ನಲ್ಲಿ amount ಓದಿ ಪ್ರತಿ fee ಅನ್ನು 2 ದಶಮಾಂಶಗಳಲ್ಲಿ print ಮಾಡಿ. Input: 1000 Output: UPI: 0.00 Card: 20.00 NetBanking: 10.00

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.