Enter password:
Derive a class Programmer from Employee. Supply a constructor Programmer(string name, double salary) that calls the base-class constructor. Supply a function get_name that returns the name in the format "Hacker, Harry (Programmer)".
#include < iostream > #include < string > using namespace std; class Employee { public: Employee(); Employee(string pname, double psalary); protected: // Inherited classes can use these member variables string name; double salary; }; Employee::Employee():name(""), salary(0) {} Employee::Employee(string pname, double psalary):name(pname), salary(psalary) {} class Programmer: public Employee { pulbic: Programmer(); Programmer(string pname, double psalary); string get_name() const; }; Programmer::Programmer(): Employee() {} Programmer::Programmer(string pname, double psalary): Employee() {} string Programmer::get_name() const { string new_name = name + " (Programmer)"; return new_name; } int main() { Programmer A("John", 100); cout << A.get_name() << endl; }
Implement a base class Person. Derive classes Student and Instructor from Person. A person has a name and a birthday. A student has a major, and an instructor has a salary. Write the class deļ¬nitions, the constructors, and the member functions print() for all classes.
#include < iostream > #include < string > using namespace std; class Person { public: Person(); Person(string pname, string pdob); void print() const; protected: string name; string dob; }; Person::Person(){ name = "default"; dob = "00/0/0/000"; } Person::Person(string pname, string pdob): name(pname), dob(pdob) {} void Person::print() const{ cout << "name : " << name << " dob : " << dob << endl; } class Student: public Person { public: Student(); Student(string pname, string pdob, string pmajor); void print() const; private: string major; }; Student::Student():Person(), major("NONE") {} Student::Student(string pname, string pdob, string pmajor): Person(pname, pdob), major(pmajor) {} void Student::print() const{ cout << "name : " << name << " dob : " << dob << " major : " << major << endl; } class Instructor: public Person { public: Instructor(); Instructor(string pname, string pdob, double psalary); void print() const; private: double salary; }; Instructor::Instructor():Person(), salary(0) {} Instructor::Instructor(string pname, string pdob, double psalary): Person(pname, pdob), salary(psalary) {} void Instructor::print() const{ cout << "name : " << name << " dob : " << dob << " salary : " << salary << endl; } int main(){ Person a1("JOhn", "1231231"); Student a2("Paul", "2324254", "math"); Instructor a3("George", "699393", 100); a1.print(); a2.print(); a3.print(); }
Enter password:
Enter password:
#include < iostream > #include < string > using namespace std; class Employee { public: Employee(); Employee(string pname, double psalary); protected: // Inherited classes can use these member variables string name; double salary; }; Employee::Employee():name(""), salary(0) {} Employee::Employee(string pname, double psalary):name(pname), salary(psalary) {} class Manager: public Employee { public: Manager(); Manager(string pname, double psalary, string pdepartment); void print() const; // accessor private: string department; }; Manager::Manager() :Employee() // base class constructor { department = ""; } Manager::Manager(string pname, double psalary, string pdepartment) :Employee(pname, psalary) // base class constructor { department = ""; } void Manager::print() const { cout << "Name : " << name << " salary : " << salary << " department : " << department << endl; } class Executive: public Manager { public: Executive(); Executive(string pname, double psalary, string pdepartment); void print() const; }; Executive::Executive() :Manager() {} Executive::Executive(string pname, double psalary, string pdepartment) :Manager(pname, psalary, pdepartment) {} void Executive::print() const{ // cout << "(Executive) Name : " << name << " salary : " << salary << " department : " << department << endl; cout << "(Executive) "; Manager::print(); } int main() { Executive e("John", 100, "PIC"); e.print(); // (Executive) Name : John salary : 100 department : PIC }