Thursday, 14 June 2018

function call from child class to base class

#include
class A
{
public:
    A() { f(); }
    virtual ~A() { }
public:
    virtual void f() const { std::cout << 1; }
};


class B
    : public A
{
public :
    B() { f(); }   //Base class function will call first
    void f() const { std::cout << 2; }
};
int main()
{
    //const B *a = new B;
    //delete a;
     B b;
    std::cout << std::endl;
     b.f();
    return 0;
}


Output: 12

No comments:

Post a Comment