474,048 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1504

bh**********@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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**********@gm ail.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.l earn.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
4079
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 and binding since Greg originally posted his meowing cat message that it's hard to remember what the original problem was that Greg pointed out. At that time Prothon had no solution for the problem. Now there is a released solution but it is...
53
8358
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 all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values.
19
2297
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 personal server. Are there any code samples or books that someone can recommend so that I can implement this. I would prefer VB.NET code, but I am willing to convert from C# if necessary.
12
2365
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: <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> Will this work for javascript alerts also?
48
5007
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 David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I cannot seem to find many retailers around here that still carry Visual Studio 2003, and some were a...
1
5023
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 - An easy way to classify different pages as either Admin, Member or Public, where login is necessary for Admin and Member but not for Public. My idea was to put the pages in different directories to easily keep my order. - An easy menu system that...
4
2609
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 unrecognised file format, it may have been created with a later version of Access, upgrade to a later version..." Could anyone please help to resolve this issue.
9
3026
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 installation. Thanks, Peng
8
2680
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 Elapse. Is there any way -- whether in my code, or in the O/S -- to determine when a Timer is scheduled to Elapse?
0
10554
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, 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...
0
10357
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,...
0
12157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11616
jinu1996
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...
1
12046
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8711
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 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...
0
6667
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...
0
6864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5430
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 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.