Skip to content
Question of 97

Q.Observe the following C++ code and answer the questions

(i) and (ii). Note : Assume all necessary files are included. class Point { int X,Y; public: Point(int I=10, int J=20) //Function 1 { X = J; Y = I; } void Show() //Function 2 { cout<< " Points are " << X << " & " << Y <<endl; } ~Point() //Function 3 { cout<<"Points Erased"<<endl; } }; void main() { Point P(5); P.Show(); }
(i) For the class Point, what is Function 3 known as? When is it executed?
(ii) What is the output of the above code, on execution?
(OR)
Explain Polymorphism in context of Object Oriented Programming. Also give a supporting example in C++.
CBSECBSE Class XII Board 2019Subjective· 2mImportance★★★★★est
0% · 0/97 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): Function 3 is the destructor (called when the object is destroyed) and the program prints Points are 20 & 5 then Points Erased. Part (b): polymorphism means one name in many forms, shown with an overloaded C++ function.

Part (a)

(i) ~Point() is the class destructor. A destructor has the class name preceded by ~, takes no arguments and returns nothing. It is invoked automatically when the object is destroyed — for a local object that is when it goes out of scope (end of main() here).

(ii) Tracing void main():

  • Point P(5); calls the constructor with I=5 and J=20 (the default for the second parameter). The constructor sets X = J = 20 and Y = I = 5.
  • P.Show(); prints " Points are " << X << " & " << Y, i.e. Points are 20 & 5.
  • When main() ends, P is destroyed, so ~Point() runs and prints Points Erased.
 Points are 20 & 5
Points Erased …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.