For this assignment you will be tasked with creating several classes In C++. 3 a
ID: 3849669 • Letter: F
Question
For this assignment you will be tasked with creating several classes In C++. 3 abstract classes and 3 concrete classes. The base class will be class Animal. This will be an abstract class following are its required specifications
• public static variable count that counts number of animals created
• default constructor that adds one to the count.
• pure virtual function speak() that returns a string.
There will be an class Canine. It inherits from Animal.
• public static variable, count, that counts number of Canines created.
• default constructor that adds one to the count.
• implement the speak() to return string “WOOF”.
There will be an class Feline. It also inherits from Animal.
• public static variable, count, that counts number of Felines created
• default constructor that adds one to the count.
• implement the speak() to return string “PURR”.
There will be a class Dog. It inherits from Canine.
• private string variable called name.
• default constructor that sets the name variable to “dog”.
• constructor that takes a single string parameter and sets that parameter as the name.
• getName function that returns the name of the Dog.
There will be a class Wolf. It inherits from Canine.
• default constructor that is empty.
• howl function that returns a string “HOWL”.
There will be a class Cat. It inherits from Feline.
• private string variable called name.
• default constructor that sets the name variable to “cat”.
• constructor that takes a single string parameter and sets the parameter as the name.
• getName function that returns the name of the Cat.
Some things to note:
• Note there is no setName function. This is intentional. The logic is that a Dog/Cat’s name is set only once and will never be changed
. • Please note that base class default constructors are called before the beginning of a derived class’s constructor.
This means you only need to handle static counts for their respective classes (so you do not have to worry about Animal count from the Canine class).
Please also create a main.cpp file that will test the following:
• Create a Dog, Wolf and Cat, using only default constructors.
• Create a Dog and Cat with a name.
• Create an Animal * array and instantiate and store at least a Dog, a Cat and a Wolf.
• Use for loop to call speak on each of the animals stored in Animal array.
• Call howl function.
• Show all counts for Animal, Canine and Feline
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class animal
{
public:
static int count;
animal()
{
count++;
}
virtual string speak()=0;
};
int animal::count=0;
class canine: public animal
{
public:
static int count;
canine()
{
count++;
}
string speak()
{
return "WOOF";
}
};
int canine::count=0;
class feline: public animal
{
public:
static int count;
feline()
{
count++;
}
string speak()
{
return "PURR";
}
};
int feline::count=0;
class dog: public canine
{
private:
string name;
public:
dog()
{
name="dog";
}
dog(string n)
{
name=n;
}
string getName()
{
return name;
}
};
class wolf: public canine
{
public:
wolf(){}
string howl()
{
return "HOWL";
}
};
class cat: public feline
{
private:
string name;
public:
cat()
{
name="cat";
}
cat(string n)
{
name=n;
}
string getName()
{
return name;
}
};
**************************************main.cpp**********************************88
int main()
{
dog d1;
cat c1;
wolf w1;
dog d2("Tommy");
cat c2("Billy");
animal *a[]={&d1,&c1,&w1};
for(int i=0;i<3;i++)
{
string s=a[i]->speak();
cout<<s<<endl;
}
w1.howl();
cout<<"Count of animals: "<<animal::count<<endl;
cout<<"Count of canines: "<<canine::count<<endl;
cout<<"Count of felines: "<<feline::count<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.