473,511 Members | 15,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritance / overriding question

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
Mar 3 '07 #1
2 1393
On Mar 3, 11:42 am, "Michael" <michael5...@yahoo.com>
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-lit...functions.html

--
roy axenov

Mar 3 '07 #2
"Michael" <mi*********@yahoo.comschrieb im Newsbeitrag
news:45***********************@per-qv1-newsreader-01.iinet.net.au...
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
Mar 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3723
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
2173
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
1
448
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
0
5702
by: Ian | last post by:
I've got this problem with overriding attributes on an inheritance chain where the XmlSerializer is concerned. For example: public class A { private string aWord = String.Empty(); public...
45
6310
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
1503
by: Emmanuel | last post by:
Hi, I'm working on a c# web app and require having some code which runs in the page Load event of each page and to be reusable in other web apps. So i decided to use a Class Library which...
6
1655
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
4
1452
by: vivekian | last post by:
Hi, Have this following hierarchy which am implementing for a networking program. The base class 'ASocket' is the base class from which 'AListener' and 'ATalker' inherit . None of the functions...
3
1807
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
11
2230
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.