Question

Extend the example program creature.cpp by adding two other type of creatures (i.e., two new classes...

Extend the example program creature.cpp by adding two other type of creatures (i.e., two new classes derived from Creature) with some example properties and methods (use your imagination for doing this). Use/implement at least one property and at least one method for each derived class. Each constructor, destructor and method should be implemented in such a way that each call is being documented via messages printed on the screen. Create one instance of Wizard, two instances of the two other classes, and call for each object methods of the particular instances (as in the original example).

#CODE FOR CREATURE PROGRAM

#include <iostream>

using namespace std;

class Creature {
   public:
       Creature();
       void run() const;

   protected:
       int distance;
};

Creature::Creature(): distance(10)
{}

void Creature::run() const
{
cout << "running " << distance << " meters!\n";
}

class Wizard : public Creature {
   public:
       Wizard();
       void hover() const;

   private:
       int distFactor;
};

Wizard::Wizard() : distFactor(3)
{}

void Wizard::hover() const
{
cout << "hovering " << (distFactor * distance) << " meters!\n";
}

int main()
{
cout << "Creating an Creature.\n";
Creature c;
c.run();

cout << "\nCreating a Wizard.\n";
Wizard w;
w.run();
w.hover();

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query do comment. Please rate your answer. Thanks

Code:

#include <iostream>

using namespace std;

class Creature {

   public:

       Creature();

       void run() const;

   protected:

       int distance;

};

Creature::Creature(): distance(10)

{}

void Creature::run() const

{

cout << "running " << distance << " meters!\n";

}

class Wizard : public Creature {

   public:

       Wizard();

       void hover() const;

   private:

       int distFactor;

};

Wizard::Wizard() : distFactor(3)

{}

void Wizard::hover() const

{

cout << "hovering " << (distFactor * distance) << " meters!\n";

}

//Created new derived class from Creature

class Widget : public Creature{

    public:

        //constructor

        Widget();

        //new method added as click

        void click() const;

    private:

        //new data member

        int clickedDistance;

};

//constructor implementation

Widget::Widget() : clickedDistance(5)

{

    cout << "\nCreating a Widget.\n";   

}

//click method implementation

void Widget::click() const

{

    cout << "Clicked " << clickedDistance + distance << " meters\n";

}

//Created new derived class from Creature

class TextCreature : public Creature{

    public:

       //constructor

        TextCreature();

        //new method added as Moved

        void moved() const;

    private:

        //new data member

        int byDistance;

};

//implementation of TextCreature methods including constructor

TextCreature::TextCreature() : byDistance(3)

{

    cout <<"\nCreating a text Creature.\n";

}

void TextCreature::moved() const

{

    cout << "moved " << byDistance + distance << " meters\n";

}

int main()

{

cout << "\nCreating an Creature.\n";

Creature c;

c.run();

cout << "\nCreating a Wizard.\n";

Wizard w;

w.run();

w.hover();

//create an object of Widget class

Widget wdg;

//calling base class method

wdg.run();

//calling newly added method

wdg.click();

//create an object of TextCreature class

TextCreature txt;

//calling base class method

txt.run();

//calling newly added method

txt.moved();

return 0;

}

============screen shot of code==========

Output:

Add a comment
Know the answer?
Add Answer to:
Extend the example program creature.cpp by adding two other type of creatures (i.e., two new classes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • Please submit the following files for this assignment: *****************Animal.h, Animal.cpp, Mammal.h, Mammal.cpp, Cat.h, Cat.cpp, main.cpp********* Assignment...

    Please submit the following files for this assignment: *****************Animal.h, Animal.cpp, Mammal.h, Mammal.cpp, Cat.h, Cat.cpp, main.cpp********* Assignment 4 – OOP – inheritances Create a base class Animal (Animal.h, Animal.cpp) Member variables (private): string name; string type; Member methods (public): Constructor(s) Setters Getters eat(); //display “Eat food.” Create a derived class Mammal from Animal (public) (Mammal.h, Mammal.cpp) Member variables (private): string hair_type; //eg: fur, hair,etc Member methods (public): Constructor(s) Setters Getters Create a derived class Cat from Mammal (public) (Cat.h, Cat.cpp) Member...

  • Define an example of inheritance with a base class that has 2 attributes, 2 derived classes...

    Define an example of inheritance with a base class that has 2 attributes, 2 derived classes that each have 2 attributes. Implement this in C++. Create a driver program that creates instances of both of the derived classes and exercises the classes’ member functionsl. Student answer should include the following: 1 base class with 2 attributes, 2 constructors (default and with parameters), get/set methods for each attribute, and display methods 2 derived classes with 2 attributes, 2 constructors (default and...

  • Create a base class and two derived classes. Also, create and call a member function named...

    Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...

  • 2. In the following program an employee of a company is represented by an object of...

    2. In the following program an employee of a company is represented by an object of type employee consisting of a name, employee id, employee salary and the workday starting time. The starting time is a class time 24 object. Implement the class employee. Declaration of Employee and Time Classes /* File : employeetime.h Hlustrates composition of classes */ #ifndef EMPLOYEETIME.H #define EMPLOYEETIME.H #include <iostream> #include <string> using namespace std; class time 24 { private : int hour; int minute...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Instructions: Consider the following C++ program. At the top you can see the interface for the...

    Instructions: Consider the following C++ program. At the top you can see the interface for the Student class. Below this is the implementation of the Student class methods. Finally, we have a very small main program. #include <string> #include <fstream> #include <iostream> using namespace std; class Student { public: Student(); Student(const Student & student); ~Student(); void Set(const int uaid, const string name, const float gpa); void Get(int & uaid, string & name, float & gpa) const; void Print() const; void...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT