The C++ multi-inheritance with variable and function overloading program example

 

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: Inherited/derived class which inherit from two base classes demonstrating the variable and function/method overloading in C++ programming

To show: How the derived/inherited class inherit variables and function/methods from two base classes for multi inheritance C++ programming

 

 

// accessing the base classes information via derived/inherited class with some

// overloaded functions/methods, similar name but different parameters (different signatures)

#include <iostream>

using namespace std;

 

// class declaration and implementation

// moving_van base class

class moving_van

{

protected:

double payload;

// take note of this variable

double weight;

double mpg;

public:

void initialize(double pl, double gw, double input_mpg)

{

payload = pl;

weight = gw;

mpg = input_mpg;

};

double efficiency(void)

{

return(payload / (payload + weight));

};

double cost_per_ton(double fuel_cost)

{

return (fuel_cost / (payload / 2000.0));

};

};

 

// driver base class

class driver

{

protected:

double hourly_pay;

// another weight variable variable with same name as in class number one

double weight;

public:

// same method name but different number of parameters (different signature)

void initialize(double pay, double input_weight)

{

hourly_pay = pay;

weight = input_weight;

};

 

double cost_per_mile(void)

{

return (hourly_pay / 55.0);

};

 

double drivers_weight(void)

{

return (weight);

};

};

 

// driven_truck derived class with multi inheritance

// declaration and implementation

class driven_truck : public moving_van, public driver

{

public:

// another same method name but different number of parameters

void initialize_all(double pl, double gw, double input_mpg, double pay)

{

payload = pl;

moving_van::weight = gw;

mpg = input_mpg;

hourly_pay = pay;

};

 

double cost_per_full_day(double cost_of_gas)

{

return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg);

};

 

// see, how to call different variables (different signatures) with similar name

double total_weight(void)

{

cout<<"\nCalling appropriate member variable, ";

cout<<"(moving_van::weight)+(driver::weight)\n";

cout<<"-------------------------------------------------------------------------\n";

return ((moving_van::weight) + (driver::weight));

};

};

 

// the main program

int main(void)

{

driven_truck john_merc;

// accessing the driven_truck derived class method

john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50);

//accessing the driver base class

john_merc.driver::initialize(15.50, 250.0);

 

// printing some data

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;

cout<<"The cost per day for John to drive Merc is $"<<john_merc.cost_per_full_day(1.129)<<endl;

cout<<"The total weight is "<<john_merc.total_weight()<<" ton"<<endl;

 

return 0;

}

 

Output example:

 

The efficiency of the Merc is 0.625 %

The cost per mile for John to drive is $0.281818

The cost per day for John to drive Merc is $219.531

Calling appropriate member variable, (moving_van::weight)+(driver::weight)

-------------------------------------------------------------------------

The total weight is 12250 ton

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index