Last active
December 6, 2016 22:31
-
-
Save ilicmarko/fd64013b6fa81587afa70921073181c4 to your computer and use it in GitHub Desktop.
Greska u graditelju
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Auto { | |
public: | |
int id; | |
Auto(int _id): id(_id) {} | |
}; | |
class Graditelj { | |
public: | |
virtual Auto* vrati() = 0; | |
}; | |
class Konkretan : public Graditelj { | |
private: | |
Auto* vozilo; | |
public: | |
Konkretan() : vozilo(new Auto(123)){} // Tad smo napravili taj auto i nikad vise | |
Auto* vrati() { return vozilo; } | |
}; | |
class Direktor { | |
protected: | |
Graditelj* graditelj; | |
public: | |
void postaviGraditelja(Graditelj* g) { | |
graditelj = g; | |
} | |
Auto* napraviAuto() { | |
return graditelj->vrati(); | |
} | |
}; | |
int main() { | |
Direktor* d = new Direktor(); | |
Konkretan* fabrika = new Konkretan(); | |
d->postaviGraditelja(fabrika); | |
Auto* a1 = d->napraviAuto(); | |
Auto* a2 = d->napraviAuto(); | |
a2->id = 5; // a2 == a1 | |
cout << a1->id << endl; // Nije 123 | |
cout << a2->id << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment