2016-11-17 8 views
0

Ich mache ein Inventar-System und versuche, Derivate zu verwenden, um verschiedene Elemente zu erstellen, so dass ich Standardelemente in den übergeordneten und spezialisierten in den untergeordneten Elementen haben kann. Also, was ich unten geschrieben habe, im Moment druckt es "Ich bin ein Elternteil", aber ich versuche es zu drucken "Ich bin ein Kind", und in dem Mangel an einem Kind Definition von StuffToSay Print " Ich bin ein Elternteil "Danke!Vererbung und Ändern von Variablen in C++

using namespace std; 


class myParent { 
public: 
    virtual void saySomething() { 
     cout << stuffToSay; 
    } 
    string stuffToSay = "I'm a parent"; 

private: 
}; 

class myDerivitive : public myParent{ 

public: 
    myDerivitive() {}; 
    string stuffToSay = "I'm a kid"; 
private: 

}; 


int main() { 
    myParent* people[] = { 
     new myDerivitive() 
    }; 

    cout << people[0]->stuffToSay; 


    system("pause"); 
} 

Antwort

0

So etwas wie dies normalerweise erfolgt der Konstruktor Ihrer Klasse verwenden, das Kind Klasse alle Variablen seiner Elternklasse so zu tun, was Sie suchen es wie dies getan werden kann:

using namespace std; 


class myParent { 
public: 
    myParent() { 
     stuffToSay = "I'm a parent" 
    } 
    virtual void saySomething() { 
     cout << stuffToSay; 
    } 
    string stuffToSay; 

private: 
}; 

class myDerivitive : public myParent{ 

public: 
    myDerivitive() { 
     stuffToSay = "I'm a kid"; 
    }; 
private: 

}; 


int main() { 
    myParent* people = new myDerivitive(); 

    cout << people->stuffToSay(); 

    delete people; // Simplified to a single pointer and remember to delete it 
    people = NULL; 

    system("pause"); 
} 

Bitte nehmen Sie sich einen Blick auf diesen Link, um weitere Informationen zu Klassen: http://www.cplusplus.com/doc/tutorial/classes/

dieser Link wird mit dem Verständnis der Vererbung helfen, da Ihre Derivat-Klasse die „stuffToSay“ Variable seit seiner pa hätte Miete hatte es:

http://www.cplusplus.com/doc/tutorial/inheritance/

2

Das ist nicht, wie es funktioniert. Die saySomething in Parent weiß nichts über die Zeichenfolge in der abgeleiteten Klasse und Mitgliedsvariablen sind nicht virtuell.

Sie können es z.B. wie diese

#include <iostream> 
#include <string> 

struct myParent { 
    void saySomething() { 
     cout << getSomething(); 
    } 
    virtual std::string getSomething(){ return "I'm a parent"; } 
    virtual ~myParent(){} // virtual destructor is needed 
}; 

struct myDerived : myParent { 
    virtual std::string getSomething(){ return "I'm the derived"; } 
}; 

int main() { 
    myParent* p = new myDerived(); 
    p->saySomething(); 
    delete p; // dont forget to delete !! 
} 
+0

puh Ich war mehr downvotes mit allen Fehlern in der Erwartung Code, aber [jetzt sollten sie behoben werden] (http://ideone.com/nTEpqm);) – user463035818

0

Es gibt so etwas wie eine virtual oder überschriebene Variable in C++ nicht; diese Art von Polymorphie gilt nur für Methoden. So könnten Sie dies tun:

struct parent { 
    virtual void saySomething() { 
     cout << "I'm a parent!\n"; 
    } 
}; 

struct child: parent { 
    void saySomething() override { 
     cout << "I'm a child!\n"; 
    } 
}; 

Oder man könnte es lösen mit etwas mehr wie Ihre aktuellen Struktur durch eine Schicht von Dereferenzierung Zugabe:

struct parent { 
    void saySomething() { 
     cout << thingToSay() << '\n'; 
    } 

private: 
    virtual string thingToSay() { return "I'm a parent!"; } 
}; 

class child: parent { 
    virtual string thingToSay() { return "I'm a child!"; } 
};