Virtual Function Implementation in C++ Programming
Question:
Write a base class called Employee. Use this class to store values name, salary etc.
Derive three specific classes called Manager, Teamleadand Programmer. Add to the
base class, a member function get_data() to initialize base class data members and
another function display() to compute and display information . Make display() as a
virtual function and redefine this function in the derived classes to suit their
requirements.
Derive three specific classes called Manager, Teamleadand Programmer. Add to the
base class, a member function get_data() to initialize base class data members and
another function display() to compute and display information . Make display() as a
virtual function and redefine this function in the derived classes to suit their
requirements.
Solutions
#include<iostream> #include<string> using namespace std; class Employee { private: string name; float sal; public: Employee() { name='NULL'; sal=0; } void get_data() { cout<<"\nPlease Enter Employee Name:"<<endl; cin>>name; cout<<"Please Salary of "<<name<<":"<<endl; cin>>sal; } float get_sal() { return sal; } string get_name() { return name; } virtual void display() { } }; class Manager:public Employee { public: void display() { cout<<"\n****Manager Details****"<<endl; cout<<"Manager Name:"<<get_name()<<endl; cout<<"Manager Salary:"<<get_sal()<<endl; } }; class Teamleader:public Employee { public: void display() { cout<<"\n****Teamleader Details****"<<endl; cout<<"Teamleader Name:"<<get_name()<<endl; cout<<"Teamleader Salary:"<<get_sal()<<endl; } }; class Programmer:public Employee { public: void display() { cout<<"\n****Programmer Details****"<<endl; cout<<"Programmer Name:"<<get_name()<<endl; cout<<"Programmer Salary:"<<get_sal()<<endl<<endl; } }; int main() { Manager m; m.get_data(); m.display(); Teamleader t; t.get_data(); t.display(); Programmer p; p.get_data(); p.display(); return 0; }
0 comments:
Post a Comment