Question

C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...

C++ program to implement inheritance with following requirements,

Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :-

-You may need getters and setters also.

1. Declare and define an animal base class:

  • animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double).
  • animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set the alive boolean to true. The three parameter constructor should accept an int for the age and two doubles for the set of coordinates. The default should set these three values to 0.
  • animal requires a virtual move method which accepts two doubles that represent two coordinates, and ‘moves’ the animal to the set of coordinates.
  • animal requires a copy constructor and a virtual destructor. The destructor should be virtual.
  • animal requires a virtual sleep method and a virtual eat method. Both methods return void. Both methods should print an appropriate message to cout.
  • animal requires a setAlive function which accepts a boolean. This is a void function that changes the alive data member to the value of the boolean that is passed in.
  • Overload the insertion operator for the animal.

2. Declare and define a bird class that is derived from animal:

  • bird stores an extra value (a double) that represents its height coordinate. Land-lubbers have two coordinates. Something that flies stores 3. How you do this is your decision. Should ALL animals store 3 coordinates? If so, how can you make the height default to zero for other animals?
  • bird requires a default constructor and a 4 parameter constructor. The four-parameter constructor should accept an int for the age and three doubles for the set of coordinates. The default should set these four values to 0.
  • bird requires a move method which accepts three doubles that represent three coordinates, and ‘moves’ the bird to the set of coordinates. Consider: if a bird has a 3-param move function, but the animal base class version accepts 2 parameters, we’re not really overriding the original virtual 2-param version, and we won’t be able to ask birds to move if they are being referenced by an animal pointer. Do you need to modify the animal class, in view of this information?
  • bird requires a copy constructor and a destructor.
  • bird must override the sleep method and the eat method. Both methods return void. Both methods should print an appropriate message to cout.
  • Overload the insertion operator for the bird. Can you call the insertion operator for the base class from the derived class’ insertion operator? Investigate!

4. Declare and define a canine class that is derived from animal:

  • canine requires a default constructor and a 3 parameter constructor. The three-parameter constructor should accept an int for the age and two doubles for the set of coordinates. The default should set these three values to 0.
  • canine requires a move method which accepts two doubles that represent two coordinates, and ‘moves’ the canine to the set of coordinates.
  • canine requires a copy constructor and a destructor.
  • canine must override the sleep method and the eat method. Both methods return void. Both methods should print an appropriate message to cout.
  • canine requires a hunt method. The hunt method returns void. This method takes in an animal pointer as a parameter. The canine will set the other animal’s alive boolean to false if both animals are in the same position. Two animals are in the same position if the x, y, and z values are within 1 of each other. This method should print an appropriate message to cout depending on if the hunt was successful or not.
  • Overload the insertion operator for the canine. Can you call the insertion operator for the base class from the derived class’ insertion operator? Investigate!AAA
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your required code in C++:

#include <iostream>
#include <math.h>

using namespace std;

class animal{

public:
long id;
static int nextID;
  
int age;
bool alive;
  
//location
double x;
double y;
double z;
  
animal()
{
age = 0;
id = 0;
alive = 1;
x = 0;
y = 0;
z = 0;
id = ++nextID;
}
  
animal(int age, double x, double y)
{
this->age = age;
this->x = x;
this->y = y;
z = 0;
id = ++nextID;
}
  
virtual void move(double x, double y)
{
this->x = x;
this->y = y;
}
  
animal(const animal &a)
{
age = a.age;
id = a.id;
alive = a.alive;
x = a.x;
y = a.y;
z = 0;
}
  
virtual void sleep()
{
cout<<"Animal "<<id<<" is Sleeping.\n";
}
  
virtual void eat()
{
cout<<"Animal "<<id<<" is Eating.\n";
}
  
void setAlive(bool alive)
{
this->alive = alive;
}
  
friend ostream & operator << (ostream &out, const animal &a);

virtual ~animal()
{}
};

ostream &operator << (ostream &out, const animal &a)
{
out<<"ID: "<<a.id<<endl;
out<<"Age: "<<a.age<<endl;
out<<"Alive: ";
if(a.alive == 1)
out<<"Yes"<<endl;
else
out<<"No"<<endl;
out<<"Coordinates: "<<a.x<<" "<<a.y<<" "<<a.z<<endl;
out<<endl;
return out;
}

class bird : public animal
{
public:
bird()
{
age = 0;
x = 0;
y = 0;
z = 0;
}
  
bird(int age, double x, double y, double z)
{
this->age = age;
this->x = x;
this->y = y;
this->z = z;
}
  
void move(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
  
bird(const bird &b)
{
age = b.age;
id = b.id;
alive = b.alive;
x = b.x;
y = b.y;
z = b.z;
}
  
virtual ~bird(){}
  
virtual void sleep()
{
cout<<"Bird "<<id<<" is Sleeping.\n";
}
  
virtual void eat()
{
cout<<"Bird "<<id<<" is Eating.\n";
}
  
friend ostream & operator << (ostream &out, const bird &b);
};

ostream &operator << (ostream &out, const bird &b)
{
out<<"ID: "<<b.id<<endl;
out<<"Age: "<<b.age<<endl;
out<<"Alive: ";
if(b.alive == 1)
out<<"Yes"<<endl;
else
out<<"No"<<endl;
out<<"Coordinates: "<<b.x<<" "<<b.y<<" "<<b.z<<endl;
out<<endl;
return out;
}

class canine : public animal
{
public:

canine()
{
age = 0;
x = 0;
y = 0;
}
  
canine(int age, double x, double y)
{
this->age = age;
this->x = x;
this->y = y;
}
  
void move(double x, double y)
{
this->x = x;
this->y = y;
}
  
canine(const canine &c)
{
age = c.age;
id = c.id;
alive = c.alive;
x = c.x;
y = c.y;
}
  
virtual ~canine(){}
  
virtual void sleep()
{
cout<<"Canine "<<id <<" is Sleeping.\n";
}
  
virtual void eat()
{
cout<<"Canine "<<id <<" is Eating.\n";
}
  
void hunt(animal *a)
{
double d1, d2, d3;
d1 = abs(a->x - x);
d2 = abs(a->y - y);
d3 = abs(a->z - z);
  
if(d1<=1 && d2<=1 && d3<=1)
{
a->setAlive(0);
cout<<"Hunt Successful\n";
}
else
{
cout<<"Hunt Unsuccessful\n";
}
}
  
friend ostream & operator << (ostream &out, const canine &c);
};

ostream &operator << (ostream &out, const canine &c)
{
out<<"ID: "<<c.id<<endl;
out<<"Age: "<<c.age<<endl;
out<<"Alive: ";
if(c.alive == 1)
out<<"Yes"<<endl;
else
out<<"No"<<endl;
out<<"Coordinates: "<<c.x<<" "<<c.y<<" "<<c.z<<endl;
out<<endl;
return out;
}

//unique id starting from 101
int animal::nextID = 100;

int main()
{
//Testing different methods of classes above
animal a(5, 55.2, 33.5);
cout<<a;
  
bird b(10, 30, 15, 20);
cout<<b;
  
canine c(15, 2, 60);
cout<<c;
  
a.move(20, 50);
c.move(21, 49);
  
cout<<a<<c;
  
a.eat();
b.sleep();
c.sleep();
  
cout<<"\n";
animal *ani;
ani = &a;
c.hunt(ani);
return 0;
}

Here is the Output of some test cases:

Add a comment
Know the answer?
Add Answer to:
C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...
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
  • c++ program Implement a class called Person with the following members: 1a. ????, a private variable...

    c++ program Implement a class called Person with the following members: 1a. ????, a private variable of type ?????? 1b. ???, a private variable of type ??? 1c. Default constructor to set name to "" and age to 0 1d. Non-default constructor which accepts two parameters for name and age 1e. A copy constructor 1f. The post-increment operator 1g. The pre-decrement operator 1h. The insertion and extraction stream operators >>and <<

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and...

    Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and will add various functionality. We will store the following private variables specific to Warehouse: . a std::vector of float values which indicate the monetary values of each sale made by this salesman . a std::string which stores that salesman's position title . a float which stores their commission percentage, i.e., the percentage of sales they receive as a paycheck . a float which stores...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

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

  • Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1"...

    Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1" << endl; dq1.insertFront(42); dq1.insertBack(216); cout << dq1.peekFront() << " - 42" << endl; cout << dq1.peekBack() << " - 216" << endl; cout << dq1.size() << " - 2" << endl; Deque dq2(dq1); Deque dq3; dq3 = dq1; cout << dq1.removeFront() << " - 42" << endl; cout << dq1.removeBack() << " - 216" << endl; cout << dq2.peekFront() << " - 42" <<...

  • Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes....

    Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding in classes that are instantiated as integer classes, you may just use the default rounding. You will add an additional data member, method, and bank account type that inherits SavingsAc-count ("CD", or certicate of deposit). Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of...

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