Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program should be a in C++ This week\'s lesson is focused on Polymorphism, f

ID: 3822233 • Letter: T

Question

The program should be a in C++

This week's lesson is focused on Polymorphism, for which you will create classes using inheritance and virtual functions. To start with, what is polymorphism? Polymorphism allows the name of a function to invoke a response on a base-class-type object and a different one in objects of a derived class, for which virtual functions are used. 1.1 Exercise 1 This exercise deals with using virtual functions, seeing how they act according to the object on which it is applied, which you will see in mai Create a file lab12pl.cpp and enter the following code class Employee protected double sal //salary base public Employ double s){ sal sp) double Payment return sal:) void prt() cout Salary Payment

Explanation / Answer

Exercise 1:

Virtual Function:

The Virtual function can be defined as the member function of a class that can be overridden in its derived class. A virtual function can be declared with virtual keyword. A virtual function call is resolved at run-time (dynamic binding) whereas the non-virtual member functions are resolved at compile time (static binding).

Program:



#include <iostream>
#include <string>
using namespace std;
class Employee
{
protected:double sal;
public:
Employee(double s)
{
sal=s;
}
virtual double payment(){
return sal;
}
void prt()
{
cout<<"Salary is"<<" "<<payment();
}
};
class Manager : public Employee
{
double inc;
public:Manager(double s,double i):Employee(s)
{
inc=i;
  
}
double payment()
{
return sal*inc;
}
};
int main()
{
Employee el(1500);
Manager ml(1500,1.5);
cout << "Exercise about inhertiance and ploymorphism " << "! ";
el.prt();
cout<<" ";
ml.prt();
return 0;
}

Output:

Exercise about inhertiance and ploymorphism !

Salary is 1500

Salary is 2250