1. 程式人生 > 其它 >多繼承和單繼承

多繼承和單繼承

#include <iostream>
 
using namespace std;
 
class shape
{
public:
    void setHigh(double _high)
    {
        high = _high;
    }

    void setWidth(double _width)
    {
        width = _width;
    }

public:
    double high;
    double width;
};

class cost
{
public:
    int getCost(double area)
    {

        
return area * 2; } }; class rectangle : public shape { public: double getarea() { return high * width; } }; class rectangleCost : public shape, public cost { public: double getarea() { return high * width; } }; int main () { rectangle rect; rect.setHigh(
2); rect.setWidth(4); double area = rect.getarea(); cout << "面積:" << area << endl; rectangleCost rectCost; rectCost.setHigh(2); rectCost.setWidth(5); double area_c = rectCost.getarea(); double cost = rectCost.getCost(area_c); cout << "面積 :
" << area_c << endl; cout << "花費 :" << cost << endl; return 0; }

輸出結果:

面積:8
面積 :10
花費 :20