Write a class called CustomerProject. It should have three double fields called
ID: 3763512 • Letter: W
Question
Write a class called CustomerProject. It should have three double fields called hours (the number of hours the project took), materials (the cost of materials) and transportation (transportation costs), in that order. It should have get and set methods for each field. It should have a constructor that takes as parameters the values for each field and calls the set methods to initialize them. It should have a pure virtual function called billAmount that takes no parameters and returns a double.
Write two classes that inherit from CustomerProject: RegularProject and PreferredProject. Both should have a constructor that takes three doubles and passes them to the base class constructor. RegularProject should override billAmount to return the sum of the materials costs, the transportation costs, and $80 times the number of hours. PreferredProject (for preferred customers) should override billAmount to return the sum of 85% of the materials costs, 90% of the transportation costs, and $80 times the number of hours up to a maximum of 100 hours (any hours beyond 100 are free).
Hint: You can access the member data of the base class either of two ways:
1) make that member data protected in the base class
or 2) simply use the get methods, which the derived classes have inherited.
The files must be called: CustomerProject.hpp, CustomerProject.cpp, RegularProject.hpp, RegularProject.cpp, PreferredProject.hpp, and PreferredProject.cpp.
Explanation / Answer
//CustomerProject.h
#ifndef CUSTOMER_PORJECT
#define CUSTOMER_PORJECT
class CustomerProject
{
protected:
int hours;
double transportCost;
double materialCost;
public:
CustomerProject();
CustomerProject(int hours,double transportCost, double materialCost);
void setHours(int hours);
void setTransportCost(double tCost);
void setMaterialCost(double mCost);
int getHours();
double getTransportCost();
double getMaterialCost();
virtual double billAmount()=0;
};
#endif CUSTOMER_PORJECT
-------------------------------------------------
//CustomerProject.cpp
#include "CustomerProject.h"
CustomerProject::CustomerProject()
{
setHours(0);
setTransportCost(0);
setMaterialCost(0);
}
CustomerProject::CustomerProject(int hours,double transportCost, double materialCost)
{
setHours(hours);
setTransportCost(transportCost);
setMaterialCost(materialCost);
}
void CustomerProject::setHours(int hours)
{
this->hours=hours;
}
void CustomerProject::setTransportCost(double tCost)
{
this->transportCost=tCost;
}
void CustomerProject::setMaterialCost(double mCost)
{
this->materialCost=mCost;
}
int CustomerProject::getHours()
{
return hours;
}
double CustomerProject::getTransportCost()
{
return transportCost;
}
double CustomerProject::getMaterialCost()
{
return materialCost;
}
--------------------------------------
//RegularProject.h
#ifndef REGULAR_PROJECT
#define REGULAR_PROJECT
#include "CustomerProject.h"
class RegularProject : public CustomerProject
{
public:
RegularProject(int hours,double transportCost, double materialCost);
virtual double billAmount();
};
#endif REGULAR_PROJECT
--------------------------------------------
//RegularProject.cpp
#include "CustomerProject.h"
#include "RegularProject.h"
RegularProject::RegularProject(int hours,double transportCost, double materialCost)
:CustomerProject(hours,transportCost, materialCost)
{
}
double RegularProject::billAmount()
{
return materialCost+transportCost+80*hours;
}
----------------------------------------------
//PreferredProject.h
#ifndef PREFERRED_PROJECT
#define PREFERRED_PROJECT
#include "CustomerProject.h"
class PreferredProject : public CustomerProject
{
public:
PreferredProject(int hours,double transportCost, double materialCost);
virtual double billAmount();
};
#endif REGULAR_PROJECT
----------------------------------------------
//PreferredProject.h
#include "CustomerProject.h"
#include "PreferredProject.h"
PreferredProject::PreferredProject(int hours,double transportCost, double materialCost)
:CustomerProject(hours,transportCost, materialCost)
{
}
//override the billAmount method
double PreferredProject::billAmount()
{
if(hours<100)
return .85*materialCost+.90*transportCost+80*hours;
else
return .85*materialCost+.90*transportCost;
};
------------------------------------------------------
/*tester program of class RegularProject and PreferredProject*/
//test.cpp
#include<iostream>
//include header files
#include "CustomerProject.h"
#include "RegularProject.h"
#include "PreferredProject.h"
using namespace std;
int main()
{
//Create an object of RegularProject
RegularProject regularProject(10,200,2000);
//calling bill method
cout<<"Bill cost of Regual project : "<<regularProject.billAmount()<<endl;
//Create an object of PreferredProject
PreferredProject prefferedProject(10,200,2000);
//calling bill method
cout<<"Bill cost of Preffered project : "<<prefferedProject.billAmount()<<endl;
system("pause");
return 0;
}
---------------------------------------
Sample output:
Bill cost of Regual project : 3000
Bill cost of Preffered project : 2680
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.