473,320 Members | 2,092 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.

will this code work always?

Hi

I have a rather simple question:

I have following class definitions:

class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
{
cout << "Derived";
}
};
void main ()
{
Base *ptr = new derived;
ptr->display ();
}

When I compile this code in VC++ 6.0 it works fine.
My question is that can this code work always?
Or the "virtual void display ()" in derived class can create problem?
What can be the problem with this code?

Thanks

Aug 2 '06 #1
9 1476

bh**********@gmail.com napísal(a):
Hi

I have a rather simple question:

I have following class definitions:

class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
{
cout << "Derived";
}
};
void main ()
{
Base *ptr = new derived;
ptr->display ();
}

When I compile this code in VC++ 6.0 it works fine.
My question is that can this code work always?
Or the "virtual void display ()" in derived class can create problem?
What can be the problem with this code?

Thanks
This code, as you have written it, will work always..

Aug 2 '06 #2
bh**********@gmail.com wrote:

[code reformatted with indentation]

Please indent code before posting it.
Hi

I have a rather simple question:

I have following class definitions:

class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
{
cout << "Derived";
}
};

void main ()
{
Base *ptr = new derived;
ptr->display ();
}

When I compile this code in VC++ 6.0 it works fine.
Unlikely, since you've not declared nor defined the class "derived". I
suppose you meant "Derived".
My question is that can this code work always?
What does "work" mean to you?
Or the "virtual void display ()" in derived class can create problem?
Such as what?
What can be the problem with this code?
The problems I see are unrelated to your question. You haven't included
<iostreamand you're missing the std:: namespace prefix of std::cout.

Assuming you fix these the code should output "Derived". I don't see
any problems related to your use of virtual functions.
Aug 2 '06 #3
bh**********@gmail.com wrote:
Hi

I have a rather simple question:

I have following class definitions:

class Base
{
public:
virtual void display ()
{
cout << "Base";
Error: cout is undefined. You need to #include <iostreamand get cout into
the global namespace with: using std::cout;
}
};

class Derived:public Base
{
public:
virtual void display ()
{
cout << "Derived";
Error (same as above).
}
};
void main ()
Error: main() must always return int.
{
Base *ptr = new derived;
Error: derived is undefined. You probably meant Derived.
ptr->display ();
}

When I compile this code in VC++ 6.0 it works fine.
That's strange. Though VC++ 6.0 is not really known for good standard
compliance, I thought it'd at least correctly implement the
case-sensitivity of C++. Are you sure it's really this code you tried?
My question is that can this code work always?
Actually, it shouldn't ever due to several errors in the code.
Or the "virtual void display ()" in derived class can create problem?
I don't see a reason why it should. What makes you think so?
What can be the problem with this code?
See above.

Aug 2 '06 #4
bh**********@gmail.com wrote:
class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
With gcc you will get a warning that you specified "virtual" a second time.
The compiler already knows that the function is virtual from the Base
class.
{
cout << "Derived";
}
};
Aug 2 '06 #5
Thorsten Kiefer wrote:
bh**********@gmail.com wrote:
>class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
With gcc you will get a warning that you specified "virtual" a second
time.
Huh? I never saw such a warning, and I tend to repeat the "virtual" for
clarity. Are you sure you didn't explicitly switch that on?
The compiler already knows that the function is virtual from the
Base class.
>{
cout << "Derived";
}
};
Aug 2 '06 #6
bh**********@gmail.com wrote:
Hi

I have a rather simple question:

I have following class definitions:

class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
{
cout << "Derived";
}
};
void main ()
{
Base *ptr = new derived;
ptr->display ();
}

[...]
What can be the problem with this code?
You might want to make the destructor of "Base" virtual. Maybe the FAQ can
help you to decide:
http://www.parashift.com/c++-faq-lit....html#faq-20.7

Aug 2 '06 #7
Rolf Magnus wrote:
Huh? I never saw such a warning, and I tend to repeat the "virtual" for
clarity. Are you sure you didn't explicitly switch that on?
I'm not sure. At work I must use VC++, and maybe that warning appears
there ?!?

Aug 2 '06 #8
Thorsten Kiefer wrote:
Rolf Magnus wrote:
Huh? I never saw such a warning, and I tend to repeat the "virtual"
for clarity. Are you sure you didn't explicitly switch that on?
I'm not sure. At work I must use VC++, and maybe that warning appears
there ?!?
I don't think so. It's a common usage, one that aids development.


Brian
Aug 2 '06 #9
On 2 Aug 2006 01:32:35 -0700, "Me*****@gmail.com" <Me*****@gmail.com>
wrote in comp.lang.c++:
>
bh**********@gmail.com napísal(a):
Hi

I have a rather simple question:

I have following class definitions:

class Base
{
public:
virtual void display ()
{
cout << "Base";
}
};

class Derived:public Base
{
public:
virtual void display ()
{
cout << "Derived";
}
};
void main ()
{
Base *ptr = new derived;
ptr->display ();
}

When I compile this code in VC++ 6.0 it works fine.
My question is that can this code work always?
Or the "virtual void display ()" in derived class can create problem?
What can be the problem with this code?

Thanks

This code, as you have written it, will work always..
WRONG. Nonsense, utter rubbish. The program, as the OP has written
it, is ill-formed and there are compilers that will reject it without
producing an executable.

The C language requires that main() be defined with a return type of
int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 3 '06 #10

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

Similar topics

90
by: Mark Hahn | last post by:
"Michael Geary" <Mike@Geary.com> wrote ... >Does anyone have some sample code where obj$func() would be used? > (Apologies if I missed it.) There have been so many messages about delegation...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
12
by: korund | last post by:
How to make javascript alert with non-english text displaying correctly on computers where english only is default system & language settings? For web page the solution is just use meta tags:...
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
1
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database -...
4
by: Bugs | last post by:
Hi everyone. I am trying to open a database which works fine using Access 2003, but when trying to open it on another PC that has Access 2002 I get the following error "This database is...
9
by: PengYu.UT | last post by:
Hi, I feel argparse has some useful things that optparse doesn't have. But I can't find it argparse in python library reference. I'm wondering when it will be available in the python standard...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
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...

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.