Using Bloodshed Dev++ I\'ve created a program called \"Animals.\" In this progra
ID: 3629065 • Letter: U
Question
Using Bloodshed Dev++ I've created a program called "Animals." In this program 3 animals (a dog, cat, and snake) "speak". The program output should read:Woof! My name is Rover.
Meow! My name is Tabby.
Hiss! My name is Rattler.
My main portion of the program reads:
#include <cstdlib>
#include <iostream>
#include "Animals.h"
int main(int argc, char *argv[]){
TestAnimals();
system("PAUSE");
return 0;
}
my header file reads:
#ifndef ANIMALS_H
#define ANIMALS_H
#include<string>
class Animal{
public:
Animal (const std::string& name) : fName(name) {}
virtual void Speak () const = 0;
protected:
std::string fName;
};//class Animal
class Dog : public Animal {
public:
Dog (const std::string& name) : Animal (name) {}
virtual void Speak () const;
};// class Dog
class Cat : public Animal {
public:
Cat (const std::string& name) : Animal (name) {}
virtual void Speak () const;
};// class Cat
class Snake : public Animal {
public:
Snake (const std::string& name) : Animal (name) {}
virtual void Speak () const;
};// class Snake
void TestAnimals ();
#endif
And my program file reads:
#include "Animals.h"
#include <iostream>
#include<vector>
void
Dog::
Speak () const {
std::cout <<"Woof! My name is "<< fName <<"."<<std::endl;
}// Dog::Speak
void
Cat::
Speak () const {
std::cout <<"Meow! My name is "<< fName <<"."<<std::endl;
}// Cat::Speak
void
Snake::
Speak () const {
std::cout <<"Hiss! My name is "<< fName <<"."<<std::endl;
}// Snake::Speak
void TestAnimals (){
std:: < Animal* > animals;
animals.push_back (new Dog ("Rover"));
animals.push_back (new Cat ("Tabby"));
animals.push_back (new Snake ("Rattle"));
for(std::size_t = 0; i < animals.size (); ++i){
animals.at (i)-> Speak ();
}//for i
}// TestAnimals
I cannot get the program to compile properly. Please tell me what I'm doing wrong.
Explanation / Answer
// modified file shown below..
// remaining all same....
#include "Animals.h"#include
<iostream>#include<vector>
void Dog::Speak () const
{
std::cout <<"Woof! My name is "<< fName <<"."<<std::endl;}
// Dog::Speak
void Cat::Speak () const
{
std::cout <<"Meow! My name is "<< fName <<"."<<std::endl;
}
// Cat::Speak
void Snake::Speak () const
{
std::cout <<"Hiss! My name is "<< fName <<"."<<std::endl;
}// Snake::Speak
void TestAnimals ()
{
std::vector<Animal*> animals;
animals.push_back (new Dog ("Rover"));
animals.push_back (new Cat ("Tabby"));
animals.push_back (new Snake ("Rattle"));
for(std::size_t i= 0; i < animals.size (); ++i)
{
animals.at (i)-> Speak ();
}
//for i
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.