pure virtual function with body

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinendrashankar
    New Member
    • Nov 2007
    • 10

    pure virtual function with body

    Hi All,

    1.Please check this code and its output which is working fine
    while pure virtual functions( FUN1) prototype of Class CA is different
    then Class CB Function (FUN1).

    2. In Class CA pure virtual function ( FUN1) have body.

    3. If i change my second pure virtual function(FUN2) prototype
    class CA----> virtual int fun2()=0;
    class CB---> void fun2(){ cout <<" fun of CB void::CB fun2() "<<endl;}

    i am getting following error

    $ CC -o CoVariance CoVariance.cpp
    "CoVariance.cpp ", line 24: Error: Virtual function CB::fun2() returns void, while CA::fun2() returns int.
    1 Error(s) detected.

    Can any one help me to find this dual behavior of code ?
    #include<iostre am>
    using namespace std;

    class CA
    {
    public:
    CA()
    {cout<<"CA::CA( )"<<endl;}
    virtual CA& fun1()=0;
    virtual void fun2()=0;
    virtual ~CA()
    {cout<<"CA::~CA ()"<<endl;}
    };
    void CA::fun2(){ cout <<"pure virtual fun of CA void::CA fun2() "<<endl;}

    class CB:public CA
    {
    public:
    CB()
    {cout<<"CB::CB( )"<<endl;}
    ~CB()
    {cout<<"CB::~CB ()"<<endl;}
    CB& fun1(){ cout <<"CB fun1() "<<endl;ret urn *this;}
    void fun2(){ CA::fun2();}
    };

    int main()
    {
    CA *p = new CB;
    p->fun1();
    p->fun2();
    delete p;
    return (0);
    }


    OUTPUT:--
    $ CC -o CoVariance CoVariance.cpp
    $ ./CoVariance
    CA::CA()
    CB::CB()
    CB fun1()
    pure virtual fun of CA void::CA fun2()
    CB::~CB()
    CA::~CA()
    $
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I have no idea what your error is about.

    Your code compiles and links with no errors or warnings using Visual Studio.NET 2005.

    Comment

    Working...