Specifying the Access Levels of the Inherited Members - C++ Class

C++ examples for Class:Inheritance

Description

Specifying the Access Levels of the Inherited Members

Demo Code

                                                                 
#include <iostream>
                                                                 
using namespace std;
                                                                 
class Product//from  w w w  .  j ava  2 s . c o m
{
private:
    int Price;
                                                                 
protected:
    int Weight;
                                                                 
public:
    Product(int APrice, int AWeight);
    int GetPrice();
    int GetWeight();
};
                                                                 
class Pizza : public Product
{
protected:
    int Diameter;
                                                                 
public:
    Pizza(int APrice, int AWeight, int ADiameter);
    void DumpInfo();
};
                                                                 
class DeepDishPizza : public Pizza
{
private:
    int Height;
                                                                 
public:
    DeepDishPizza(int APrice, int AWeight, int ADiameter, int AHeight);
    void DumpDensity();
};
                                                                 
Product::Product(int APrice, int AWeight)
{
    Price = APrice;
    Weight = AWeight;
}
                                                                 
int Product::GetPrice()
{
    return Price;
}
                                                                 
int Product::GetWeight()
{
    return Weight;
}
                                                                 
Pizza::Pizza(int APrice, int AWeight, int ADiameter) :
Product(APrice, AWeight)
{
    Diameter = ADiameter;
}
                                                                 
void Pizza::DumpInfo()
{
    cout << "\tFrozen pizza info:" << endl;
    cout << "\t\tWeight: " << Weight << " ounces" << endl;
    cout << "\t\tDiameter: " << Diameter << " inches" << endl;
}
                                                                 
DeepDishPizza::DeepDishPizza(int APrice, int AWeight,
int ADiameter, int AHeight) :
Pizza(APrice, AWeight, ADiameter)
{
    Height = AHeight;
}
                                                                 
void DeepDishPizza::DumpDensity()
{
    // Calculate pounds per cubic foot of deep-dish pizza
    cout << "\tDensity: ";
    cout << Weight * 12 * 12 * 12 * 14 / (Height * Diameter * 22 * 16);
    cout << " pounds per cubic foot" << endl;
}
                                                                 
int main(int argc, char *argv[])
{
    cout << "Thin crust pepperoni" << endl;
    Pizza pepperoni(450, 12, 14);
    pepperoni.DumpInfo();
    cout << "\tPrice: " << pepperoni.GetPrice() << " cents" << endl;
                                                                 
    cout << "Deep dish extra-cheese" << endl;
    DeepDishPizza extracheese(650, 21592, 14, 3);
    extracheese.DumpInfo();
    extracheese.DumpDensity();
    cout << "\tPrice: " << extracheese.GetPrice() << " cents" << endl;
    return 0;
}

Result


Related Tutorials