473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What is the different between override overload and hide?

Hi everyone . I am confused about the different between override
overload and hide.
May be I have a wrong opinion, I always think that hide is very
similar with override.
Both of them call the function of derived class,do not care the
function in the base class.
I know I make something wrong here, but i dont kow what the wrong it
is? Could some one point out for me? sorry for my poor english.

Nov 2 '07 #1
2 3506
dolphin wrote:
Hi everyone . I am confused about the different between override
overload and hide.
May be I have a wrong opinion, I always think that hide is very
similar with override.
Both of them call the function of derived class,do not care the
function in the base class.
A derived class's function overrides a base class function if the signature
is the same and it's declared virtual in the base class. If you call it
through a pointer or a reference to the base class, the function in the
derived class is called. It "overrides" the one in the base class.
Hiding only comes into play if you call a non-virtual function through a
pointer or reference or directly with an object of the derived class. A
derived class' function hides all base class functions with the same name.
Maybe a piece of example code helps:

#include <iostream>
class Base
{
public:
virtual void foo() { std::cout << "Base::foo\n"; }
void foo(int) { std::cout << "Base::foo(int)\n"; }
};

class Derived : public Base
{
public:
void foo() { std::cout << "Derived::foo\n"; }
};

int main()
{
Base b;
b.foo(); // Base's foo() is called
b.foo(3); // Base's foo(int) is called
Derived d;
d.foo(); // Derived's foo() is called. The one in Base is hidden
d.foo(3); // error, that function is also hidden
Base &ref = d;
ref.foo(); // Derived's foo() is called. It overrides the Base version
ref.foo(3); // Base's foo(int) is called
}

Nov 2 '07 #2
On 2007-11-02 10:41, Rolf Magnus wrote:
dolphin wrote:
>Hi everyone . I am confused about the different between override
overload and hide.
May be I have a wrong opinion, I always think that hide is very
similar with override.
Both of them call the function of derived class,do not care the
function in the base class.

A derived class's function overrides a base class function if the signature
is the same and it's declared virtual in the base class. If you call it
through a pointer or a reference to the base class, the function in the
derived class is called. It "overrides" the one in the base class.
Hiding only comes into play if you call a non-virtual function through a
pointer or reference or directly with an object of the derived class. A
derived class' function hides all base class functions with the same name.
Maybe a piece of example code helps:

#include <iostream>
class Base
{
public:
virtual void foo() { std::cout << "Base::foo\n"; }
void foo(int) { std::cout << "Base::foo(int)\n"; }
};
Just like to point out that overloading is when you have two functions
with different signatures but the same name (as above) in the same
scope. The last part about scope is important.

struct B
{
void foo();
void foo(int);
void bar();
};

struct D : public B
{
void bar(int);
};

Here foo is overloaded in B, but bar(int) in D is not an overload of of
bar() in B since they are in different scopes. As Rolf Magnus explained
bar(int) in D hides bar() in B.

--
Erik Wikström
Nov 2 '07 #3

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

Similar topics

2
by: | last post by:
Sorry, I mixed up the code- reposting with correct example now :) I'm trying to better understand the use of inheritance vs. the implementation of a handler. I wanted to ask this question to the...
4
by: ad | last post by:
We can use new or override to rewrite a method in an child class. But what is the difference between new and override?
5
by: Heribert Coumans | last post by:
given the following 2 functions: public bool AnOtherSimpleTest1(string aString) { StringBuilder strB1 = new StringBuilder(); StringBuilder strB2 = new StringBuilder(); strB1.Append(aString);...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
4
by: Rolf Welskes | last post by:
Hello, the problem seems simple I have an own webcontrol one property is public bool IsEdit { get
5
by: sunny | last post by:
hai i am not able to overload a member function of base class in derived calss.what is the wrong thning i am doing here in the following program. # include<iostream> using namespace std; ...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
14
by: =?Utf-8?B?R3JlZ2cgV2Fsa2Vy?= | last post by:
Hopefully someone can set me straight on this issue. I was testing some code where I have a method that is being overriden. The method overrides were working fine until I passed a literal 0 (int...
3
by: PengYu.UT | last post by:
Hi, In the function body of doit, I want to assign -x to _a if x is a real type. I want to assign conj(x) to _a if it is a complex type. I could define template <typename T> class...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.