Question

In: Computer Science

Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()

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);

Solutions

Expert Solution


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++.


Related Solutions

public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT