Consider the following code
#include <iostream>
class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};
class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};
int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}
Running this program produces following output.
$ ./a.out
Derived
$
I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?
Thanks 13 2833
dragoncoder wrote: Consider the following code
#include <iostream>
class Base { public: virtual void say() { std::cout << "Base" << std::endl; } };
class Derived: public base { private: void say() { std::cout << "Derived" << std::endl; } };
int main() { Derived d; Base* b = &d; b->say(); return 0; }
Running this program produces following output.
$ ./a.out Derived $
I have 2 questions: 1. Is the above code legal ?
Yes. And an excellent example of poor coding practice.
2. If the virtual function is private in Base, can I make it public in Derived ?
No. If it were private in Base, then Derived wouldn't inherit it.
Best regards,
Tom
dragoncoder wrote: Consider the following code
#include <iostream>
class Base { public: virtual void say() { std::cout << "Base" << std::endl; } };
class Derived: public base {
You mean
class Derived: public Base {
private: void say() { std::cout << "Derived" << std::endl; } };
int main() { Derived d; Base* b = &d; b->say(); return 0; }
Running this program produces following output.
$ ./a.out Derived $
I have 2 questions: 1. Is the above code legal ?
Yes, absolutely, considering the correction I made. Try not to type your
code directly into the message next time. Use "copy-and-paste" mechanism
undoubtedly available to you.
2. If the virtual function is private in Base, can I make it public in Derived ?
The access specifier only has effect on the ability to call the function
as if it weren't virtual. For example, if you make 'say' private in 'Base',
your call to it in 'main' (b->say()) will be ill-formed, and won't compile.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thomas Tutone wrote: dragoncoder wrote: Consider the following code
#include <iostream>
class Base { public: virtual void say() { std::cout << "Base" << std::endl; } };
class Derived: public base { private: void say() { std::cout << "Derived" << std::endl; } };
int main() { Derived d; Base* b = &d; b->say(); return 0; }
Running this program produces following output.
$ ./a.out Derived $
I have 2 questions: 1. Is the above code legal ?
Yes. And an excellent example of poor coding practice.
Why do you call this practice "poor"? 2. If the virtual function is private in Base, can I make it public in Derived ?
No. If it were private in Base, then Derived wouldn't inherit it.
Nothing is inherited here. It's _overridden_. Try it:
#include <iostream>
class Base {
virtual int foo() { return 42; } // private
public:
int bar() { return foo(); }
};
class Derived : public Base {
virtual int foo() { return 73; } // also private
};
int main()
{
Derived d;
Base *b = &d;
std::cout << b->bar() << std::endl;
return 0;
}
What happens here?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
dragoncoder wrote: Consider the following code
#include <iostream>
class Base { public: virtual void say() { std::cout << "Base" << std::endl; } };
class Derived: public base { private: void say() { std::cout << "Derived" << std::endl; } };
int main() { Derived d; Base* b = &d; b->say(); return 0; }
Running this program produces following output.
$ ./a.out Derived $
I have 2 questions: 1. Is the above code legal ?
Since the above code compiled, it is syntactically legal.
2. If the virtual function is private in Base, can I make it public in Derived ?
The call to b->say() is calling Derived class say() function. Here
Derived class does not know anything about say() function of Base class
since say() is private in Base(). Hence say() functions in Derived and
Base class are not related by virtual function paradigm.
Even if you remove "virtual" specifier from Base class say() function,
you will still run Derived class say() function since say() is public
in Derived and there is no public say() in Base class.
Tejas Kokje
Tejas Kokje wrote: 1. Is the above code legal ?
Since the above code compiled, it is syntactically legal.
Famous last words.
Try: "The code is well-formed, so it should compile."
Plenty of compilers reject plenty of well-formed constructions.
Yes, the OP is correct to rely on their compiler to judge how well such a
simple bit of code is formed!
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Phlip wrote: Tejas Kokje wrote:
1. Is the above code legal ? Since the above code compiled, it is syntactically legal.
Famous last words.
Try: "The code is well-formed, so it should compile."
Plenty of compilers reject plenty of well-formed constructions. Yes, the OP is correct to rely on their compiler to judge how well such a simple bit of code is formed!
I think you misunderstood my comments. I said that the code is
*syntactically* legal. This does not mean that code semantics make
sense. If compilers could detect semantic errors in code, most of the
softwares would be bug free.
Anyways, in OP's case, compiler did compile the code. So there is no
reason for him/her to see the code as illegal. However, OP is not
relying on compiler to tell him that code is well formed. Hence he/she
posted a question here.
Tejas Kokje
Tejas Kokje <bi************ *@gmail.com> wrote: dragoncoder wrote:
Running this program produces following output.
$ ./a.out Derived $
I have 2 questions: 1. Is the above code legal ?
Since the above code compiled, it is syntactically legal.
No. Most compilers are broken in one respect or the other. I do not
know if Comeau is 100% compliant even. The code below will compile with
some compilers, but it is *not* syntactically legal:
class foo
{
void bar () {}
};
int main ()
{
void (foo::*pbar) () = foo::bar;
}
regards
--
jb
(reply address in rot13, unscramble first)
Can you tell me which compiler compiles above code ?
since bar() is private and non-static to class foo, how can you access
it outside the class using class scope ?
Tejas Kokje
In article <11************ **********@g10g 2000cwb.googleg roups.com>,
"dragoncode r" <pk******@gmail .com> wrote: Consider the following code
#include <iostream>
class Base { public: virtual void say() { std::cout << "Base" << std::endl; } };
class Derived: public base { private: void say() { std::cout << "Derived" << std::endl; } };
int main() { Derived d; Base* b = &d; b->say(); return 0; }
Running this program produces following output.
$ ./a.out Derived $
I have 2 questions: 1. Is the above code legal ?
Absolutely. It's a great way to make sure the derived object is used
through a base pointer.
2. If the virtual function is private in Base, can I make it public in Derived ?
Yes.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mshetty |
last post by:
Hi,
I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.
class y : public x
{
private:
|
by: p988 |
last post by:
using System;
using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
MyForm ()
{
Text = "Windows Forms Demo";
}
|
by: Paul Cheetham |
last post by:
Hi,
I am creating a custom control in c#, derived from System.Drawing.Image
In my derived control, I want to make the Public property Image into a
Protected property, as I only want to manipulate it from within the
class.
In C++ this would be as simple as re-declaring it with the new access
specifier (protected) but it doesn't seem to work in c#
|
by: RitualDave |
last post by:
This compiles and runs successfully in VS2005:
ref class A
{
private:
~A() { this->!A(); } // private!
!A() { } // private!
};
....
|
by: Christian Christmann |
last post by:
Hi,
I've a a question on the specifier extern.
Code example:
void func( void )
{
extern int e;
//...
| |
by: mrclash |
last post by:
Hello,
I have a class that uses some variables to access a sql database, and I
want to make a derived class that inherits all the methods of this
class, because the derived one will do exactly the same process that
the parent one, but with a postgres database, thus I've got to use
diferent type variables to acces the database.
There's one big function in the class that does all the DB stuff, and I
want to know if there's any way to...
|
by: Tugrul HELVACI |
last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ??
I'm using Delphi 2006 and I have a class defination like this:
TPerson = class
fPersonName : String;
fPersonSurName : String;
fPersonAge : Integer;
published
property PersonName : String read fPersonName write fPersonName;
|
by: Wade Ward |
last post by:
/* C version */
static unsigned long
t,x=123456789,y=362436069,z=21288629,w=14921776,c=0;
unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w); }
What would be an appropriate caller for this function. Am I correct
that the static specifier has the function remembering its older
|
by: Rahul |
last post by:
Hi Everyone,
The following code works fine,
class A
{
private: friend void sample(A& obj)
{
printf("in friend...\n");
printf("%d",obj.i);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |