Question

Please submit the following files for this assignment: **************************Animal.h, Animal.cpp, Mammal.h, Mammal.cpp, Cat.h, Cat.cpp, main.cpp******************************************************** Assignment...

Please submit the following files for this assignment:

**************************Animal.h, Animal.cpp, Mammal.h, Mammal.cpp, Cat.h, Cat.cpp, main.cpp********************************************************

Assignment 4 – OOP – inheritances

  1. Create a base class Animal (Animal.h, Animal.cpp)

Member variables (private):

string name;

string type;

Member methods (public):

Constructor(s)

Setters

Getters

eat(); //display “Eat food.”

  1. Create a derived class Mammal from Animal (public) (Mammal.h, Mammal.cpp)

Member variables (private):

string hair_type; //eg: fur, hair,etc

Member methods (public):

Constructor(s)

Setters

Getters

  1. Create a derived class Cat from Mammal (public) (Cat.h, Cat.cpp)

Member variables:

bool domesticated (private):

Member methods (public):

Constructor(s)

Setters

Getters

  1. main()(Main.h)

Instantiate a Cat object.

Set the following values to each of the member variables

  • name: “Abyssinian cat”
  • type: “carnivorous mammal”
  • hair_type: “short hair”
  • domesticated: true
  • eat () //display “Eat fish”

                                      //function overriding

*****************************************************************************************************************************************

************************************************************************************************************************

Key point:

Animal(int m_legs) {legs = m_legs;} //parameterized constructor in the base class Animal

Cat(int m_size,int m_legs):Animal(m_legs) {size = m_size; } //parameterized constructor in the derived class Cat

*****************************************************************************************************************************************************

please use Microsoft Visual Studio

C++

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

Code To Copy:

Animal.cpp

#include "Animal.h"
#include <string>
#include<iostream>

Animal::Animal(int age, std::string name) : _name(name), _age(age) { }

Animal::~Animal() { }

void Animal::sleep(){ printf("Shhh! %s is sleeping...\n", this->name); }

// Operator overloading

bool Animal::operator==(const Animal &animal){

return (this->age == animal.age()) && (this->name == animal.name());

}

std::ostream &operator<<(std::ostream &o, const Animal &animal){

return o << "Name: " << animal.name() << " Age: " << animal.age();

}

Animal.h

#include <string>

class Animal

{

private:

int _age;
std::string _name;

public:

Animal(int age, std::string name);
~Animal();

void sleep();
int age(void) const { return _age; }

std::string name() const { return _name; }

bool operator==(const Animal &animal); // the other animal is referenced by "this"
friend std::ostream &operator<<(std::ostream &o, const Animal &animal)

};

Cat.cpp

#include "Cat.h"
#include <string>
#include <iostream>

Cat::Cat(int age, std::string name, int weight) : Animal::Animal(age, name), _weight(weight) {}

Cat::~Cat()

{

}

void Cat::climb(){
std::cout << this->name() + " is climbing...";
}

std::ostream &operator<<(std::ostream &o, const Cat cat){
o << (Animal&)cat << " Weight: " << cat.weight();
return o;
}

bool Cat::operator==(const Cat &cat){
return (Animal)*this == (Animal)cat && this->weight() == cat.weight();
}

Cat.h

#include "Animal.h"
#include <string>

class Cat : public Animal

{

private:

int _weight;

public:

Cat(int age, std::string name, int weight);

~Cat();

int weight() const { return _weight; }
void climb();
bool operator==(const Cat &cat);

friend std::ostream &operator<<(std::ostream &o, const Cat &cat);

};

Dog.cpp

#include "Dog.h"
#include <iostream>
#include <string>

Dog::Dog(int age, std::string name, int numLifes) : Animal::Animal(age, name), _numLifes(numLifes) {}

Dog::~Dog() {};

void Dog::bark()

{ std::cout << ((Animal*)this)->name() + " says: \"ruff, ruff!\""; }

bool Dog::operator==(const Dog &dog){

return (Animal)*this == (Animal)dog && this->numLifes() == dog.numLifes();

}

std::ostream& operator<<(std::ostream &o, const Dog &dog){

o << (Animal)dog << " Number Of Lifes: " << dog.numLifes();

}

Dog.h

#include "Animal.h"
#include <string>
#include <iostream>

class Dog : public Animal{

private:

int _numLifes;

public:

Dog(int age, std::string name, int numLifes);

~Dog();

void bark();

int numLifes() const { return _numLifes; }

bool operator==(const Dog &dog);

friend std::ostream& operator==(std::ofstream& o, const Dog &dog);

};

Screenshots:

Add a comment
Know the answer?
Add Answer to:
Please submit the following files for this assignment: **************************Animal.h, Animal.cpp, Mammal.h, Mammal.cpp, Cat.h, Cat.cpp, main.cpp******************************************************** Assignment...
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
  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

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

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

  • Hi, the language is Java. Learning Outcomes and Introduction In this lab assignment you will practice:...

    Hi, the language is Java. Learning Outcomes and Introduction In this lab assignment you will practice: . applying access modifiers using getters and setters to mediate access using getters and setters to create derived attributes using static variables and methods testing code using a unit testing approach . For each of the tasks in this lab, you will create a new class or create an improved version of one of the classes from the previous lab. Remember to document all...

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

  • Create a C++ project with 2 classes, Person and Birthdate. The description for each class is...

    Create a C++ project with 2 classes, Person and Birthdate. The description for each class is shown below: Birthdate class: private members: year, month and day public members: copy constructor, 3 arguments constructor, destructor, setters, getters and age (this function should return the age) Person class: private members: firstName, lastName, dateOfBirth, SSN public members: 4 arguments constructor (firstName, lastName, datOfBirth and SNN), destructor and printout Implementation: - use the separated files approach - implement all the methods for the 2...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

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