Coding Challenge: Bark Dog, Bark!
Problem Statement
The Animal
base class has been provided for you in the Animal.h
header file. This class contains two pure virtual methods:
get_noise()
: Returns a string representing the noise that the animal makes.
get_num_legs()
: Returns an integer representing the number of legs the animal has.
Additionally, the Animal
class contains a constructor that accepts the name and age of the animal, as well as methods to retrieve these properties:
Animal()
: A constructor that accepts a string for the name and an integer for the age.
get_name()
: Returns the name of the animal.
get_age()
: Returns the age of the animal.
Your task is to implement a Dog
class that inherits from Animal
. The Dog
class should have a Dog.h
file that contains the Dog
class declaration, and a Dog.cpp
file for the Dog
class implementation.
You need to override the get_noise()
and get_num_legs()
methods, and provide a constructor for Dog
that takes in a name and an age:
get_noise()
: Should return the string "Woof"
.
get_num_legs()
: Should return the integer 4
.
Dog()
: This is the constructor that should accept a string for the name and an integer for the age. It should pass these values to the base class (Animal
) constructor.
Input Format
There is no input for this task.
Constraints
The Dog
class must inherit from the Animal
class, override the required methods, and correctly implement its constructor.
How will I test your Code?
Your program should pass the following tests:
Dog dog{"Spot", 5}; std::string noise = dog.get_noise(); // Woof int legs = dog.get_num_legs(); // 4 int age = dog.get_age(); // 5 std::string name = dog.get_name(); // Spot
Your program should also pass the following tests that use a pointer to Dog and a Base class pointer:
Dog *dog_ptr = new Dog{"Spot", 5}; std::string noise = dog_ptr->get_noise(); // Woof int legs = dog_ptr->get_num_legs(); // 4 int age = dog_ptr->get_age(); // 5 std::string name = dog_ptr->get_name(); // Spot
Animal *animal_ptr = new Dog{"Spot", 5}; std::string noise = animal_ptr->get_noise(); // Woof int legs = animal_ptr->get_num_legs(); // 4 int age = animal_ptr->get_age(); // 5 std::string name = animal_ptr->get_name(); // Spot
}
#include "gtest/gtest.h"
#include "helpers/iohelper.h"
#include "Dog.h"
namespace {
class Evaluate : public ::testing::Test {
protected:
Evaluate() {
// You can do set-up work for each test here.
}
virtual ~Evaluate() {
// You can do clean-up work that doesn't throw exceptions here.
}
// Objects declared here can be used by all tests in the test case for Foo.
};
TEST_F(Evaluate, DogTest) {
Dog dog{"Spot", 5};
std::string noise = dog.get_noise();
int legs = dog.get_num_legs();
int age = dog.get_age();
std::string name = dog.get_name();
ASSERT_EQ(noise, "Woof");
ASSERT_EQ(legs, 4);
ASSERT_EQ(name, "Spot");
ASSERT_EQ(age, 5);
}
TEST_F(Evaluate, DogPointerTest) {
Dog *dog_ptr = new Dog{"Spot", 5};
std::string noise = dog_ptr->get_noise();
int legs = dog_ptr->get_num_legs();
int age = dog_ptr->get_age();
std::string name = dog_ptr->get_name();
ASSERT_EQ(noise, "Woof");
ASSERT_EQ(legs, 4);
ASSERT_EQ(name, "Spot");
ASSERT_EQ(age, 5);
}
TEST_F(Evaluate, AmimalPointerTest) {
Animal *animal_ptr = new Dog{"Spot", 5};
std::string noise = animal_ptr->get_noise();
int legs = animal_ptr->get_num_legs();
int age = animal_ptr->get_age();
std::string name = animal_ptr->get_name();
ASSERT_EQ(noise, "Woof");
ASSERT_EQ(legs, 4);
ASSERT_EQ(name, "Spot");
ASSERT_EQ(age, 5);
}
} // namespace
/*****************************/
/** DO NOT MODIFY THIS FILE **/
/*****************************/
// Note that we do NOT need an Animal.cpp file since we variable
// implementing the get_name() and get_age() methods inline here
#ifndef ANIMAL_H
#define ANIMAL_H
#include
// Define the base class Animal
class Animal {
protected:
std::string name; // Member variable for the animal's name
int age; // Member variable for the animal's age
public:
// Constructor for Animal class that initializes name and age
Animal(const std::string& name, int age) : name{name}, age{age} {}
// Pure virtual function for getting the noise an animal makes
virtual std::string get_noise() = 0;
// Pure virtual function for getting the number of legs an animal has
virtual int get_num_legs() = 0;
// Function for getting the name of the animal
virtual std::string get_name() { return name; }
// Function for getting the age of the animal
virtual int get_age() { return age; }
};
#endif
#ifndef DOG_H
#define DOG_H
#include "Animal.h"
class Dog : public Animal { // Inheritance Dog is-an Animal
public:
// Be sure to call the Animal constructor from the Dog constuctor
Dog(const std::string& name, int age) : Animal(name, age) {}
std::string get_noise() override;
int get_num_legs() override;
};
#endif /* DOG_H */
#include "Dog.h"
// Implement Dog::get_noise()
std::string Dog::get_noise() {
return "Woof";
}
// Implement Dog::get_num_legs()
int Dog::get_num_legs() {
return 4;
}
// Dog.h
#ifndef DOG_H
#define DOG_H
#include "Animal.h"
class Dog : public Animal { // Inheritance Dog is-an Animal
public:
// Be sure to call the Animal constructor from the Dog constuctor
Dog(const std::string& name, int age) : Animal(name, age) {}
std::string get_noise() override;
int get_num_legs() override;
};
#endif /* DOG_H */
// Dog.cpp
#include "Dog.h"
// Implement Dog::get_noise()
std::string Dog::get_noise() {
return "Woof";
}
// Implement Dog::get_num_legs()
int Dog::get_num_legs() {
return 4;
}