In: Computer Science
Given:
class Monster
{
private:
string name;
int dangerLevel;
public:
Monster(sting, int);
virtual void hunt() = 0;
virtual void fight(Monster&);
string getName() const;
};
class GiantMonster : public Monster
{
protected:
int height;
public:
GiantMonster(string, int, int);
virtual void trample();
};
class Dinosaur : public GiantMonster
{
public:
Dinosaur(string, int, int);
void hunt();
void roar();
};
class Kraken : protected GiantMonster
{
public:
Kraken(string, int, int);
virtual void hunt();
void sinkShip();
};
Indicate if the code snippets below are valid or invalid:
a)
GiantMonster * m = new Kraken (“Sea Horror”, 500, 100);
b)
Kraken * k = new Kraken(“Cthulu”, 500, 100);
Cout << k->getName() << endl;
c)
Dinosaur trex(“T-Rex”, 100, 70);
GiantMonster m = trex;
m.roar();
d)
Monster * m = new GiantMonster (“Godzilla”, 100, 300);
e)
Dinosaur dino ( “Triceratops”, 0, 150);
Dinosaur trex ( “T-rex”, 200, 120);
Trex.fight(dino);
f)
Dinosaur * d = new Dinosaur (“T-Rex”, 100, 50);
GiantMonster * g = new Dinosaur (“Godzilla”, 100, 500);
g->fight(*d);
g)
Dinosaur * d = newGiantMonster(“T-Rex”, 100, 50);
a)
GiantMonster * m = new Kraken (“Sea Horror”, 500, 100);
This statement is invalid.
Because here base class is GiantMonster which is
inaccessible.
So the conversion from Kraken to inaccessible base class
GiantMonster is not allowed.
b)
Kraken * k = new Kraken(“Cthulu”, 500, 100);
Cout << k->getName() << endl;
This code snippet is invalid.
Because getName is in base class Monster. From GiantMonster getName
is accessible. But Kraken can access only protected members of
GiantMonster.
so for object k getName is inaccessible.
c)
Dinosaur trex(“T-Rex”, 100, 70);
GiantMonster m = trex;
m.roar();
This code snippet is invalid.
Because object of abstract class type "GiantMonster" is not allowed
in c++.
Note :
GiantMonster is abstract class because this class has not
Overriden(re-defined) "hunt" function from base class
Monster.
hunt is a Pure Virtual Function and it needs to be implemented in
GiantMonster.
Additionally to convert this snippet into valid one we need
following code.
Dinosaur trex("T - Rex", 100, 70);
Dinosaur m = trex;
m.roar();
d)
Monster * m = new GiantMonster (“Godzilla”, 100, 300);
This code snippet is invalid.
Because object of abstract class type "GiantMonster" is not allowed
in c++.
e)
Dinosaur dino ( “Triceratops”, 0, 150);
Dinosaur trex ( “T-rex”, 200, 120);
Trex.fight(dino);
I am not sure if there is a spelling mistake for 3rd line.
But if the statement at 3rd line is Trex.fight(dino); then the code
snippet is invalid because variable names are case
sensitive.
But if the statement at 3rd line is starts with small 't' i.e.
trex.fight(dino); then the statement is valid.
f)
Dinosaur * d = new Dinosaur (“T-Rex”, 100, 50);
GiantMonster * g = new Dinosaur (“Godzilla”, 100, 500);
g->fight(*d);
This code snippet is valid.
g)
Dinosaur * d = newGiantMonster(“T-Rex”, 100, 50);
This code snippet is invalid.
Because object of abstract class type "GiantMonster" is not allowed
in c++.