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

Study Guide:

You may use the following class demo code as your reference.

// OOP Inheritance demo 2/28/20
#include
#include

class Animal {
private:
int legs;

public:
Animal() { legs = 0; } //default constructor
Animal(int m_legs) {legs = m_legs;} //parameterized constructor
void run() { std::cout << "Animal can run.\n";}
void setLegs(int i) {legs = i;} //setter, mutator
int getLegs() const {return legs;} //getter, accessor
//const here guarantees that the logic state of your object will not change
//Comparing: const int getLegs(); //const here means the return type is a const int
};

class Cat : public Animal {//is-a
int size;
public:
Cat(int m_size,int m_legs):Animal(m_legs) {size = m_size; } //parameterized constructor
int getSize() { return size;}
void run() { std::cout<<"Cat can run.\n"; } //function overriding. Same function signature than the one in the base class
};


int main()
{
Cat myCat(20,4);
std::cout << myCat.getSize() << "\n"
<< myCat.getLegs() << "\n";

myCat.run();
  
return 0;
  
}

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

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

};

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
  • 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 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.” 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 Create a derived class Cat from Mammal (public) (Cat.h, Cat.cpp) Member...

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

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

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

  • Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...

    Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height; int weight; public: void setHeight(int h){ height = h; } int getHeight(){ return height; } void setWeight(int w){ weight = w; } int getWeight(){ return weight; } virtual void makeSound() = 0; virtual void eat(); }; class Dog: public Animal{ public: void makeSound(){ cout << "Bow Bow\n"; } void eat (){ cout <<"The dog is eating\n"; } void Catch(){ cout << "The dog is...

  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

  • My main() file does not call to the class created in a .hpp file. It comes...

    My main() file does not call to the class created in a .hpp file. It comes out with the error "undefined reference to "___" const. I want to solve this by only changing the main() .cpp file, not the .hpp file. .cpp file .hpp file Thank you! include <iostream> include <string> tinclude "CourseMember.hpp using namespace std int main0 CourseMember CourseMember(90, "Jeff", "Goldblum"): // initializing the constructor cout<< "Member ID is <<CourseMember.getID) << endl; 1/ calling getID) accessor cout <<"First Name...

  • Create a base class and two derived classes. Also, create and call a member function named...

    Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...

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