inheritance / overriding question

Discussion in 'C++' started by Michael, Mar 3, 2007.

  1. Michael

    Michael Guest

    Hi All,

    I have the following:

    #include <iostream>

    class BaseClass {
    protected:
    int var;
    public:
    void setVar(int var){
    this->var = var;
    std::cout << "using BaseClass\n";
    }
    virtual void showVar() = 0;
    };

    class ClassA : public BaseClass {
    public:
    void setVar(int var){
    this->var = var;
    std::cout << "using ClassA\n";
    }
    void showVar(){
    std::cout << "using ClassA var is " << var;
    }
    };

    int main(){
    BaseClass* base;

    base = new ClassA;

    base->setVar(12); //I know I have a magic number here, please ignore
    this
    base->showVar();
    }

    why does base->setVar() call the function in the BaseClass, not the one in
    ClassA? Shouldn't ClassA override the BaseClass function?

    Thanks for your help

    Michael
     
    Michael, Mar 3, 2007
    #1
    1. Advertising

  2. Michael

    roy axenov Guest

    On Mar 3, 11:42 am, "Michael" <>
    wrote:
    > #include <iostream>
    >
    > class BaseClass {
    > protected:
    > int var;
    > public:
    > void setVar(int var){
    > this->var = var;
    > std::cout << "using BaseClass\n";
    > }
    > virtual void showVar() = 0;
    > };
    >
    > class ClassA : public BaseClass {
    > public:
    > void setVar(int var){
    > this->var = var;
    > std::cout << "using ClassA\n";
    > }
    > void showVar(){
    > std::cout << "using ClassA var is " << var;
    > }
    > };
    >
    > int main(){
    > BaseClass* base;
    >
    > base = new ClassA;
    >
    > base->setVar(12);
    > base->showVar();
    > }
    >
    > why does base->setVar() call the function in the
    > BaseClass, not the one in ClassA?


    It's what you asked for. base is a pointer to a BaseClass
    object, and setVar is not a virtual function.

    > Shouldn't ClassA override the BaseClass function?


    Nope, because you didn't ask for it.

    http://www.parashift.com/c -faq-lite/virtual-functions.html

    --
    roy axenov
     
    roy axenov, Mar 3, 2007
    #2
    1. Advertising

  3. Michael

    Heinz Ozwirk Guest

    "Michael" <> schrieb im Newsbeitrag
    news:45e94323$0$31090$...
    > Hi All,
    >
    > I have the following:
    >
    > #include <iostream>
    >
    > class BaseClass {
    > protected:
    > int var;
    > public:
    > void setVar(int var){
    > this->var = var;
    > std::cout << "using BaseClass\n";
    > }
    > virtual void showVar() = 0;
    > };
    >
    > class ClassA : public BaseClass {
    > public:
    > void setVar(int var){
    > this->var = var;
    > std::cout << "using ClassA\n";
    > }
    > void showVar(){
    > std::cout << "using ClassA var is " << var;
    > }
    > };
    >
    > int main(){
    > BaseClass* base;
    >
    > base = new ClassA;
    >
    > base->setVar(12); //I know I have a magic number here, please ignore
    > this
    > base->showVar();
    > }
    >
    > why does base->setVar() call the function in the BaseClass, not the one in
    > ClassA? Shouldn't ClassA override the BaseClass function?


    base is a pointer to BaseClass, so, except for virtual functions, only
    functions of BaseClass can be called through this pointer. To call a
    (overloaded) function in a derived class through a pointer to (one of) its
    base classes, declare the function as virtual in the base class. For all the
    compiler knows, base points to an instance of BaseClass, so only those
    declarations matter. When the compiler calls base->showVar(), it doesn't
    even know that showVar has been overridden somewhere.

    HTH
    Heinz
     
    Heinz Ozwirk, Mar 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,198
    Martijn van Steenbergen
    Dec 21, 2003
  2. bugbear
    Replies:
    13
    Views:
    6,432
    P.Hill
    Jan 11, 2005
  3. cppsks
    Replies:
    0
    Views:
    854
    cppsks
    Oct 27, 2004
  4. =?Utf-8?B?aGZkZXY=?=
    Replies:
    0
    Views:
    358
    =?Utf-8?B?aGZkZXY=?=
    Sep 13, 2007
  5. Tim S
    Replies:
    3
    Views:
    149
    Brian McCauley
    Jun 1, 2007
Loading...

Share This Page