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 <<
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) 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 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...
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 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 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" << 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. 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...