Polymorphism
Task 1: https://www.educative.io/courses/learn-cpp-from-scratch Complete the following exercises from the above link: Polymorphism & Virtual Functions
Task 2: ● C++ Program to demonstrate Run time polymorphism
#include <iostream>
using namespace std;
class practice
{
public:
void display()
{
cout << "Called from the base class \\n";
}
};
class practice1 : public practice
{
public:
void display()
{
cout << "Called from the derived class \\n";
}
};
int main()
{
practice p;
practice1 p1;
p.display();
p1.display();
}
● C++ Program to illustrate the use of pure virtual function in Polymorphism
#include <iostream>
using namespace std;
class practice
{
public:
virtual void display()
{
cout << "Display using virtual in parent class \\n";
}
void print()
{
cout << "Print in parent class \\n";
}
};
class practice1 : public practice
{
public:
void display()
{
cout << "Display in child class \\n";
}
void print()
{
cout << "Print in child class \\n";
}
};
int main()
{
practice *p;
practice1 p1;
p = &p1;
p->display();
p->print();
}
Task 3:
● C++ Program to define an Abstract Class in Polymorphism
#include <iostream>
using namespace std;
class EmpDB
{
public:
string name;
char code;
float pay;
int exp;
void getdata()
{
cout << "\\n=======GETDATA IN=======";
cout << "\\nEnter name :- ";
cin >> name;
cout << "\\nEnter code :- ";
cin >> code;
cout << "\\nEnter Pay amount :- ";
cin >> pay;
cout << "\\nEnter experience in yrs :- ";
cin >> exp;
cout << "\\n\\n";
}
void display()
{
cout << "\\n=======DISPLAY DETAILS=======";
cout << "\\nNAME :- " << name;
cout << "\\nCODE :- " << code;
cout << "\\nPAY :- " << pay;
cout << "\\nEXPERIENCE :- " << exp;
cout << "\\n\\n";
}
void update(string inp_name)
{
name = inp_name;
}
void update(char inp_code)
{
code = inp_code;
}
void update(float inp_pay)
{
pay = inp_pay;
}
void update(int inp_exp)
{
exp = inp_exp;
}
};
int main()
{
EmpDB emp1;
int cont = 1, choice;
while (cont)
{
cout << "\\n=======EMPLOYEE DATABASE=======";
cout << "\\n\\nChoose Operation that you want to perform";
cout << "\\n1 Create Record\\n2 Display Record\\n3 Update Record\\n4 Exit";
cout << "\\nEnter your choice ";
cin >> choice;
switch (choice)
{
case 1:
emp1.getdata();
break;
case 2:
emp1.display();
break;
case 3:
int upd_choice;
cout << "\\n=======UPDATE DETAILS=======";
cout << "\\nChoose detail you want to update";
cout << "\\n1) NAME\\n2) CODE\\n3) PAY\\n4) EXPERIENCE";
cout << "\\nEnter your choice :- ";
cin >> upd_choice;
if (upd_choice == 1)
{
string n1;
cout << "\\n\\nEnter your name :- ";
cin >> n1;
emp1.update(n1);
}
else if (upd_choice == 2)
{
char c1[2];
cout << "\\n\\nEnter your code :- ";
cin >> c1;
emp1.update(c1);
}
else if (upd_choice == 3)
{
float p1;
cout << "\\n\\nEnter your pay :- ";
cin >> p1;
emp1.update(p1);
}
else if (upd_choice == 4)
{
int e1;
cout << "\\n\\nEnter your experience :- ";
cin >> e1;
emp1.update(e1);
}
break;
case 4:
cont = 0;
}
}
}
● C++ Program to illustrates the use of Virtual base class
#include <iostream>
using namespace std;
class shape
{
public:
int width;
int height;
int base;
virtual void area() = 0;
};
class rect : public shape
{
public:
void area()
{
cout << "\\nArea of rectangle is " << width * height;
};
};
class tri : public shape
{
public:
void area()
{
cout << "\\nArea of triangle is " << (height * base) / 2;
};
};
int main()
{
rect r1;
cout << "\\nEnter the length and width of rectangle :- ";
cin >> r1.width >> r1.height;
r1.area();
tri t1;
cout << "\\nEnter the height and base of triangle :- ";
cin >> t1.base >> t1.height;
t1.area();
}