inheritance / overriding question

M

Michael

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
 
R

roy axenov

#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
 
H

Heinz Ozwirk

Michael said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top