Calling method within classess
Jun 16, 2011 at 5:37pm UTC
Hello everyone. Simple question for the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Story {
public :
Sheep* SHEEP;
Persons PERSONS [2];
Bajka() {
PERSONS[0] = Dragon();
PERSONS[1] = Girl();
tellStory();
}
void tellStory() {
}
};
How can I reffer to the methods from PERSONS[0] or PERSONS[1] (Dragon and Girl classes which inherits from Persons class)? Because I have no idea how to do this.
Jun 16, 2011 at 5:41pm UTC
PERSONS[0].Method()
Jun 16, 2011 at 5:48pm UTC
Then compiler says: 'class Persons' has no member named 'myMethod'.
I recall. Dragon and Girl inherits from Persons class.
Last edited on Jun 16, 2011 at 5:48pm UTC
Jun 16, 2011 at 5:55pm UTC
Do they actually inherit from the 'Persons' class or do they just have Persons data members? There is a difference.
Is it public, private or protected inheritence? At what level are the member functions from the 'Persons' class?
There are a dozen questions here you should answer, or you could post the other three classes.
Jun 16, 2011 at 6:09pm UTC
Currently, it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include <iostream>
#include <String>
using namespace std;
class Postac {
public :
string imie;
virtual void peknij() {};
};
extern Postac POSTACIE [2];
class Owca {
public :
Postac wlasciciel;
bool zafaszerowana;
Owca() {
zafaszerowana = false ;
}
~Owca() {
}
};
class Smok : public Postac {
public :
bool najedzony;
Smok() {
imie = "Smok" ;
najedzony = false ;
}
~Smok() {
}
void jedzOwce(Owca OWCA) {
najedzony = true ;
cout << POSTACIE[0].imie << " pozarl owce w calosci." << endl;
}
void peknij() {
cout << POSTACIE[0].imie << " peknal sobie. Jaka szkoda." << endl;
}
};
class Dratewka : public Postac {
public :
Dratewka() {
imie = "Dratewka" ;
}
~Dratewka() {
}
void faszeruj(Owca* OWCA) { // dzialamy na wskazniku, nie kopii
OWCA->zafaszerowana = true ;
cout << POSTACIE[1].imie << " zafaszerowal owieczke zabojcza trucizna." << endl;
}
void daj(Owca* OWCA) {
OWCA->wlasciciel = POSTACIE[0];
cout << POSTACIE[1].imie << " zlozyl swoj podarunek." << endl;
cout << "Owieczka zmienila wlasciciela na " << OWCA->wlasciciel.imie << "." <<endl;
}
};
class Bajka {
public :
Owca* OWCA;
Postac POSTACIE [2];
Bajka() {
POSTACIE[0] = Smok();
POSTACIE[1] = Dratewka();
OWCA->wlasciciel = POSTACIE[1];
cout << "Obecnie wlascicielem owieczki jest " << OWCA->wlasciciel.imie << "." <<endl;
opowiedzBajke();
}
void opowiedzBajke() {
POSTACIE[0]::peknij();
}
};
int main()
{
new Bajka();
//Bajka* bajka = new Bajka();
//cout << "Koniec bajki.";
//delete bajka;
return 0;
}
Dratewka crams sheep with the poison and gives it to the Dragon. Dragon eats that poisoned sheep, and dies. Program suppose to tell that story.
Topic archived. No new replies allowed.