Virtual Method

class Foo
{
public:
    
void f()
    {
        std::cout 
<< "Foo::f()" << std::endl;
    }
    
virtual void g()
    {
        std::cout 
<< "Foo::g()" << std::endl;
    }
};

class Bar : public Foo
{
public:
    
void f()
    {
        std::cout 
<< "Bar::f()" << std::endl;
    }
    
virtual void g()
    {
        std::cout 
<< "Bar::g()" << std::endl;
    }
};

int main()
{
    Foo foo;
    Bar bar;

    Foo 
*baz = &bar;
    Bar 
*quux = &bar;

    foo.f(); 
// "Foo::f()"
    foo.g(); // "Foo::g()"

    bar.f(); 
// "Bar::f()"
    bar.g(); // "Bar::g()"

    
// So far everything we would expect

    baz
->f();  // "Foo::f()"
    baz->g();  // "Bar::g()"

    quux
->f(); // "Bar::f()"
    quux->g(); // "Bar::g()"

    
return 0;
}
Pure Virtual Method

class Widget
{
public:
    
virtual void paint() = 0;
};

class Button : public Widget
{
public:
    
virtual void paint()
    {
        
// do some stuff to draw a button
    }
};

cited from http://developer.kde.org/~wheeler/cpp-pitfalls.html

http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/
Posted on 2011-08-05 12:22 于江浩 阅读(143) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理