Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 90

Inheritance, Polymorphism, and Virtual

Functions
(Reusability)
What Is Inheritance?
• Provides a way to create a new class from an existing class
• The new class is a specialized version of the existing class
• It makes it easier to create and maintain an application.
• It provides an opportunity to reuse the code functionality and fast
implementation time.
• Syntax:

class derived-class_name : visivility-mode base-


class_name
{ . . . . // members of the derived class . . . . };
Example: Insect Taxonomy
The "is a" Relationship
• Inheritance establishes an "is a" relationship between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete
Inheritance – Terminology and Notation in C++
• Base class (or parent) – inherited from
• Derived class (or child) – inherits from the base class
• Notation:
class Student // base class
{
...
};
class UnderGrad : public student
{ // derived class
...
};
Example: void display_p()
{
#include<iostream> cout<<endl<<id<<"\t"<<name;
using namespace std; } };
class Person {
class Student: private Person
int id; {
char name[100]; char course[50];
public: int fee;
public:
void set_p() { void set_s()
cout<<"Enter the Id:"; {
cin>>id; set_p();
cout<<"Enter the Course Name:";
fflush(stdin);
fflush(stdin);
//or cin.ignore(); cin.getline(course,50);
cout<<"Enter the Name:"; cout<<"Enter the Course Fee:";
cin.get(name,100); cin>>fee;
}
}
Example
void display_s()
{
display_p();
cout<<"t"<<course<<"\t"<<fee;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
What Does a Child Have?
An object of the derived
class has:
• all members defined
in child class
• all members declared
in parent class

An object of the derived


class can use:
• all public members
defined in child class
• all public members
defined in parent
class
Protected Members and Class Access
• protected member access specification: like private,
but accessible by objects of derived class
• Class access specification: determines how private,
protected, and public members of base class are
inherited by the derived class
Class Access Specifiers
1) public – object of derived class can be treated as
object of base class (not vice-versa)
2) protected – more restrictive than public, but allows
derived classes to know details of parents
3) private – prevents objects of derived class from
being treated as objects of base class.
Public, Private, and Protected Inheritance
Inheritance vs. Access
How inherited base class
members
Base class members appear in derived class
private: x private x is inaccessible
protected: y base class
private: y
public: z private: z

private: x protecte x is inaccessible


protected: y d protected: y
base class
public: z protected: z

private: x public x is inaccessible


protected: y base class protected: y
public: z public: z
Inheritance vs. Access Public
class Grade class Test : public Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
public class access, it public members:
Test(int, int);
looks like this: void setScore(float);
float getScore();
char getLetter();
Inheritance vs. Access Protected
class Grade class Test : protected Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
protected class access, it public members:
Test(int, int);
looks like this: protected members:
void setScore(float);
float getScore();
float getLetter();
Inheritance vs. Access Private
class Grade class Test : private Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
private class access, it void setScore(float);
float getScore();
looks like this: float getLetter();
public members:
Test(int, int);
void disp_A()
Example 2 {
cout<<endl<<"Value of A="<<a;
}
#include<iostream>
};
using namespace std;
class A class B: public A
{
{ int b, p;
protected:
int a; public:
void set_B()
public: {
void set_A() set_A();
cout<<"Enter the Value of B=";
{ cin>>b;
cout<<"Enter the Value of }
A="; void disp_B()
{
cin>>a; disp_A();
} cout<<endl<<"Value of B="<<b;
}
Example 2
void cal_product()
{
p=a*b;
cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;
}
};

int main()
OUTPUT:
{ Enter the Value of A=2
B b; Enter the Value of B=10

b.set_B(); Product of 2 * 10 = 20
b.cal_product();
return 0;
}
Forms of Inheritance
•Single Inheritance
•Multiple Inheritance
•Hierarchical Inheritance
•Multilevel Inheritance
•Hybrid Inheritance (also known as Virtual Inheritance)
Multilevel Inheritance
#include <iostream>
using namespace std;
class base //single base class
{
protected:
int x;
public:
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
Multilevel Inheritance
class derive1 : public base // derived class from base class
{
protected:
int y;
public:
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
Multilevel Inheritance
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
} };
Multilevel Inheritance
int main( )
OUTPUT:
{
Enter value of x= 2
derive2 a; //object of derived class
Enter value of y= 4
a.getdata();
a.readdata(); Enter value of z= 5
a.indata(); Product= 40
a.product();
return 0;
}
Hierarchical Inheritance
#include <iostream>
using namespace std;
class A // Base class
{
Protected:
int x, y; // data members
public:
void getdata() // to input x and y
{
cout<< "Enter value of x and y:\n";
cin>> x >> y;
}
};
Hierarchical Inheritance
class B : public A //B is derived from class base
{
public:
void product()
{
cout<< "\nProduct= " << x * y <<endl;
} };
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout<< "\nSum= " << x + y; // Perform sum
} };
Hierarchical Inheritance
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata(); OUTPUT:
Enter value of x and y:
obj1.product(); 24
obj2.getdata();
Product= 8
obj2.sum(); Enter value of x and y:
24
return 0;
} Sum= 6
Multiple Inheritance
#include <iostream>
using namespace std;
class stud {
protected:
int roll, m1, m2;
public:
void get()
{
cout << "Enter the Roll No.: ";
cin >> roll;
cout << "Enter the two highest marks: ";
cin >> m1 >> m2;
}
};
Multiple Inheritance
class extracurriculam {
protected:
int xm;

public:
void getsm()
{
cout << "\nEnter the mark for Extra Curriculam Activities:
"; cin >> xm;
}
};
Multiple Inheritance
class output : public stud, public extracurriculam {
int tot, avg;
public:
void display()
{
tot = (m1 + m2 + xm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << roll << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
} };
int main() OUTPUT:
{ Enter the Roll No.: 2
output O; Enter the two highest marks: 34 56
O.get();
Enter the mark for Extra Curriculam Activities: 5
O.getsm();
O.display(); } Roll No : 2
Total : 95
Average : 31
MultPath Inheritance
#include <iostream>
using namespace std;
class person {
protected:
char name[100];
int code;
public:
void input()
{
cout<<"\nEnter the name of the person : ";
cin>>name;
cout<<endl<<"Enter the code of the person : ";
cin>>code; }
void display()
{
cout<<endl<<"Name of the person : "<<name;
cout<<endl<<"Code of the person : "<<code; } };
MultiPath Inheritance
class account:virtual public person
{
protected:
float pay;
public:
void getpay()
{
cout<<endl<<"Enter the pay : ";
cin>>pay;
}
void display()
{
cout<<endl<<"Pay : "<<pay; } };
MultiPath Inheritance
class admin:virtual public person
{
Protected:
int experience;
public:
void getexp()
{
cout<<endl<<"Enter the experience : ";
cin>>experience;
}
void display()
{
cout<<endl<<"Experience : "<<experience; } };
MultiPath Inheritance
class master: public account, public admin
{

char n[100];
public:
void gettotal()
{
cout<<endl<<"Enter the company name : ";
cin>>n;
}
void display()
{
cout<<endl<<"Company name : "<<n;
}
};
MultiPath Inheritance
int main() OUTPUT:
{ Enter the name of the person : PPPP

master m1; Enter the code of the person : 09


m1.input(); Enter the pay : 80000
m1.getpay();
Enter the experience : 15
m1.getexp();
m1.gettotal(); Enter the company name : UMIT

m1.person::display(); Name of the person : PPPP


m1.account::display(); Code of the person : 9
Pay : 80000
m1.admin::display(); Experience : 15
m1.display(); Company name : UMIT

return 0;
}
What to inherit?

• In principle, every member of a base class is


inherited by a derived class
– just with different access permission

• However, there are exceptions for


– constructor and destructor
– operator=() member
– friends
Since all these functions are class-specific
CS1 -- Inheritance and Polymorphism 34
Constructors and Destructors in Base and
Derived Classes
• Derived classes can have their own constructors and
destructors
• When an object of a derived class is created, the base
class’s constructor is executed first, followed by the
derived class’s constructor
• When an object of a derived class is destroyed, its
destructor is called first, then that of the base class
Constructor Rules for Derived Classes
The default constructor and the destructor of the
base class are always called when a new object
of a derived class is created or destroyed.

class A { class B : public A


public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
{cout<<“A:parameter”<<endl;} };
};

output: A:default
B test(1); B
Constructors and Destructors in Base and
Derived Classes
#include<iostream>
using namespace std;
class BC
{
public:
BC()
{
cout<<"Base Class Constructor"<<endl;
}
~BC()
{
cout<<"Base Class Distructor"<<endl;
}
};
Constructors and Destructors in Base and
Derived Classes
class Derv:public BC
{
public:
Derv()
{
cout<<"Derived Class Constructor"<<endl;
}
~Derv()
{
cout<<"Derived Class Distructor"<<endl;
} };
int main()
{
Derv ob1;
cout<<"Program is ending"<<endl;
return 0;
}
Passing Arguments to Base Class Constructor

• Allows selection between multiple base class


constructors
• Specify arguments to base constructor on derived
constructor heading:
Square::Square(int side): Rectangle(side, side)
• Can also be done with inline constructors
• Must be done if base class has no default
constructor
Constructor Rules for Derived Classes
You can also specify an constructor of the
base class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass args )

{ DerivedClass constructor body }

class A { class C : public A {


public: public:
A() C (int a) : A(a)
{cout<< “A:default”<<endl;} {cout<<“C”<<endl;}
A (int a) };
{cout<<“A:parameter”<<endl;}
};
output: A:parameter
C test(1); C
Passing Arguments to
Base Class Constructor

derived class constructor base class constructor

Square::Square(int side):Rectangle(side,side)

derived constructor base constructor


parameter parameters
Example:
// C++ program to show how to call parameterized Constructor
// of base class when derived class's Constructor is called
#include <iostream>
using namespace std;
class Parent {
int x;
public:
Parent(int i)
{
x = i;
cout << "Inside base class's parameterized "
"constructor“ << endl; } };
Example:
class Child : public Parent {
OUTPUT:
public: Inside base class's
Child(int x): Parent(x) parameterized constructor
{ Inside sub class's
cout << "Inside sub class's parameterized constructor
parameterized
constructor“ << endl;
}
};
int main()
{
Child obj1(10);
return 0; }
Constructor in Multiple Inheritance in C++
// C++ program to show the order of constructor calls in Multiple
Inheritance
#include <iostream>
using namespace std;
// first base class
class Parent1
{
public:
// first base class's Constructor
Parent1()
{
cout << "Inside first base class" << endl;
}};
Constructor in Multiple Inheritance in C++
// second base class
class Parent2
{
public:

// second base class's Constructor


Parent2()
{
cout << "Inside second base class" << endl;
}
};
Constructor in Multiple Inheritance in C++
int main() {
// child class inherits Parent1 and Parent2
class Child : public Parent2, public Parent1 Child obj1;
return 0;
{
}
public:

// child class's Constructor


Child()
{ OUTPUT:
Inside second base class
cout << "Inside child class" << endl;
Inside first base class
} Inside child class
};
Constructor in Multiple Inheritance :Virtual
#include<iostream>
using namespace std;
class A1
{
public:
A1()
{
cout << "Constructor of the base class A1 \n";
} };
class A2
{
public:
Constructor in Multiple Inheritance :Virtual
A2() // Driver code
{ int main()
{
cout << "Constructor of the base class A2 \n"; S obj;
} return 0;
}
};

class S: public A1, virtual A2


{ OUTPUT:
Constructor of the base class A2
public: Constructor of the base class A1
S(): A1(), A2() Constructor of the derived class S
{
cout << "Constructor of the derived class S \
n";
}
};
Order of Constructor Invocation in Derived Class

CS1 -- Inheritance and Polymorphism


Object Composition and Delegation
#include <iostream>
using namespace std;
class A {
public:
int x;
// Constructor initializing the data members
A() { x = 0; }
A(int a)
{
cout << "Constructor A(int a) is invoked" << endl;
x = a;
} };
Object Composition and Delegation
class B {
int data;
A objA;
public:
B(int a) : objA(a)
{
data = a; }
void display()
{
cout << "Data in object of class B = " << data << endl;
cout << "Data in member object of “ << "class A & B = " <<
objA.x;
} };
Object Composition and Delegation
// Driver code OUTPUT:
Constructor A(int a) is invoked
int main() Data in object of class B = 25
{ Data in member object of class A
in class B = 25
// Creating object of class B
B objb(25);

// Invoking display function


objb.display();
return 0;
}
Object Composition and Delegation
#include <iostream>
using namespace std;
class First {
public:
void print() { cout << "The Delegate"; }
};
class Second {
// Creating instance of the class
First ob;
public:
void print() { ob.print(); }
};
Object Composition and Delegation
// Driver Code OUTPUT:
The Delegate
int main()
{
Second ob1;
ob1.print();
return 0;
}
Virtual Function
Problem with Redefining Function in Child
Class
BaseClass

void X(); Object D invokes function X()


void Y(); In BaseClass in BaseClass,
not function Y() in DerivedClass,
DerivedClass because function calls are bound at
compile time.
This is static binding.

void Y();

DerivedClass D;
D.X();
Late Binding
#include<iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
} };
Late Binding
class derived : public base {
public:
void print()
{
cout << "print derived class\n";
}

void show()
{
cout << "show derived class\n";
}
};
Late Binding
int main()
OUTPUT:
{ print derived class
show base class
base *bptr;
derived d;
bptr = &d;

// Virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
return 0;
}
Rules for Virtual Functions
1. Virtual functions cannot be static.
2. A virtual function can be a friend function of another class.
3. Virtual functions should be accessed using pointer or reference
of base class type to achieve runtime polymorphism.
4. The prototype of virtual functions should be the same in the
base as well as derived class.
5. They are always defined in the base class and overridden in a
derived class. It is not mandatory for the derived class to
override (or re-define the virtual function), in that case, the
base class version of the function is used.
6. A class may have virtual destructor but it cannot have a virtual
constructor.
Pure Virtual Function

#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
Pure Virtual Function
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
Abstract Class with Constructor
#include<iostream>
using namespace std;
// An abstract class with constructor
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i) {
x = i;
cout<<"Constructor of base called\n";
} };
Abstract Class with Constructor

class Derived: public Base


{
int y;
public:
Derived(int i, int j):Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " << y<<'\n'; }
};
Abstract Class with Constructor
OUTPUT:
int main(void) Constructor of base
called
{ x = 4, y = 5
Derived d(4, 5); Constructor of base
called
d.fun(); x = 6, y = 7

//object creation using pointer of base class


Base *ptr=new Derived(6,7);
ptr->fun();
return 0;
}
Virtual Destructors
#include <iostream>
class derived : public base {
using namespace std; public:
class base { derived()
public: {
base() cout << "Constructing
{ cout << "Constructing base\n"; derived\n"; }
} virtual ~derived()
{
virtual ~base()
cout << "Destructing
{ cout << "Destructing base\ derived\n"; }
n"; } };
};
Virtual Destructors
int main() OUTPUT:
Constructing base
{ Constructing derived
Destructing derived
derived *d = new derived(); Destructing base
base *b = d;
delete b;
return 0;
}
Generic Programming
Class and Function Templates
An abstract recipe for producing concrete code.

Reference:
https://techvidvan.com/tutorials/cpp-templates/
Function Overloading

function overloading allows the same function to act on different argument


types:

int max(int a, int b) {


if (a > b) return a;
else return b;
}
float max(float x, float y) {
if (x > y) return x;
else return y;
}
This is useful, but we are duplicating code!
Function Template
Templates let us write the two functions with no duplication:
Type parameter
template <class T>
T max(T x, T y) { The use of the word class here means
if (x > y) return x; “any type”
else return y; Template parameters are place
} holders for types and classes.
The compiler generates code for us when it comes across statements
such as:

For each call, the compiler generates the


i = max(j, k); complete function, replacing the type
parameter with the type or class to
which the arguments belong.
70
Function Template
#include <iostream>
using namespace std;
template <class T>
T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.5, 7.7) << endl; // call myMax
double
cout << myMax<char>('g', 'e’) << endl; // call myMax for char
return 0; }
Function Template

It also works for user defined types:


Date d1,d2,d3;
d3 = max(d1,d2);
Provided that as we have an operator> for the Date class.

Function templates can themselves be overloaded:


We can now compare an object with any other
object as long as > is defined.
template <class T, class S>
T max(T x, S y) { In fact this version can replace the previous one
if (x > y) return x; (it is the special case when S = T)
else return y;
}
Example:2
#include <iostream>
using namespace std;
template<class A,class B>
void func(A x,B y)
{ OUTPUT:
cout << "Value->x: " <<x<< endl; Value->x: 15.2
cout << "Value->y: " <<y<< endl; Value->y: 1.3
} Value->x: 4.4
Value->y: 8
int main() Value->x: c
{ Value->y: 8
func(15.2,1.3); Value->x: prachi
func(4.4,8); Value->y: 8
func('c',8);
func("prachi",8);
return 0;
}
Example:3
#include <iostream>
using namespace std;
template<class A> void func(A x)
{
cout << "Value->x: " <<x<< endl;
}
template<class A,class B> void func(A y ,B z)
{
cout << "Value->y: " <<y<< endl;
cout << "Value->z: " <<z<< endl;
}
int main()
{
func(20);
func(2,2.1);
return 0; }
Function Template
• Function templates are a direct generalization of function
overloading.

• Function templates share source code among structurally


similar families of functions.

• Templates can also be used with classes.

• We can create generic classes that handle different types


of data.
Class Templates
Like function templates, a class template may have several type parameters.
Moreover, some of them may be ordinary non-type parameters.

Non-type parameter
template <class T, int n, class U, class V>

• Templates are instantiated at compile time, and so, values passed


to non-type parameters must be constants.
• Class templates are sometimes called parameterized types.
• Instantiating templates is one of its major features of c++ and
one that distinguishes it from most other programming
languages.
• As a mechanism for code generation, it allows for substantial
improvements in programming efficiency.
Class Template Ex.1
#include <iostream>
using namespace std;
template <typename T>
class Array {
private:
T* ptr;
int size;
public:
Array(T arr[], int s);
void print();
};
Class Template
template <typename T>
Array<T>::Array(T arr[], int s)
{
ptr = new T[s];
size = s;
for (int i = 0; i < size; i++)
ptr[i] = arr[i];
}
template <typename T>
void Array<T>::print()
{
for (int i = 0; i < size; i++)
cout << " " << *(ptr + i); cout << endl; }
Class Template
int main() OUTPUT:
{ 12345
1.1 2.3 3.4 4.5 6.7
int arr[5] = { 1, 2, 3, 4, 5 };
Array<int> a(arr, 5);
a.print();
float afl[5]= {1.1, 2.3, 3.4, 4.5, 6.7};
Array<float> a1(afl, 5);
a1.print();
return 0;
}
Class Templates with Non Type
Parameter
template<class T, int n>
class X {}; Non-type parameter

int main()
{
X<float, 22> x1; //OK
const int n=44;
X<char, n> x2; //OK
int m=66;
X<short, m> x3; //error! – m must be a constant
}
Example Addition:
#include <iostream>
using namespace std; int main()
template<class T> {
class Add Add<int> data;
{ data.addition();
return 0;
T n1 = 6; }
T n2 = 1;
public:
void addition()
{
cout << "n1+n2: " << n1+n2<<endl;
}

};
Class Templates and Member Function
Instantiating Class templates

Member functions of a class template are themselves function


templates with the same template header as their class.

template<class T>
class X{ Member function of a
class Template
T square(T t) { return t*t; }
};
The square function in this example would be handled in the
same way.

template<class T> Template function


T square(T t) { return t*t; }
Class Templates Working
Consider the class declaration:
X<short> x;
It is instantiated by the compiler, replacing the template parameter T with the type
passed to it.

Therefore, the compiler generates the following class and object

Class X_short{
short square(short t) {return t*t;}
};
* Except that your compiler might
assign a different name for the class
X_short x;
other than X_short.
Class Templates Example:
For the Vector class - we may want Vectors of integer, float, Date, etc. Without
templates, we have to create different vectors for each of them, with templates we
can create a single generic vector:

template <class T>


class Vector {
public:
Vector(int n=100); //constructor
T& operator[](int i); //range checked!
Vector<T>& operator=(Vector<T> &v);
private:
T* p;
int size;
};
Class Templates
Constructor:

template <class T>


Vector<T>::Vector(int n=100) : size(n) {
assert(n>0);
p = new T[size];
assert(p != NULL);
}

operator[ ]

template <class T>


T& Vector<T>::operator[](int i) {
assert((i>=0) && (i<size));
return p[i];
}
Class Templates

operator =

template <class T>


Vector<T> & Vector<T>::operator=(Vector<T> &v) {
assert(size == v.size);
for (int i=0; i<size; i++) {
p[i] = v.p[i];
}
return *this;
}
Example Calculator:
#include<iostream>
using namespace std;
template <class Temp>
class Calculator
{
private:
Temp n1, n2;

public:
Calculator(Temp num1, Temp num2)
{
n1 = num1;
n2 = num2;
}
Example Calculator:
void show()
{
cout << "Addition : " << n1 << "+" << n2 << "= " << addition() << endl;
cout << "Subtraction: " <<n1 << "-" << n2 << "= " << subtraction() <<
endl;
cout << "Product: " << n1 << "*" << n2 << "= " << multiplication() <<
endl;
cout << "Division: " << n1 << "/" << n2 << "= " << division() << endl;
}
Temp addition() { return (n1 + n2); }

Temp subtraction() { return n1 - n2; }

Temp multiplication() { return n1 * n2; }

Temp division() { return n1 / n2; } };


Example Calculator:
int main()
{
Calculator<int> Calc1(25, 12);
Calculator<float> Calc2(13.6, 5.9);
cout << "Integer results for 25 and 12:" << endl;
Calc1.show();
cout << endl << "Float results for 13.6 and 5.9:" << endl;
Calc2.show(); OUTPUT:
return 0; Integer results for 25 and 12:
Addition is: 25+12= 37
} Subtraction is: 25-12= 13
Product is: 25*12= 300
Division is: 25/12= 2

Float results for 13.6 and 5.9:


Addition is: 13.6+5.9= 19.5
Subtraction is: 13.6-5.9= 7.7
Product is: 13.6*5.9= 80.24
Division is: 13.6/5.9= 2.30508
References
• https://www.simplilearn.com/tutorials/cpp-tutorial/types-of-in
heritance-in-cpp
• https://www.w3schools.in/cplusplus/inheritance
• https://www.geeksforgeeks.org/constructor-in-multiple-inherit
ance-in-cpp/
• https://www.geeksforgeeks.org/object-composition-
delegation-in-c-with-examples/

You might also like