I need to create a base class animal
and 3 other hereditary classes of other animals. Each class must have its own emitirSom
function. They must be polymorphically invoked through a vector. It turns out I do not know if the builder was done right. Type, should the constructor be done in the base class or inheritable classes?
Errormessages:
main.cpp(15):errorC2661:'dog::dog':nooverloadedfunctiongets2arguments
main.cpp(16):errorC2661:'cow::cow':nooverloadedfunctiongets2arguments
main.cpp(17):errorC2661:'cat::cat':nooverloadedfunctiongets2arguments
Header.h
#include<string>usingnamespacestd;classanimal{protected:stringnome;intidade;public:animal(conststring&nome_,intidade_);virtualvoidemitirSom()const=0;};classcachorro:publicanimal{public:virtualvoidemitirSom()const;};classvaca:publicanimal{virtualvoidemitirSom()const;};classgato:publicanimal{virtualvoidemitirSom()const;};
Implmentation.cpp
#include"stdafx.h"
#include "header.h"
#include <iostream>
#include <string>
using namespace std;
animal::animal(const string &nome_, int idade_)
{
nome = nome_;
idade = -idade_;
}
void cachorro::emitirSom() const
{
cout << "\n" << nome << "latindo: Aauauauauau";
}
void gato::emitirSom() const
{
cout << "\n" << nome << "miando: MiauMiau";
}
void vaca::emitirSom() const
{
cout << "\n" << nome << "mugindo: Muuuuuuu";
}
Main.cpp
#include "stdafx.h"
#include "header.h"
#include <vector>
void fazerBarulho(const animal* const &bicho)
{
bicho->emitirSom();
}
int main()
{
cachorro toby("Toby", 8);
vaca lili("Lili",29);
gato satanas("Satanas",3);
vector < animal * > zoo( 3 );
zoo[0] = &toby;
zoo[1] = &lili;
zoo[2] = &satanas;
for (size_t i = 0; i < zoo.size(); ++i)
fazerBarulho(zoo[0]);
#if WIN32
system("PAUSE");
#endif
}