Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 3 Weeks Ago   #1 (permalink)
4.0 GHz
 
stn0092's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: N. California
Posts: 604

Rep: 41 stn0092 is acknowledged by some
Unique Rep: 36
Trader Rating: 15
Default OO C++: Inheritance Assignment

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
__________________

System: Lancelot - 2008
CPU
Intel Q9550 E0 @ 4.03GHz (475x8.5)
Motherboard
eVGA 790i FTW SLI
Memory
2x2GB Corsair XMS3 DDR3
Graphics Card
2x eVGA Geforce GTX 285 in SLI
Hard Drive
128GB Falcon SSD, 2x1TB Caviar Black
Sound Card
ASUS Xonar D2
Power Supply
Corsair HX1000W
Case
Lian-Li A70B
CPU cooling
HDT-S1283; Ultra Kaze 3000 @ 2200RPM; Tuniq TX-2
GPU cooling
Stock fan
OS
Windows 7 Professional x64
Monitor
24" Samsung 2493HM
stn0092 is offline   Reply With Quote
Old 3 Weeks Ago   #2 (permalink)
4.0 GHz
 
Jrice00's Avatar
 
intel nvidia

Join Date: Dec 2008
Location: Richmond, ME
Posts: 1,139

Rep: 63 Jrice00 is acknowledged by some
Unique Rep: 57
Folding Team Rank: 1226
Hardware Reviews: 3
Trader Rating: 7
Default

bump for good luck

System: PC
CPU
e5200 3.75Ghz
Motherboard
Gigabyte UD3R
Memory
2Gb Patriot
Graphics Card
BFG GTX 260
Hard Drive
Seagate 250gb
Sound Card
ob
Power Supply
OCZ ModXStream 500w
Case
Raidmax Aura 'really black'
CPU cooling
S1283V
GPU cooling
Stock
OS
Vista Home Premium 32-bit
Monitor
Samsung Syncmaster 19"
Jrice00 is offline I fold for Overclock.net   Reply With Quote
Old 3 Weeks Ago   #3 (permalink)
4.0 GHz
 
stn0092's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: N. California
Posts: 604

Rep: 41 stn0092 is acknowledged by some
Unique Rep: 36
Trader Rating: 15
Default

Yay, I got it to compile. I called the getSalary function in Salary to do it.

__________________

System: Lancelot - 2008
CPU
Intel Q9550 E0 @ 4.03GHz (475x8.5)
Motherboard
eVGA 790i FTW SLI
Memory
2x2GB Corsair XMS3 DDR3
Graphics Card
2x eVGA Geforce GTX 285 in SLI
Hard Drive
128GB Falcon SSD, 2x1TB Caviar Black
Sound Card
ASUS Xonar D2
Power Supply
Corsair HX1000W
Case
Lian-Li A70B
CPU cooling
HDT-S1283; Ultra Kaze 3000 @ 2200RPM; Tuniq TX-2
GPU cooling
Stock fan
OS
Windows 7 Professional x64
Monitor
24" Samsung 2493HM
stn0092 is offline   Reply With Quote
Old 3 Weeks Ago   #4 (permalink)
4.0 GHz
 
stn0092's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: N. California
Posts: 604

Rep: 41 stn0092 is acknowledged by some
Unique Rep: 36
Trader Rating: 15
Default

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
__________________

System: Lancelot - 2008
CPU
Intel Q9550 E0 @ 4.03GHz (475x8.5)
Motherboard
eVGA 790i FTW SLI
Memory
2x2GB Corsair XMS3 DDR3
Graphics Card
2x eVGA Geforce GTX 285 in SLI
Hard Drive
128GB Falcon SSD, 2x1TB Caviar Black
Sound Card
ASUS Xonar D2
Power Supply
Corsair HX1000W
Case
Lian-Li A70B
CPU cooling
HDT-S1283; Ultra Kaze 3000 @ 2200RPM; Tuniq TX-2
GPU cooling
Stock fan
OS
Windows 7 Professional x64
Monitor
24" Samsung 2493HM
stn0092 is offline   Reply With Quote
Old 3 Weeks Ago   #5 (permalink)
The Dapper Swindler
 
nathris's Avatar
 
intel ati

Join Date: Sep 2007
Location: Canada
Posts: 7,141

Rep: 495 nathris is a proven membernathris is a proven membernathris is a proven membernathris is a proven membernathris is a proven member
Unique Rep: 382
Folding Team Rank: 1016
Hardware Reviews: 1
Trader Rating: 0
Default

Grosspay(), not Grosspay

Also if you want variables to be accessible from child classes declare them under protected:, not private:

System: The Possum
CPU
e8400 @ 4GHz (500x8)
Motherboard
P5Q Deluxe
Memory
4GB G.SKILL DDR2-1000
Graphics Card
XFX Radeon HD 4870 XXX (840/1078)
Hard Drive
WD6401AALS
Sound Card
X-Fi Platinum Fatal1ty Championship Gamer Edition
Power Supply
Corsair HX750W
Case
CM690
CPU cooling
Xigmatek HDT-S1283 w/XLF-F1253
GPU cooling
Accelero Twin Turbo
OS
Windows 7 Professional
Monitor
Samsung 2443BW 24"
nathris is online now I fold for Overclock.net Overclocked Account   Reply With Quote
Old 3 Weeks Ago   #6 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Nov 2007
Location: Surrey, BC, Canada
Posts: 272

Rep: 14 serge2k Unknown
Unique Rep: 14
Trader Rating: 1
Default

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!
__________________
System: The Black Box
CPU
E6750
Motherboard
Asus P5N-E SLI
Memory
2GB DDR-2 800 MHz
Graphics Card
EVGA 8800 GTS 640
Hard Drive
2x250GB Seagate in Raid 0, 2x1TB WD Caviar Blacks
Sound Card
integrated
Power Supply
Antec Earthwatts 500W
Case
Antec Sonata 3
CPU cooling
Xigmatek HDT S1283
GPU cooling
stock
OS
Windows 7 Ultimate x64
Monitor
22" LG
serge2k is offline   Reply With Quote
Old 3 Weeks Ago   #7 (permalink)
4.0 GHz
 
stn0092's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: N. California
Posts: 604

Rep: 41 stn0092 is acknowledged by some
Unique Rep: 36
Trader Rating: 15
Default

Quote:
Originally Posted by serge2k View Post
Half the class dropped after the assignment. Anyway, good luck!
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.
__________________

System: Lancelot - 2008
CPU
Intel Q9550 E0 @ 4.03GHz (475x8.5)
Motherboard
eVGA 790i FTW SLI
Memory
2x2GB Corsair XMS3 DDR3
Graphics Card
2x eVGA Geforce GTX 285 in SLI
Hard Drive
128GB Falcon SSD, 2x1TB Caviar Black
Sound Card
ASUS Xonar D2
Power Supply
Corsair HX1000W
Case
Lian-Li A70B
CPU cooling
HDT-S1283; Ultra Kaze 3000 @ 2200RPM; Tuniq TX-2
GPU cooling
Stock fan
OS
Windows 7 Professional x64
Monitor
24" Samsung 2493HM
stn0092 is offline   Reply With Quote
Old 1 Week Ago   #8 (permalink)
4.0 GHz
 
stn0092's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: N. California
Posts: 604

Rep: 41 stn0092 is acknowledged by some
Unique Rep: 36
Trader Rating: 15
Default

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;
}
__________________

System: Lancelot - 2008
CPU
Intel Q9550 E0 @ 4.03GHz (475x8.5)
Motherboard
eVGA 790i FTW SLI
Memory
2x2GB Corsair XMS3 DDR3
Graphics Card
2x eVGA Geforce GTX 285 in SLI
Hard Drive
128GB Falcon SSD, 2x1TB Caviar Black
Sound Card
ASUS Xonar D2
Power Supply
Corsair HX1000W
Case
Lian-Li A70B
CPU cooling
HDT-S1283; Ultra Kaze 3000 @ 2200RPM; Tuniq TX-2
GPU cooling
Stock fan
OS
Windows 7 Professional x64
Monitor
24" Samsung 2493HM

Last edited by stn0092 : 1 Week Ago at 07:41 PM
stn0092 is offline   Reply With Quote
Old 1 Week Ago   #9 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

"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
Overclock.net Mod of the Month

Last edited by Spotswood : 1 Week Ago at 08:56 PM
Spotswood is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 02:31 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.14027 seconds with 8 queries