注册 登录
编程论坛 C++教室

我是新手,请高手指教!!

Forever_U 发布于 2014-04-23 19:24, 580 次点击
调试时都没有错误,就是不能运行!!!为什么呢??

#include"iostream.h"
#include"string.h"
class Animal  //基类名 Animal 的声明
{
private:      //私有数据成员
    int age;
    char *dogname;
public:  //共有函数成员
     Animal(char *dogming,int in_age)
     {
          dogname=new char[strlen(dogming)+1];  // 测试接收数据长度+1
           strcpy(dogname,dogming);     //实现赋值
            age=in_age;               

}
   
     int getage(){return age;}  //返回值 动物的年龄
     char *getname(){return dogname;}  //返回 动物的名字
};


class Dog:public Animal  //派生类 Dog 类的声明和共有继承
{
private:       //新增私有函数
    char *birthplace;
public:     //新增共有函数 birtplace
    Dog(char *dogming, int in_age,char *bir):Animal (dogming,in_age) //调用基类Animal 的共有的函数成员
    {
    birthplace=new char[strlen(birthplace)+1];  //测试长度+1
       strcpy(birthplace,bir);   //赋值
       }

    char *getbirt(){return birthplace;}  //返回 birtplace
};
    void main()
    {
        Dog t("dog",1,"chain");  
        cout<<"狗狗资料:\n小狗名\t年龄\t出生地"<<endl;
        cout<<t.getname()<<"\t"<<t.getage()<<"\t"<<t.getbirt()<<endl;
}
1 回复
#2
a3893503182014-05-03 18:53
你在这一部分出错误了
 Dog(char *dogming, int in_age,char *bir):Animal (dogming,in_age) //调用基类Animal 的共有的函数成员
    {
    birthplace=new char[strlen(birthplace)+1];  //测试长度+1
       strcpy(birthplace,bir);   //赋值
       }
将其改成这样就可以了
 Dog(char *dogming, int in_age,char *bir):Animal (dogming,in_age) //调用基类Animal 的共有的函数成员
    {
    birthplace=new char[strlen(bir)+1];  //测试长度+1
       strcpy(birthplace,bir);   //赋值
       }
    ~Dog()
    {
        delete []birthplace;
    }
1