T
Tony Johansson
Hello Experts!
I have some class definition and a main below.
This is a strange problem.
When cout is executing in the for loop below locating in main I get this
print out on the screen
Bird eats Bird food
Cat eats Birds
for(int i = 0; i<sizeof p/sizeof *p; i++)
cout << p->type() << " eats " << p->eats()->foodType() << endl;
The strange thing is that it doesn't matter what return type I have on the
eats method I always get
the same answer which is the same as above.
I mean these two gives the same output for class Bird when foodType() is
called in the for loop
BirdFood* eats()
PetFood* eats()
and these two gives the same output for class Cat when foodType() is called
in the for loop
CatFood* eats()
PetFood* eats()
Just to try to understand this I removed the virtual for this foodType
method in class PetFood.
Now I always get the same output which is
Bird eats Pet food
Cat eats Pet food.
it doesn't matter if I have
CatFood* eats()
or
PetFood* eats()
In class Cat.
I mean if I don't use virtual I will be using the static type and not the
dynamic type.
This would mean that if I return CatFood* eats()
I would be calling foodType in class CatFood and not in class PetFood.
//Class definitions
#include <iostream>
#include <string>
using namespace std;
class PetFood
{
public:
string foodType() const
{ return "Pet food"; }
};
class Pet
{
public:
virtual string type() const = 0;
virtual PetFood* eats() = 0;
};
class Bird : public Pet
{
public:
string type() const
{ return "Bird"; }
class BirdFood : public PetFood
{
public:
string foodType() const
{ return "Bird food"; }
};
PetFood* eats()
{ return &bf; }
private:
BirdFood bf;
};
class Cat : public Pet
{
public:
string type() const
{ return "Cat"; }
class CatFood : public PetFood
{
public:
string foodType() const
{ return "Birds"; }
};
CatFood* eats()
{ return &cf; }
private:
CatFood cf;
};
#include <iostream>
#include "pet.h"
using namespace std;
int main()
{
Bird b;
Cat c;
Pet* p[] = {&b, &c};
for(int i = 0; i<sizeof p/sizeof *p; i++)
cout << p->type() << " eats " << p->eats()->foodType() << endl;
return 0;
}
Many thanks
//Tony
I have some class definition and a main below.
This is a strange problem.
When cout is executing in the for loop below locating in main I get this
print out on the screen
Bird eats Bird food
Cat eats Birds
for(int i = 0; i<sizeof p/sizeof *p; i++)
cout << p->type() << " eats " << p->eats()->foodType() << endl;
The strange thing is that it doesn't matter what return type I have on the
eats method I always get
the same answer which is the same as above.
I mean these two gives the same output for class Bird when foodType() is
called in the for loop
BirdFood* eats()
PetFood* eats()
and these two gives the same output for class Cat when foodType() is called
in the for loop
CatFood* eats()
PetFood* eats()
Just to try to understand this I removed the virtual for this foodType
method in class PetFood.
Now I always get the same output which is
Bird eats Pet food
Cat eats Pet food.
it doesn't matter if I have
CatFood* eats()
or
PetFood* eats()
In class Cat.
I mean if I don't use virtual I will be using the static type and not the
dynamic type.
This would mean that if I return CatFood* eats()
I would be calling foodType in class CatFood and not in class PetFood.
//Class definitions
#include <iostream>
#include <string>
using namespace std;
class PetFood
{
public:
string foodType() const
{ return "Pet food"; }
};
class Pet
{
public:
virtual string type() const = 0;
virtual PetFood* eats() = 0;
};
class Bird : public Pet
{
public:
string type() const
{ return "Bird"; }
class BirdFood : public PetFood
{
public:
string foodType() const
{ return "Bird food"; }
};
PetFood* eats()
{ return &bf; }
private:
BirdFood bf;
};
class Cat : public Pet
{
public:
string type() const
{ return "Cat"; }
class CatFood : public PetFood
{
public:
string foodType() const
{ return "Birds"; }
};
CatFood* eats()
{ return &cf; }
private:
CatFood cf;
};
#include <iostream>
#include "pet.h"
using namespace std;
int main()
{
Bird b;
Cat c;
Pet* p[] = {&b, &c};
for(int i = 0; i<sizeof p/sizeof *p; i++)
cout << p->type() << " eats " << p->eats()->foodType() << endl;
return 0;
}
Many thanks
//Tony