

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This c++ code demonstrates inheritance and polymorphism through the use of three classes: base, derived1, and derived2. The base class has constructor and destructor functions, as well as a base_function and increment/decrement virtual functions. Derived1 inherits from base and adds a new function, square. Derived2 further inherits from derived1 and overrides the increment/decrement functions and adds a new function, derv2_function. The main function creates instances of each class and calls their functions.
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!
#include
int decrement(int a) { cout<<"In Derived2 \n"; return --a; } int derv2_function(int a) { cout<<"In Derived2 \n"; return a; } ~Derived2() { cout<<"Derived2 is now destructed \n"; } }; void main() { Base* ptr; //Base Starts ptr = new Base; cout<<ptr->base_function(-5)<<endl; cout<<ptr->increment(3)<<endl; delete ptr; //Base Ends //Derived1 Starts ptr = new Derived1; cout<<ptr->base_function(-5)<<endl; cout<<ptr->increment(2)<<endl; cout<<ptr->decrement(4)<<endl; delete ptr; //Derived1 Ends //Derived2 Start ptr = new Derived2; cout<<ptr->increment(3)<<endl; cout<<ptr->base_function(98)<<endl; cout<<ptr->decrement(54)<<endl; //ptr->derv2_function(3); Not visible delete ptr; //Derived2 Ends Derived1* ptr2; //Derived1 Starts