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.