I having trouble on running this code. I\'m lost now and seeking for helps:( The
ID: 3768113 • Letter: I
Question
I having trouble on running this code. I'm lost now and seeking for helps:(
These are the .cpp and .h files:
// animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
class animal
{
protected:
char sound[10]; // contains "moo", "quack"
unsigned char data:1; // 0 - cannot fly, 1 - can fly
public:
virtual void displaydata();
animal(char str[], bool fly);
~animal();
};
#endif
// animal.cpp
#include<iostream>
#include "animal.h"
using namespace std;
animal::animal(char str[], bool fly):data(0)
{
strcpy(sound, str);
data = data|fly;
}
void animal::displaydata()
{
cout <<"Animal sounds " << sound << " and it " << (data == 1?" Can Fly":" Can't Fly") << endl;
}
animal::~animal()
{
}
// bird.cpp
#include<iostream>
#include "bird.h"
using namespace std;
bird::bird(char str[], bool fly, int legs): animal(str, fly), numberoflegs(0)
{
strcpy(sound, str);
data = data|fly;
numberoflegs = numberoflegs|legs;
}
void bird::displaydata()
{
cout << "Bird Sounds: " << sound << " With number of legs " << numberoflegs << " and it " << (data == 1?" Can Fly":" Can't Fly") << endl;
}
bird::~bird()
{
}
// bird.h
#ifndef BIRD_H
#define BIRD_H
#include "animal.h"
class bird: public animal
{
private:
unsigned int numberoflegs:2;
public:
void displaydata();
bird(char str[], bool fly, int legs);
~bird();
};
#endif
// cow.cpp
#include<iostream>
#include "cow.h"
using namespace std;
cow::cow(char str[], bool fly, int legs): animal(str, fly)
{
strcpy(sound, str);
data = data|fly;
numberoflegs = numberoflegs|legs;
}
void cow::displaydata()
{
cout <<"Cow Sounds " << sound << " With number of legs " << numberoflegs << " and it" << (data == 1?" Can Fly":" Can't Fly") << endl;
}
// cow.h
#ifndef COW_H
#define COW_H
#include "animal.h"
class cow: public animal
{
private:
unsigned int numberoflegs:2;
public:
void displaydata();
cow(char str[], bool fly, int legs);
~cow();
};
#endif
// main.cpp
#include <iostream>
#include <vector>
#include <cstring>
#include "animal.h"
#include "bird.h"
#include "cow.h"
using namespace std;
void animaldisplaydata(animal *a)
{
a -> displaydata();
}
int main()
{
vector <animal*> animalList;
animal *ani = new animal("None", 0);
animalList.push_back(ani);
animal *COW = new cow("moo",0,2);
animalList.push_back(COW);
animal *BIRD = new bird("quack",1,2);
animalList.push_back(BIRD);
for(int i = 0; i < animalList.size(); i++)
animaldisplaydata(animalList[i]);
return 0;
}
Explanation / Answer
change this in animal.h
public:
virtual void displaydata(); // prints the value of sound and fly
animal (char *sound1, const int &fly1 ) ;
};