1. 程式人生 > >c++ 多繼承 public

c++ 多繼承 public

實例 .class str pri get 由於 cout 修改 erl

以下代碼會報錯

#include <iostream>
using namespace std;
 
class Sofa
{
public:
    Sofa();
    ~Sofa();
 
    void sit() {
        cout<<"sit!"<<endl;
    }
 
    void setWeight(int w) {
        this->weight = w;
    } 
 
    int getWeight() {
        return this->weight;
    }
 
    
void showWeight() { cout<<this->weight<<endl; } private: int weight; }; Sofa::Sofa() { } Sofa::~Sofa() { } class Bed { public: Bed(); ~Bed(); void lie() { cout<<"lie!"<<endl; } void setWeight(int w) { this
->weight = w; } int getWeight() { return this->weight; } void showWeight() { cout<<this->weight<<endl; } private: int weight; }; Bed::Bed() { } Bed::~Bed() { } class Sofabed : public Bed, public Sofa { public: Sofabed();
~Sofabed(); void showWeight() { Sofa::showWeight(); cout<<"&&"<<endl; Bed::showWeight(); } private: }; Sofabed::Sofabed() { } Sofabed::~Sofabed() { } int main () { Sofabed myfur; myfur.sit(); myfur.lie(); myfur.setWeight(12); // 不清楚是哪個函數,會報錯 myfur.Sofa::setWeight(12); // 要寫 [obj.classname::function(12)] ; myfur.Sofa::showWeight(); myfur.Bed::setWeight(99); myfur.Bed::showWeight(); // sofa & bed 的 member 是兩個不一樣的。 // 也可以在 derived class 裏,overload 名字沖突的函數 myfur.showWeight(); system("pause"); return 0; }

修改的代碼

#include <iostream>
using namespace std;
 
class Sofa
{
public:
    Sofa(){
        cout<<"Sofa()"<<endl;
}
    ~Sofa(){
        cout<<"~Sofa()"<<endl;
    }
 
    void sit() {
        cout<<"sit!"<<endl;
    }
 
    void setWeight(int w) {
        this->weight = w;
    } 
 
    int getWeight() {
        return this->weight;
    }
 
    void showWeight() {
        cout<<this->weight<<endl;
    }
 
private:
 
    int weight;
 
};
 
 
class Bed
{
public:
    Bed(){
        cout<<"Bed()"<<endl;
}
    ~Bed(){
        cout<<"~Bed()"<<endl;
    }
 
    void lie() {
        cout<<"lie!"<<endl;
    }
 
    void setWeight(int w) {
        this->weight = w;
    } 
 
    int getWeight() {
        return this->weight;
    }
 
    void showWeight() {
        cout<<this->weight<<"\n\n"<<endl;
    }
 
private:
 
    int weight;
};
 
 
 
class Sofabed : public Bed, public Sofa
{
public:
    Sofabed(){
        cout<<"Sofabed()\n\n"<<endl;
}
    ~Sofabed(){
        cout<<"~Sofabed()"<<endl;
    }
 
    void showWeight() {
        cout<<"Sofa::showWeight()";
        Sofa::showWeight();
        cout<<"&";
        cout<<this->weight;
        cout<<"&"<<endl;
        cout<<"Bed::showWeight()";
        Bed::showWeight();
 
    }
    
    void setWeight(int w) {
        this->weight = w;
    } 
    
private:
    int weight;
 
};
 
 
int main () {
 
    Sofabed myfur;
    myfur.sit();
    myfur.lie();
 
    
    myfur.Sofa::setWeight(12); // 要寫 [obj.classname::function(12)] ;
    myfur.Sofa::showWeight();
    myfur.Bed::setWeight(99);
    myfur.Bed::showWeight(); // sofa & bed 的 member 是兩個不一樣的。
 
    myfur.setWeight(199); // 修改後 
    myfur.showWeight();
    
    return 0;
 
}

由於,繼承後,編譯器不清楚setWeight函數是哪個類的,所以報錯了,修改後,我們調用的就是實例化的那個類的函數,所以不會報錯

c++ 多繼承 public