Dynamic Binding (Virtual Functions)
Lab Question:
Q1) Write a c++ program to take a number derived 3 classes from it Decimal, Hexadecimal, Octal. Use the concept of virtual function to display the number in various number systems. Include input function in the appropriate class.
Program
#include<iostream>
#include<iomanip>
using namespace std;
class number{
protected:
int val;
public:
void setval(int i)
{
val=i;
}
virtual void show(){}
};
class hextype : public number{
public:
void show() {
cout<<hex<<val<<endl;
}
};
class dectype : public number{
public:
void show() {
cout<<dec<<val<<endl;
}
};
class octtype : public number{
public:
void show() {
cout<<oct<<val<<endl;
}
};
int main()
{
number *ptr;
dectype d;
hextype h;
octtype o;
ptr = &o;
ptr->setval(20);
ptr->show();
ptr=&h;
ptr->setval(20);
ptr->show();
ptr=&d;
ptr->setval(50);
ptr->show();
}
Output
24
14
50
Q2) Create a class media having data members title and price. Derived two classes Book, Tape from it. Book class has number of pages as a data member and Tape class has time to play as data member. Using Virtual function, display the information to a particular type of media.
Program
#include<iostream>
#include<string.h>
using namespace std;
class media
{
protected:
char title[20];
float price;
public:
media(char* t, float p)
{
strcpy(title,t);
price = p;
}
virtual void show()
{
cout<<"Title : "<<title<<endl;
cout<<"Price : "<<price<<endl;
}
};
class book:public media
{
int pages;
public:
book(char* t, float p, int page): media(t,p)
{
pages = page;
}
void show()
{
media::show();
cout<<"No of pages : "<<pages<<endl;
}
};
class tape:public media
{
int time_to_play;
public:
tape(char* t, float p, int ttp): media(t,p)
{
time_to_play = ttp;
}
void show()
{
media::show();
cout<<"Time to play : "<<time_to_play<<" hours"<<endl;
}
};
int main()
{
book b1((char*)"xyz",500.36,200);
tape t1((char*)"abc",320.45,5);
media *m;
m = &b1;
m->show();
m = &t1;
m->show();
}
Output
Title : xyz
Price : 500.36
No of pages : 200
Title : abc
Price : 320.45
Time to play : 5 hours
Q3) Create a class which stores employ name, id and salary. Derived two classes from 'Employee' class: 'Regular' and 'Part-time'. Regular class stores DA, HRA and basic salary the part time class stores the number of hours and pay per hour. Calculate the salary of regular employee and part time employ using virtual function. Display salary.
Program
#include <iostream>
#include <cstring>
using namespace std;
class employee
{
protected:
int id;
char *name;
float salary;
public:
employee(int i, char *n)
{
this->id = i;
this->name = new char[strlen(n) + 1];
strcpy(this->name, n);
this->salary = 0;
}
virtual void calc() = 0;
virtual void display()
{
cout << "\nID: " << id << "\nName: " << name << endl;
}
~employee()
{
cout << "\nDestructor called!!";
delete name;
}
};
class regular : public employee
{
protected:
double basic, da, hra;
public:
regular(int i, char *name, double b) : employee(i, name)
{
this->basic = b;
da = 80 * basic / 100;
hra = 20 * basic / 100;
}
void calc()
{
salary = basic + da + hra;
}
void display()
{
employee::display();
cout << "\nBasic: " << basic << "\nSalary: " << salary << endl;
}
};
class part_time : public employee
{
protected:
double hours, pay_per_hr;
public:
part_time(int i, char *name, double hrs, double pay) : employee(i, name)
{
this->hours = hrs;
this->pay_per_hr = pay;
}
void calc()
{
salary = hours * pay_per_hr;
}
void display()
{
employee::display();
cout << "\nSalary: " << hours << " * " << pay_per_hr << " = " << salary << endl;
}
};
int main()
{
regular r1(101, (char *)"abc", 45000.500);
part_time p1(102, (char *)"xyz", 7.5, 100.00);
employee *ptr[] = {&r1, &p1};
ptr[0]->calc();
ptr[0]->display();
ptr[1]->calc();
ptr[1]->display();
return 0;
}
Output
ID: 101
Name: abc
Basic: 45000.5
Salary: 90001
ID: 102
Name: xyz
Salary: 7.5 * 100 = 750
Destructor called!!
Destructor called!!
Tags:
C Plus