This inheritance problem compiles and runs, however, the output is not right and I am having a hard time tracking down the problem. Can anyone point in the right direction. Here is the code: [code]
[code][/c[co[code]#include <iostream >
usingnamespace std;
// Member function definitions for Date class.
class Date
{
public:
Date(){} // default constructor
void setDate();
void print() const; // print date in month/day/year format
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int ) const;
};
void Date::setDate()
{
int mn,dy,yr;
cout<<"Enter day:";
cin>>dy;
cout<<"Enter month:";
cin>>mn;
cout<<"Enter year";
cin>>yr;
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr >= 1900 && yr <= 2100 ? yr : 1990;
day = checkDay( dy ); // validate the day
}
// Utility function to confirm proper day value
// based on month and year.
int Date::checkDay( int testDay ) const
{
int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for possible leap year
testDay == 29 &&
( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
}
// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
class Employee
{
private:
int ID;
public:
char name[80];
char address[80];
char phone[80];
Date birthDate;
Date hireDate;
Employee(int id);
int GetID();
virtualdouble getMonthsPay() = 0;
};
Employee::Employee(int id)
{
ID=id;
cout<<"Enter name:";
cin>>name;
//fflush(stdin);
cout<<"Enter Address:";
cin>>address;
cout<<"Enter phone no:";
cin>>phone;
birthDate.setDate();
hireDate.setDate();
}
int Employee::GetID()
{
return ID;
}
class SalariedEmployee : public Employee
{
public:
double salary;
double getMonthsPay();
SalariedEmployee(int id);
};
SalariedEmployee::SalariedEmployee(int id):Employee(id)
{
if(id!=0){
cout<<"Enter salary:";
cin>>salary;}
}
double SalariedEmployee::getMonthsPay()
{
return salary;
}
class HourlyEmployee : public Employee
{
public:
double hours;
double hourlyRate;
double getMonthsPay();
HourlyEmployee(int id);
};
HourlyEmployee::HourlyEmployee(int id):Employee(id)
{
cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
}
double HourlyEmployee::getMonthsPay()
{
return hours*hourlyRate;
}
/* All outsourced employees have an ID code of zero
and are paid a flat salary rate of $20/month. */
class OutsourcedEmployee : public SalariedEmployee
{
public:
double getMonthsPay();
OutsourcedEmployee();
};
OutsourcedEmployee::OutsourcedEmployee():SalariedEmployee(0)
{}
double OutsourcedEmployee::getMonthsPay()
{
return 20;
}
void main()
{
//Hourly Employee class
cout<<"Hourly Employee class"; //creating object
HourlyEmployee emp1(23);
//outputting employee datails
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"<< //Saleried employee class
cout<<"Salaried employee class"; //creating object
SalariedEmployee emp2(143);
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"; //Outsourced Employee class
OutsourcedEmployee emp3;
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp3.hireDate.print();
cout<<"PAY:"<< //pause system for a while
system("pause");
}//end main
How do you know the output is not right? What were you expecting?
Also, one of the main ways to track down bugs is to make your code as clear and readable as possible. You might consider using some indentation and leaving the occasional gap between blocks of code. That will make finding errors easier for you, and for those you ask to help.
#include <iostream >
using namespace std;
// Member function definitions for Date class.
class Date
{
public:
Date(){} // default constructor
void setDate();
void print() const; // print date in month/day/year format
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int ) const;
};
void Date::setDate()
{
int mn,dy,yr;
cout<<"Enter day:";
cin>>dy;
cout<<"Enter month:";
cin>>mn;
cout<<"Enter year";
cin>>yr;
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr >= 1900 && yr <= 2100 ? yr : 1990;
day = checkDay( dy ); // validate the day
}
// Utility function to confirm proper day value
// based on month and year.
int Date::checkDay( int testDay ) const
{
int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for possible leap year
testDay == 29 &&
( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
}
// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
class Employee
{
private:
int ID;
public:
char name[80];
char address[80];
char phone[80];
Date birthDate;
Date hireDate;
Employee(int id);
int GetID();
virtual double getMonthsPay() = 0;
};
class HourlyEmployee : public Employee
{
public:
double hours;
double hourlyRate;
double getMonthsPay();
HourlyEmployee(int id);
};
HourlyEmployee::HourlyEmployee(int id):Employee(id)
{
cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
}
double HourlyEmployee::getMonthsPay()
{
return hours*hourlyRate;
}
/* All outsourced employees have an ID code of zero
and are paid a flat salary rate of $20/month. */
class OutsourcedEmployee : public SalariedEmployee
{
public:
double getMonthsPay();
OutsourcedEmployee();
};
OutsourcedEmployee::OutsourcedEmployee():SalariedEmployee(0)
{}
double OutsourcedEmployee::getMonthsPay()
{
return 20;
}
void main()
{
//Hourly Employee class
cout<<"Hourly Employee class"; //creating object
HourlyEmployee emp1(23);
//outputting employee datails
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"
<< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"<< //Saleried employee class
cout<<"Salaried employee class"; //creating object
SalariedEmployee emp2(143);
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp1.hireDate.print();
cout<<"PAY:"; //Outsourced Employee class
OutsourcedEmployee emp3;
cout<<"ID:"<< cout<<"Name:"<< cout<<"Address:"<< cout<<"Phone:"<< cout<<"HireDate:";
emp3.hireDate.print();
cout<<"PAY:"<< //pause system for a while
system("pause");
Hourly Employee classEnter name:Brian
Enter Address:1234 glijl
Enter phone no:Enter day:19
Enter month:12
Enter year2010
Enter day:14
Enter month:10
Enter year2010
Enter number of hours:40
Enter hourly pay rate:13.00
[b]ID:6486CACCName:6486CACCAddress:6486CACCPhone:6486CACCHireDate:10/14/2010PAY:648
6CACCSalaried employee classEnter name:
today I go through your program, actually which you do is almost correct but you need to add
some organizing to your program to be clear to the user.
As filipe said you need to be carefull to that points which he mentioned above.
Also you need to check your calculations and that formulas which you use.
Generally, I added some hints on your program to be easy to you to fix it, try to do your best,
if you give up I'll help you.
all best :
Bushra
#include <iostream >
using namespace std;
// Member function definitions for Date class.
class Date
{
public:
Date(){} // default constructor
void setDate();
void print() const; // print date in month/day/year format
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int ) const;
};
void Date::setDate()
{
int mn,dy,yr;
cout<<"Enter day:";
cin>>dy;
cout<<"Enter month:";
cin>>mn;
cout<<"Enter year:";
cin>>yr;
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
//year = yr >= 1900 && yr <= 2100 ? yr : 1990;
if(yr>=1900 && yr<=2100)
year=yr;
day = checkDay( dy ); // validate the day
}
// Utility function to confirm proper day value
// based on month and year.
int Date::checkDay( int testDay ) const
{
int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( (month == 2) && // February: Check for possible leap year
(testDay == 29) &&
( year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0 ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
}
// Print Date object in form month/day/year
void Date::print() const
{
cout << month << '/' << day << '/' << year<<endl;
}
class Employee
{
private:
int ID;
public:
char name[80];
char address[80];
char phone[80];
Date birthDate;
Date hireDate;
Employee(int id);
int GetID();
virtual double getMonthsPay() = 0;
};
cout<<"Enter number of hours:";
cin>>hours;
cout<<"Enter hourly pay rate:";
cin>>hourlyRate;
cout<<endl;
}
double HourlyEmployee::getMonthsPay()
{
return hours*hourlyRate;
}
/* All outsourced employees have an ID code of zero
and are paid a flat salary rate of $20/month. */
class OutsourcedEmployee : public SalariedEmployee
{