The C++ multi-inheritance code sample demonstrating the inherited variable and function/method also variable and function/method overriding and overloading respectively
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Child class that derived/inherited from two base classes overriding the inherited variables and overloading the inherited function/method in C++ programming
To show: How to override the base class variables and overloading the base class function/method for C++ multi inheritance in C++ programming
// C++ multi inheritance example: the inherited variable and function/method,
// variable and function/method overriding and overloading
#include <iostream>
using namespace std;
// declaration and implementation class
// moving_van base class
class moving_van
{
protected:
double payload;
double gross_weight;
double mpg;
public:
void initialize(double pl, double gw, double input_mpg)
{
payload = pl;
gross_weight = gw;
mpg = input_mpg;
};
double efficiency(void)
{
return (payload / (payload + gross_weight));
};
double cost_per_ton(double fuel_cost)
{
return (fuel_cost / (payload / 2000.0));
};
// notice this function/method
double cost_per_full_day(double cost_of_gas)
{
return (8.0 * cost_of_gas * 55.0 / mpg);
};
};
// driver base class
class driver
{
protected:
double hourly_pay;
public:
// same method name as in moving_van base class
void initialize(double pay) {hourly_pay = pay; };
double cost_per_mile(void) {return (hourly_pay / 55.0); } ;
// another similar function/method name
double cost_per_full_day(double overtime_premium)
{return (8.0 * hourly_pay); };
};
// driven_truck derived class
// notice also the duplicated method names but different number of parameters
class driven_truck :public moving_van, public driver
{
public:
void initialize_all(double pl, double gw, double input_mpg, double pay)
{
payload = pl;
gross_weight = gw;
mpg = input_mpg;
hourly_pay = pay;
};
// another similar method name with single parameter
double cost_per_full_day(double cost_of_gas)
{
return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg);
};
};
// main program
int main(void)
{
driven_truck john_merc;
john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50);
cout<<"The efficiency of the Merc is "<<john_merc.efficiency()<<" %"<<endl;
cout<<"The cost per mile for John to drive is $"<<john_merc.cost_per_mile()<<endl<<endl;
cout<<" Calling the appropriate method using:"<<endl;
cout<<" john_merc.moving_van::cost_per_full_day()"<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<"The cost per day for the Merc is $"<<john_merc.moving_van::cost_per_full_day(1.129)<<endl<<endl;
cout<<" Calling the appropriate method using:"<<endl;
cout<<" john_merc.driver::cost_per_full_day()"<<endl;
cout<<"----------------------------------------"<<endl;
cout<<"The cost of John for a full day is $"<<john_merc.driver::cost_per_full_day(15.75)<<endl<<endl;
cout<<" Calling the appropriate method using:"<<endl;
cout<<" john_merc.driven_truck::cost_per_full_day()"<<endl;
cout<<"----------------------------------------------"<<endl;
cout<<"The cost per day for John to drive Merc is $"<<john_merc.driven_truck::cost_per_full_day(1.129)<<endl;
return 0;
}
Output example:
The efficiency of the Merc is 0.625 %
The cost per mile for John to drive is $0.227273
Calling the appropriate method using:
john_merc.moving_van::cost_per_full_day()
--------------------------------------------
The cost per day for the Merc is $95.5308
Calling the appropriate method using:
john_merc.driver::cost_per_full_day()
----------------------------------------
The cost of John for a full day is $100
Calling the appropriate method using:
john_merc.driven_truck::cost_per_full_day()
----------------------------------------------
The cost per day for John to drive Merc is $195.531
Press any key to continue . . .