C++ Inheritance and Polymorphism: Base, Derived1, and Derived2 Classes, Exercises of Object Oriented Programming

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

2011/2012

Uploaded on 07/31/2012

netu
netu 🇮🇳

4.5

(4)

50 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{ cout<<"Base is created \n"; }
~Base()
{ cout<<"Base is destructed \n"; }
int base_function (int a)
{
cout<<"In base \n";
return a;
}
virtual int increment(int a) { return ++a; }
virtual int decrement(int a) { return --a; }
};
class Derived1: public Base
{
public:
Derived1()
{ cout<<"Derived1 is now created \n"; }
int increment(int a)
{
cout<<"In Derived1 \n";
return ++a;
}
int square(int a)
{
cout<<"In Derived1 \n";
return a*a;
}
~Derived1()
{ cout<<"Derived1 is now destructed \n"; }
};
class Derived2: public Derived1
{
public:
Derived2()
{ cout<<"Derived2 is now created \n"; }
virtual int increment(int a)
{
cout<<"In Derived2 \n";
return ++a;
}
docsity.com
pf3

Partial preview of the text

Download C++ Inheritance and Polymorphism: Base, Derived1, and Derived2 Classes and more Exercises Object Oriented Programming in PDF only on Docsity!

#include using namespace std; class Base { public: Base() { cout<<"Base is created \n"; } ~Base() { cout<<"Base is destructed \n"; } int base_function (int a) { cout<<"In base \n"; return a; } virtual int increment(int a) { return ++a; } virtual int decrement(int a) { return --a; } }; class Derived1: public Base { public: Derived1() { cout<<"Derived1 is now created \n"; } int increment(int a) { cout<<"In Derived1 \n"; return ++a; } int square(int a) { cout<<"In Derived1 \n"; return a*a; } ~Derived1() { cout<<"Derived1 is now destructed \n"; } }; class Derived2: public Derived { public: Derived2() { cout<<"Derived2 is now created \n"; } virtual int increment(int a) { cout<<"In Derived2 \n"; return ++a; }

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