Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Polymorphism, Simple Payroll Application, Sample Payroll, Sample Output, Never Treat Arrays Polymorphically, Shape Hierarchy Revisited, DrawShapes(), Polymorphism and Arrays are the points you can learn in this object oriented programming subject.
Typology: Slides
1 / 30
Employee
SalariedEmp HourlyEmp CommEmp
getName calcSalary
sales commRate
hours hourlyRate
salary
calcSalary calcSalary
calcSalary
name taxRate
String
String operator = operator <<
pStr
class Employee {
private: String name; double taxRate; public: Employee( String&, double ); String getName(); virtual double calcSalary() = 0;
}
Employee::Employee( String& n,
double tr ): name(n){ taxRate = tr;
}
String Employee::getName() {
return name;
}
class SalariedEmp : public Employee
{
private:
double salary;
public:
SalariedEmp(String&,double,double); virtual double calcSalary();
}
SalariedEmp::SalariedEmp(String& n,
double tr, double sal) : Employee( n, tr ) { salary = sal;
}
double SalariedEmp::calcSalary() {
double tax = salary * taxRate; return salary – tax;
}
class HourlyEmp : public Employee {
private:
int hours; double hourlyRate;
public:
HourlyEmp(string&,double,int,double); virtual double calcSalary();
}
HourlyEmp ::HourlyEmp( String& n,
double tr, int h, double hr )
: Employee( n, tr ) { hours = h; hourlyRate = hr;
}
double HourlyEmp::calcSalary()
{
double grossPay, tax;
grossPay = hours * hourlyRate; tax = grossPay * taxRate;
return grossPay – tax;
}
class CommEmp : public Employee
{
private:
double sales; double commRate;
public:
CommEmp( String&, double, double, double ); virtual double calcSalary();
}
CommEmp::CommEmp( String& n,
double tr, double s, double cr ) : Employee( n, tr ) {
sales = s; commRate = cr;
}
double CommEmp::calcSalary()
{
double grossPay = sales * commRate;
double tax = grossPay * taxRate;
return grossPay – tax;
}
int main() {
Employee* emp[10]; emp[0] = new SalariedEmp( “Aamir”, 0.05, 15000 ); emp[1] = new HourlyEmp( “Faakhir”, 0.06, 160, 50 ); emp[2] = new CommEmp( “Fuaad”, 0.04, 150000, 10 ); … generatePayroll( emp, 10 ); return 0;
}
void generatePayroll(Employee* emp[], int size) {
cout << “Name\tNet Salary\n\n”;
for (int i = 0; i < size; i++) { cout << emp[i]->getName() << ‘\t’ << emp[i]->calcSalary() << ‘\n’; }
}
class Shape {
…
public:
Shape(); virtual void draw(){ cout << “Shape\n”; } virtual int calcArea() { return 0; }
};
class Line : public Shape {
…
public:
Line(Point p1, Point p2); void draw(){ cout << “Line\n”; }
}
void drawShapes( Shape _shape[],
int size ) { for (int i = 0; i < size; i++) {
_shape[i].draw(); }
}
int main() {
Shape _shape[ 10 ]; _shape[ 0 ] = Shape(); _shape[ 1 ] = Shape();
… drawShapes( _shape, 10 ); return 0;
}
Shape
Shape
Shape
…
int main() {
Point p1(10, 10), p2(20, 20), … Line _line[ 10 ]; _line[ 0 ] = Line( p1, p2 ); _line[ 1 ] = Line( p3, p4 ); … drawShapes( _line, 10 ); return 0;
}