Question

Language: C++ Reorganize your previous files (creature.cpp) content into three parts: declaration, imple- mentation of the c #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 : 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;

}

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

Creature.h

#ifndef CREATURES_H

#define CREATURES_H

#include <iostream>

using namespace std;

class Creature {

public:

Creature();

void run() const;

protected:

int distance;

};



class Wizard : public Creature {

public:

Wizard();

void hover() const;

private:

int distFactor;

};


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

};



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

};


#endif

----------------------------------------------------------------------------------------------------

Creature.cpp

#include <iostream>

#include "Creature.h"

using namespace std;

//constructor implementation

Widget::Widget() : clickedDistance(5)

{

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

}

//click method implementation

void Widget::click() const

{

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

}


//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";

}

Creature::Creature(): distance(10)

{}

void Creature::run() const

{

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

}

Wizard::Wizard() : distFactor(3)

{}

void Wizard::hover() const

{

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

}

//-------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------

testcreature.cpp

//#code for creature.cpp

#include <iostream>

using namespace std;

#include "Creature.h"

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;

}

--------------------------------------------------------------------------------------------------------

SEE OUTPUT

Files : main.cpp B saved - 1 //#code for creature.cpp https://RemorsefulVerifiable Profile.rahulkumar29.repl.rul main.cpp #in

PLEASE COMMENT if there is any concern.

==============================

Add a comment
Know the answer?
Add Answer to:
#code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...
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
  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...

    Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...

    Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height; int weight; public: void setHeight(int h){ height = h; } int getHeight(){ return height; } void setWeight(int w){ weight = w; } int getWeight(){ return weight; } virtual void makeSound() = 0; virtual void eat(); }; class Dog: public Animal{ public: void makeSound(){ cout << "Bow Bow\n"; } void eat (){ cout <<"The dog is eating\n"; } void Catch(){ cout << "The dog is...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • #include <iostream> #include <queue> using namespace std; class Graph { public: Graph(int n); ~Graph(); void addEdge(int...

    #include <iostream> #include <queue> using namespace std; class Graph { public: Graph(int n); ~Graph(); void addEdge(int src, int tar); void BFTraversal(); void DFTraversal(); void printVertices(); void printEdges(); private: int vertexCount; int edgeCount; bool** adjMat; void BFS(int n, bool marked[]); void DFS(int n, bool marked[]); }; Graph::Graph(int n=0) { vertexCount = n; edgeCount = 0; if(n == 0) adjMat = 0; else { adjMat = new bool* [n]; for(int i=0; i < n; i++) adjMat[i] = new bool [n]; for(int i=0;...

  • Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class...

    Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class Product { private:    // private variables    int id_number;    char pDescription[40];    int mId;    double productPrice;    double productMarkUp;    int qty; public:    // constructor    Product() {        id_number = 0;        strcpy_s(pDescription, "");        mId = 0;        productPrice = 0.0;        productMarkUp = 0.0;        qty = 0;    }   ...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

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
ADVERTISEMENT