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:
2. Declare and define a bird class that is derived from animal:
4. Declare and define a canine class that is derived from animal:
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:

C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...
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 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 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 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 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 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 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 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: 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 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...