|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
OO C++: Inheritance Assignment
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
For my assignment, my instructor has provided me with a generic employee class. I am to use it to create the inherited derived class "Salaried." From that I will create another derived class "Foreperson."
The part I'm having trouble with is calculating gross pay of a "Foreperson." Gross pay is "salary + (salary*bonus)." But my problem is that salary is a private part of "Salaried" so I get compile errors. ![]() Code:
Salaried.h
#ifndef _SALARIED
#define _SALARIED
#include "Employee.h"
class Salaried : public Employee
{
double _salaried_pay;
public:
Salaried ( );
Salaried ( const STRING& Name, unsigned Dependents, double Salary );
Salaried ( const Salaried& );
const Salaried& operator= ( const Salaried& );
double getSalary ( ) const;
void setSalary ( double );
friend ostream &operator << ( ostream&, const Salaried& );
};
#endif
Code:
Salaried.cpp
#include "Salaried.h"
Salaried::Salaried ( )
: Employee()
{
_salaried_pay = 0;
}
Salaried::Salaried ( const STRING& Name, unsigned Dependents, double Salary
)
: Employee ( Name, Dependents )
{
_salaried_pay = Salary;
}
Salaried::Salaried ( const Salaried& S )
: Employee( static_cast<const Employee&>(S))
{
_salaried_pay = S._salaried_pay;
}
const Salaried& Salaried::operator= ( const Salaried& S )
{
if ( &S == this ) return *this;
static_cast<Employee&>(*this) = static_cast<const Employee&>(S);
_salaried_pay = S._salaried_pay;
return *this;
}
double Salaried::getSalary ( ) const { return _salaried_pay; }
void Salaried::setSalary ( double d ) { _salaried_pay = d; }
ostream& operator << ( ostream& os, const Salaried& S )
{
os << static_cast<const Employee&>(S);
os << "Salary: " << S._salaried_pay<<endl;
return os;
}
Code:
Foreperson.h
#ifndef _FOREPERSON
#define _FOREPERSON
#include "Salaried.h"
class Foreperson : public Salaried
{
double _bonus_rate;
public:
Foreperson ( );
Foreperson ( const STRING& Name, unsigned Dependents, double
Commission,
double BonusRate );
Foreperson ( const Foreperson& );
const Foreperson& operator= ( const Foreperson& );
double getBonusRate ( ) const;
void setBonusRate ( double );
friend ostream &operator << ( ostream&, const Foreperson& );
virtual double GrossPay ( ) const;
};
#endif
Code:
Foreperson.cpp
#include "Foreperson.h"
Foreperson::Foreperson ( )
:Salaried()
{
_bonus_rate = 0;
}
Foreperson::Foreperson ( const STRING& Name, unsigned Dependents,
double Commission, double BonusRate )
:Salaried(Name, Dependents, Commission)
{
_bonus_rate = BonusRate;
}
Foreperson::Foreperson ( const Foreperson& Is )
: Salaried ( static_cast<const Salaried&>(Is) )
{
_bonus_rate = Is._bonus_rate;
}
const Foreperson& Foreperson::operator= ( const Foreperson& Is)
{
if ( &Is == this ) return *this;
static_cast<Salaried&>(*this) = static_cast<const Salaried&>(Is);
_bonus_rate = Is._bonus_rate;
return *this;
}
double Foreperson::getBonusRate ( ) const { return _bonus_rate; }
void Foreperson::setBonusRate ( double d ) { _bonus_rate = d; }
ostream &operator << ( ostream& os, const Foreperson& Is)
{
os << static_cast<const Salaried&>(Is);
os << "Bonus Rate: " << Is._bonus_rate<<endl;
return os;
}
double Foreperson::GrossPay ( ) const
{
double grosspay;
cout << "Foreperson's pay: ";
grosspay = _salaried_pay + (_salaried_pay *_bonus_rate);
cout << grosspay << endl << endl;
return 1;
}
Code:
g++ -pedantic Foreperson.cpp -c Salaried.h: In member function âvirtual double Foreperson::GrossPay() constâ: Salaried.h:10: error: âdouble Salaried::_salaried_payâ is private Foreperson.cpp:48: error: within this context Salaried.h:10: error: âdouble Salaried::_salaried_payâ is private Foreperson.cpp:48: error: within this context make: *** [Foreperson.o] Error 1
__________________
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
bump for good luck
__________________
The Samsung Owners Club Member of the OCN Diablo III Club The AMERICAN Overclockers!
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
Yay, I got it to compile. I called the getSalary function in Salary to do it.
__________________
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
Well, now I'm having trouble getting GrossPay to actually output something when I use it in main.
Code:
Foreperson.cpp
...
double Foreperson::GrossPay ( ) const
{
double p;
double s = Salaried::getSalary();
cout << "Foreperson's Gross Pay: " << endl;
p = s + (s * _bonus_rate);
return 1;
}
Code:
main.cpp
#include <iostream>
#include "Foreperson.h"
using namespace std;
int main()
{
Foreperson f1("Bob",4,300,20);
cout << f1;
f1.GrossPay;
return 0;
}
Code:
In f1.Grosspay error: statement cannot resolve address of overloaded function
__________________
|
|||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
The Dapper Swindler
![]() |
Grosspay(), not Grosspay
Also if you want variables to be accessible from child classes declare them under protected:, not private:
|
|||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
New to Overclock.net
|
this sounds almost exactly like the assignment I got from a prof in first year.
__________________It was in java and his requirement was that he should be able to drop in any class file (for an employee, named properly with appropriate methods) and have it work correctly. Half the class dropped after the assignment. Anyway, good luck!
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
That's great... ._.
In any case, I need to bring all this together in another class that has a fixed size array (pick a size arbitrarily) with pointers to the Employee class and have it be processed polymorphically.
__________________
|
|||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
Pardon the double post...
Alright, everything compiles, but I'm getting a segmentation fault. What does that mean? Code:
PAYROLL.CPP
#include "Payroll.h"
Payroll::Payroll ( )
{
}
Payroll::Payroll(int Employee[10], int size)
{
Employee = new int[size];
for (int i=0; i<size; i++)
{
Employee[i] = i;
}
}
Payroll::~Payroll()
{
//delete[] Employee;
}
void Payroll::Run( )
{
int x;
int t;
double salary;
double bonus;
double hourly_rate;
double hours;
double bonus_num;
double gross = 0;
int i = 1;
int j;
int exit = 0;
STRING name;
Employee * Employee[10];
do
{
cout << "Which of the following tasks would you like to perform? \n";
cout << "(1) - Enter information for a new employee\n";
cout << "(2) - Calculate gross payroll for this pay period\n";
cout << "(3) - Display a list of employees\n";
cout << "(4) - Exit the program\n\n";
cout << "Please choose a task... \n";
cin >> x;
switch(x)
{
case 1:
cout << "Enter the employee's name: \n";
cin >> name;
cout << "What type of employee is he/she? \n";
cout << "(1) - Foreperson\n";
cout << "(2) - Manager\n";
cout << "(3) - Assembler\n";
cout << "(4) - Truckdriver\n";
cout << "Please choose a type... \n";
cin >> t;
switch(t)
{
case 1:
cout << "Enter the Foreperson's salary: \n";
cin >> salary;
cout << "Enter the Foreperson's bonus rate: \n";
cin >> bonus;
Employee[i] = new Foreperson(name, 4, salary, bonus);
i++;
cout << endl << endl;
break;
case 2:
cout << "Enter the Manager's salary: \n";
cin >> salary;
cout << "Enter the Manager's profit share: \n";
cin >> bonus;
Employee[i] = new Manager(name, 4, salary, bonus);
i++;
cout << endl << endl;
break;
case 3:
cout << "Enter the Assembler's hourly rate: \n";
cin >> hourly_rate;
cout << "Enter the number of hours worked by Assembler's: \n";
cin >> hours;
cout << "Enter the Assembler's piecework bonus: \n";
cin >> bonus;
cout << "Enter the number of pieces assembled by Assembler: \n";
cin >> bonus_num;
Employee[i] = new Assembler(name, 6, hourly_rate, hours, bonus, bonus_num);
i++;
cout << endl << endl;
break;
case 4:
cout << "Enter the Truckdriver's hourly rate: \n";
cin >> hourly_rate;
cout << "Enter the number of hours driven by Truckdriver's: \n";
cin >> hours;
cout << "Enter the Truckdriver's driving bonus: \n";
cin >> bonus;
cout << "Enter the number of hours driven by Truckdriver: \n";
cin >> bonus_num;
Employee[i] = new Truckdriver(name, 6, hourly_rate, hours, bonus, bonus_num);
i++;
cout << endl << endl;
break;
}
case 2:
for (j = 1; j<=i; j++)
{
gross = gross + Employee[j]->GrossPay();
}
cout << gross << endl << endl;
break;
case 3:
for (j = 1; j<=i; j++)
{
cout << Employee[j]->getName() << endl;
}
break;
case 4:
exit = 1;
break;
}
} while (exit != 1);
Code:
MAIN.CPP
#include <iostream>
#include "Payroll.cpp"
using namespace std;
int main()
{
Payroll x;
x.Run();
return 0;
}
__________________
Last edited by stn0092 : 1 Week Ago at 07:41 PM |
|||||||||||||
|
|
|
|
|
#9 (permalink) |
|
Case Modder
![]() |
"Segmentation fault" means your program tried to access an invalid memory location which the OS then prevented it from doing.
Edit: Your constructor doesn't make much sense. What are you trying to accomplish there?
__________________
Rich Custom Wooden Case Builder
Last edited by Spotswood : 1 Week Ago at 08:56 PM |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|