Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341095
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:18

#include
#include

/**
 * There're some serious issues in class Base and Subclass.
 * You should check for current and POTENTIAL bugs in them,
 * especially memory problems.
 */

class Base
{
  private:
    char * name;
  public:
    Base(char * className)
    {
        name = new char[strlen(className)];
        strcpy(name, className);
    }

    ~Base()
    {
        delete name;
    }

    char * copyName()
    {
        char newname [256];
        strcpy(newname, name);
        return newname;
    }

    virtual char * getName()
    {
        return name;
    }

    static void print(Base base)
    {
        printf("name: %s\n" , base.name);
    }
};

class Subclass : public Base
{
  public:
    Subclass(char * className) : Base(className)
    {
    }
};

int main()
{
    Base * pBase = new Subclass("test");
    Base::print(*pBase);
    printf("name: %s\n", pBase->getName());
    printf("new name: %s\n", pBase->copyName());

    return 0;
}




--------------------next---------------------

阅读(1705) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~