will this code work always?

B

bhavik.patel

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
 
M

Mehturt

(e-mail address removed) 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..
 
M

Mark P

(e-mail address removed) 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
<iostream> and 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.
 
R

Rolf Magnus

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";

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.
 
T

Thorsten Kiefer

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.
 
R

Rolf Magnus

Thorsten said:
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?
 
V

Volker Lukas

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-lite/virtual-functions.html#faq-20.7
 
T

Thorsten Kiefer

Rolf said:
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 ?!?
 
D

Default User

Thorsten said:
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
 
J

Jack Klein

(e-mail address removed) napísal(a):

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top