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.