Chinaunix首页 | 论坛 | 博客
  • 博客访问: 827371
  • 博文数量: 168
  • 博客积分: 5431
  • 博客等级: 大校
  • 技术积分: 1560
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-22 11:56
文章存档

2015年(2)

2014年(1)

2013年(12)

2012年(12)

2011年(15)

2010年(5)

2009年(16)

2008年(41)

2007年(64)

分类: C/C++

2008-01-15 18:18:55

   
    好长时间没有用C++的class编写程序了,发现很多基础知识点已经淡忘了,今天特别抽出时间把它们整理一下。

    有如下代码
    class father{
    public:
       virtual print(){ cout<<"In the father!"<    };

    class son0{
    public:
       virtual print(int x){
            cout<<"In the son"<<"input x is"<    }
    };
    class son_of_son{
    public:
       virtual print(int x){
          cout<<"In son of son!"<<"input x is"<    }
    };

    son0 son_0;
    son_of_son son_of_son_0
    father*  p_ father =  &son_0;
    son0* p_son_0 = &son_of_son_0;

    p_father->print();
    p_son_0->print(1);


    以上代码是真确的可以编译运行,不过其中有几个地方需要注意。
    1)虽然在father中的print()函数与它的儿子的一个函数同名而且同样有virual关键字,但是它们直接并不是一种虚拟多态形式的运用!!因为它们的参数不一致(返回值可以不同的情形是:同一个继承体系下class可以不同;其余的情况不可以!)。在son_of_son中,我改写了son0中的print,从而产生了一种虚拟改写,产生了多态!!

    也就是说,在son当中我只是重新声明了一个新的virutal function而并没有延续father中的virtual print!这一点很重要!


一棵使用b-tree实现的动态增长排序树

#include
#include
#include

using namespace std;

template
class element{
    public:

        typedef T content_type;
        enum{ cnt };
        element():data(0),data_cnt(0),p_right(0),p_left(0){}
        element(content_type t):data(t),data_cnt(0),p_right(0),p_left(0){}
        ~element(){ if(!p_right) delete p_right;
                if(!p_left) delete p_left;
        }

        bool is_right(content_type input)const;    //judge whether the present node
                            //is larger than present,but not
        bool is_left(content_type input)const;
                            //proper to insert!!
        bool insert(content_type input, element*p);//p points to the proper node
                            //as new node's position
        inline element* get_right()const{ return p_right; }
        inline element* get_left()const{ return p_left; }
        inline void add_cnt(void){ data_cnt++; }
        inline unsigned int get_cnt(void)const{ return data_cnt; }
       
    private:
        content_type data;
        unsigned int data_cnt;
        element* p_right;
        element* p_left;
};
//unsigned int element::data_cnt = 0;

template bool element::is_right(T input)const
{ return input > data ? true : false; }
template bool element::is_left(T input)const
{ return input < data ? true : false; }

template bool element::insert(T input, element*p)
//p point to the place to insert while not its parent!
{
    if(!p){
        p = new element(input);
        return true;
    }
    else
        false;
}

element* p_tree;
template bool find_pos_insert(T input, element* p_root)
//p_root point the tree's sub-tree's root
{
    bool ret;
    if (!p_root){
        //p_tree = new element(input);
        ret = p_root->insert(input, p_root);
        return ret;
    }
    if(p_root->is_right(input) == true)//at right size of the present node
        if(!p_root->get_right())
            p_root->insert(input, p_root->get_right());
        else
            ret = find_pos_insert(input, p_root->get_right());
    else if(p_root->is_left(input) == true)//at the left
        if(!p_root->get_left())
            p_root->insert(input, p_root->get_left());
        else
            ret = find_pos_insert(input, p_root->get_left());
    else                    //equal root
        p_root->add_cnt();
    return ret;
}
template bool right_browse(const element* p)
{
    if(!p)
        return false;
   
}

template bool left_browse(const element* p)
{
    if(!p)
        return false;
}
int
main()
{
    int input;

    while(cin>>input && input != -1){
        cout<<"The input:"<        find_pos_insert(input, p_tree);
    }
    delete p_tree;
}
阅读(1037) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~