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

  • In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the...

    In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...

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

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

  • previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print...

    previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() {    //create Animal object    Animal animal("Dog","Mammal",4);    cout<<"Animal object details"<<endl;    cout<<"Name: "<<animal.getName()<<endl;    cout<<"Type: "<<animal.getType()<<endl;    cout<<"# of Legs: "<<animal.getLegs()<<endl;          cout<<endl<<endl;    //create Cat object    Cat cat("byssinian cat","carnivorous mammal",4,"short...

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

  • Design and implement the following 3 classes with the exact fields and methods (these names and c...

    Design and implement the following 3 classes with the exact fields and methods (these names and caps exactly): 1. An abstract class named BankAccount (java file called BankAccount.java) Description Filed/Method Balance NumberDeposits NumberWithdrawals AnnualInterestRate MonthlyServiceCharge BankAccount SetHonthlyServiceCharges A method that accepts the monthly service charges as an argument and set the field value GetBalance GetNum berDeposits GetNum berWithdrawals GetAnnualinterestRate GetMonthlyServiceCharge A method that returns the monthly service charge Deposit field for the bank account balance A field for the number...

  • C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1:...

    C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1: default constructor that initialized the attributes with proper default data of your choice. 2: destructor() method, that releases all the dynamically allocated memory. 3: copy constructor() method: That properly manages the dynamic array when the object is passed by value to a function as a parameter. 4: overload the assignment operator: To properly handle copying the dynamic array when one object is assigned to...

  • Implement a program that requests from the user a list of words (i.e., strings) and then...

    Implement a program that requests from the user a list of words (i.e., strings) and then prints on the screen, one per line, all four-letter strings in the list. >>> Enter word list: ['stop', 'desktop', 'top', 'post'] stop post An acronym is a word formed by taking the first letters of the words in a phrase and then making a word from them. For example, RAM is an acronym for random access memory. Write a function acronym() that takes a...

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