各位,小弟虚函数不好,请大家来看看
4个文件
animal.h
#include <iostream.h>
#include <string.h>
class animal
{
char *pp=new char[20];
public:
animal(char *);
virtual ~animal();
virtual void fn();
};
animal::animal(char *Tp="noname")
{
strcpy(pp,Tp);
}
void animal::fn(){cout<<"animal can swell"<<endl;}
animal::~animal()
{
delete pp;
cout<<"animal will dead"<<endl;
}
dog.h
#include <iostream.h>
#include <string.h>
#ifndef ANIMAL
#define ANIMAL
#include "animal.h"
#endif
class dog: pubilc animal
{
int m_age=new int;
public:
dog(char *);
virtual ~dog();
virtual void fn();
};
dog::dog(int tm_age=0):animal("noname")
{
m_age=tm_age;
}
void dog::fn(){cout<<"dog can swell"<<endl;}
dog::~dog()
{
delete m_age;
cout<<"dog will dead"<<endl;
}
cat.h
#include <iostream.h>
#include <string.h>
#ifndef ANIMAL
#define ANIMAL
#include "animal.h"
#endif
class cat: pubilc animal
{
int m_age=new int;
public:
cat(char *);
virtual ~cat();
virtual void fn();
};
cat::cat(int tm_age=0):animal("noname")
{
m_age=tm_age;
}
void cat::fn(){cout<<"cat can swell"<<endl;}
cat::~cat()
{
delete m_age;
cout<<"cat will dead"<<endl;
}
main.cpp
#include <iostream.h>
#include "cat.h"
#include "dog.h"
void text (animal &tt);
void main()
{
animal n1;
dog n2;
cat n3;
}
void text (animal &tt)
{
tt.fn();
}
请帮忙