Sie sind auf Seite 1von 5

PROGRAM NO.

-13
AIM:
Make a class Employee with the name and salary. Make a class Manager inherit from
Employee. Add an instance variable, named department of type string. Supply a method to
toString that prints the manager’s name, department and salary. Make a class Executive
inherit from Manager. Supply a method toString that prints the string”Executive” followed
by the information stored in the Manager superclass object. Supply a test program that tests
these classes and methods.

CODE:

#include<bits/stdc++.h>

using namespace std;

class employee
{
public:
string name;
float salary;
};

class manager:public employee


{
public:
string dept;
void tostring()
{
cout<<"enter name of manager dept and his salary:";
cin>>name>>dept>>salary;
cout<<"manager's name, dept,salary:"<<name<<" "<<dept<<" "<<salary;
}
};

class executive:public manager


{
public:
void tostring()
{
cout<<"\n\nEnter name of executive, dept and his salary:";
cin>>name>>dept>>salary;
cout<<"Executive-name, dept,salary:"<<name<<" "<<dept<<" "<<salary;
}
};

int main()
{
manager m;
executive e;
m.tostring();
e.tostring();
}

OUTPUT:
PROGRAM NO.-14
AIM:
Write a superclass Worker and subclass HourleyWorker and SalariedWorker. Every
worker has a name and a salary rate. Write a method computePay (int hours) that computes
the weekly pay for every worker. An hourly worker gets paid the hourly wages for the actual
number of hours worked. If hours is atmost 40. If the hourly worker work more than 40
hours, the excess is paid at time and a half. The salary worker gets paid the hourly wage for
40 hours, no matter what the actual number of hours is.Write a static method that uses
polymorphism to compute the pay of any Worker. Supply a test program that tests these
cases and methods.

CODE:
#include<bits/stdc++.h>

using namespace std;

class worker {

public:

string name;

float salaryrate=2;

};

class hourlyworker:public worker {

public:

hourlyworker() {

cout<<"Enter the name of hourlyworker:";

cin>>name;

void computepay(int hours) {

if(hours<=40)
{

float weeklypay=hours*salaryrate;

cout<<"Weeklypay of hourlyworker"<<name<<" is:"<<weeklypay;

else

int hours1=hours-40;

float weeklypay1=(hours1*salaryrate)/2;

float weeklypay=hours*salaryrate+weeklypay1;

cout<<"Weeklypay of hourlyworker"<<name<<" is:"<<weeklypay;

};

class salariedworker:public worker

public:

salariedworker()

cout<<"\n\nEnter the name of salariedworker:";

cin>>name;

void computepay(int hours)

float weeklypay=40*salaryrate;

cout<<"weeklypay of salariedworker is:"<<weeklypay;

}
};

main()

int hour;

cout<<"enter hours of a worker in a week:";

cin>>hour;

hourlyworker h;

h.computepay(hour);

salariedworker s;

s.computepay(hour);

OUTPUT:

Das könnte Ihnen auch gefallen