#include "iostream.h"
#include "string.h"
/*首先定义一个动物类作为基类*/
class Dongwu
{
protected:
char* Name;
char* Aihao;
public:
Dongwu();
~Dongwu();
void SetName(char* name);
void SetAihao(char* aihao);
void xianshi();
};
Dongwu::Dongwu()
{
Name=new char[20];
Aihao=new char[20];
}
Dongwu::~Dongwu() //注意要回收动态创建的空间
{
delete [] Name;
delete [] Aihao;
}
void Dongwu::SetName(char* name)
{
strcpy(Name,name);
}
void Dongwu::SetAihao(char* aihao)
{
strcpy(Aihao,aihao);
}
void Dongwu::xianshi()
{
cout<<"基类显示:"<<'\t';
cout<<"动物类中,一个名为:"<<Name<<"的未知的动物,爱好:"<<Aihao<<'\n';
}
/*创建一个继承与动物类的 狗类 Dog类*/
class Dog:public Dongwu
{
protected:
int* Age;
public:
Dog();
~Dog();
void DogAge(int* age);
void Dogxianshi();
};
Dog::Dog()
{
Age=new int; //这里是这样子
Name=new char[20];
Aihao=new char[20];
}
Dog::~Dog() //注意要回收动态创建的空间
{
delete Age;
}
void Dog::DogAge(int* age)
{
*Age=*age; //这里是这样子
}
void Dog::Dogxianshi()
{
cout<<"Dog中显示"<<'\t';
cout<<"狗类中,一个名为:"<<Name<<"的狗,平时喜欢:"<<Aihao<<"今年:"<<*Age<<"岁了"<<'\n'; //这里是这样子
}
int main(int argc, char* argv[])
{
Dog dog;
dog.SetName("皮皮");
dog.SetAihao("咬人");
int a=20; //这里是这样子
dog.DogAge(&a); //这里是这样子
dog.xianshi();
dog.Dogxianshi();
return 0;
}
[bo]PS:[/bo]
皮皮真的是很长寿啊……
#include "string.h"
/*首先定义一个动物类作为基类*/
class Dongwu
{
protected:
char* Name;
char* Aihao;
public:
Dongwu();
~Dongwu();
void SetName(char* name);
void SetAihao(char* aihao);
void xianshi();
};
Dongwu::Dongwu()
{
Name=new char[20];
Aihao=new char[20];
}
Dongwu::~Dongwu() //注意要回收动态创建的空间
{
delete [] Name;
delete [] Aihao;
}
void Dongwu::SetName(char* name)
{
strcpy(Name,name);
}
void Dongwu::SetAihao(char* aihao)
{
strcpy(Aihao,aihao);
}
void Dongwu::xianshi()
{
cout<<"基类显示:"<<'\t';
cout<<"动物类中,一个名为:"<<Name<<"的未知的动物,爱好:"<<Aihao<<'\n';
}
/*创建一个继承与动物类的 狗类 Dog类*/
class Dog:public Dongwu
{
protected:
int* Age;
public:
Dog();
~Dog();
void DogAge(int* age);
void Dogxianshi();
};
Dog::Dog()
{
Age=new int; //这里是这样子
Name=new char[20];
Aihao=new char[20];
}
Dog::~Dog() //注意要回收动态创建的空间
{
delete Age;
}
void Dog::DogAge(int* age)
{
*Age=*age; //这里是这样子
}
void Dog::Dogxianshi()
{
cout<<"Dog中显示"<<'\t';
cout<<"狗类中,一个名为:"<<Name<<"的狗,平时喜欢:"<<Aihao<<"今年:"<<*Age<<"岁了"<<'\n'; //这里是这样子
}
int main(int argc, char* argv[])
{
Dog dog;
dog.SetName("皮皮");
dog.SetAihao("咬人");
int a=20; //这里是这样子
dog.DogAge(&a); //这里是这样子
dog.xianshi();
dog.Dogxianshi();
return 0;
}
[bo]PS:[/bo]
皮皮真的是很长寿啊……
生活就是一个七日接着又一个七日