473,396 Members | 1,961 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,396 software developers and data experts.

Question about inheritance in c++

Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;
};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.
2. how can i protect this code and make this program fail in the
compilation phase?

Thanks,
Dotan

Mar 11 '07 #1
6 1525
Do*******@gmail.com wrote:
Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;
};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.
It doesn't but you used a cast to fool the compiler.
2. how can i protect this code and make this program fail in the
compilation phase?
C++ isn't that sort of language, you don't get the protection you do in
some languages. The simple advice is don't use casts, or when you must
use them, use C++ casts instead of C casts, they're a little bit safer.

john
Mar 11 '07 #2
On Mar 11, 8:35 am, John Harrison <john_androni...@hotmail.comwrote:
Dotani...@gmail.com wrote:
Hello everyone,
I wrote:
class CA{
public:
CA() {number = 1;}
~CA() {}
int number;
};
class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};
void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}
OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.

It doesn't but you used a cast to fool the compiler.
2. how can i protect this code and make this program fail in the
compilation phase?

C++ isn't that sort of language, you don't get the protection you do in
some languages. The simple advice is don't use casts, or when you must
use them, use C++ casts instead of C casts, they're a little bit safer.

john
Thanks,
1. why it does'nt crush on the runtime phase?
2. can you show me the differ from c casting style & c++?

dotan

Mar 11 '07 #3
Do*******@gmail.com wrote:
On Mar 11, 8:35 am, John Harrison <john_androni...@hotmail.comwrote:
>>Dotani...@gmail.com wrote:
>>>Hello everyone,
>>>I wrote:
>>>class CA{
>>>public:
CA() {number = 1;}
~CA() {}
int number;
};
>>>class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};
>>>void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}
>>>OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.

It doesn't but you used a cast to fool the compiler.

>>2. how can i protect this code and make this program fail in the
compilation phase?

C++ isn't that sort of language, you don't get the protection you do in
some languages. The simple advice is don't use casts, or when you must
use them, use C++ casts instead of C casts, they're a little bit safer.

john


Thanks,
1. why it does'nt crush on the runtime phase?
When you break the rules of C++ usually what you get is 'undefined
behaviour'. Undefined behaviour means anything could happen with your
program. It could run, it could crash, it could run but print out
garbage, anything can happen. If you do this kind of thing too often I
promise you that your programs will crash, but they are not *required*
to crash.
2. can you show me the differ from c casting style & c++?
C++ has four new styles of cast.

static_cast<CB*>(a1)->foo();
reinterpret_cast<CB*>(a1)->foo();
const_cast<CB*>(a1)->foo();
dynamic_cast<CB*>(a1)->foo();

They are all designed to do one kind of casting, and if you use them to
do a different kind of casting you will get a compile error.

static_cast is for related types (it would work in your example because
CB inherits from CA but it wouldn't work if CB and CA were unrelated)

reinterpret_cast is for unrelated types, it works pretty much anytime
and is closest to the C style cast.

But even reinterpret_cast cannot remove const, const_cast is for that.

Finally dynamic_cast is something new. It is for polymorphic types (i.e.
classes with virtual functions) and determines at runtime whether a cast
can be made.

john

>
dotan
Mar 11 '07 #4
On 3ÔÂ11ÈÕ, ÏÂÎç1ʱ39·Ö, "Dotani...@gmail.com" <Dotani...@gmail.comwrote:
Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;

};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}

};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();

}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.
2. how can i protect this code and make this program fail in the
compilation phase?

Thanks,thi
Dotan
Hello Dotan
Your programe maybe can't pass compilation phase,because the
compiler isn't able to find the declaration of "uid".I modified this
programe and let "uid" be the data menber of class CB.Then my programe
can get the same result as yours.The reason for this result is that
the constructor of class CB wasn't called,so "uid" wasn't initialized
and the value of it is uncertaint.
Changing the pointer of bass class to the ones of derived class is
unsafe except that you could make sure the object to which this
pointer points is derived class object.You can use operator
"dynamic_cast" to finish this work,but "dynamic_cast" requires the
bass class to have visual functions,because the compiler gets RTTI
from the visual tables.
Thanks
Mar 11 '07 #5
Do*******@gmail.com wrote:
Hello everyone,

I wrote:

class CA{

public:
CA() {number = 1;}
~CA() {}
int number;
};

class CB : public CA
{
public:
CB(){number = 2;}
~CB()
void foo(){
uid++;
cout<<"uid:"<<uid<<endl;}
};

void main()
{
CA* a1 = new CA();
((CB*) a1)->foo();
}

OK, i pass compilation phase & runtime phase the output was
"uid:-23847298":
questions:
1. why i pass the runtime phase? CA class have no "uid" member.
You used a C-style cast here:

((CB*) a1)->foo();

That is a very powerful tool, telling the compiler to just "shut up and do
it!".

You are not asking the compiler to convert a1 into a CB*, you tell the
compiler that it already IS a CB*.
2. how can i protect this code and make this program fail in the
compilation phase?
You shouldn't tell lies to the compiler. :-)

If you had tried the C++ way, and done a static_cast<CB*>(a1), the compiler
would have told you that it cannot do that. You told it to do it anyway!
Bo Persson
Mar 11 '07 #6
Bo Persson wrote:
>
>1. why i pass the runtime phase? CA class have no "uid" member.

You used a C-style cast here:

((CB*) a1)->foo();

That is a very powerful tool, telling the compiler to just "shut up and do
it!".

You are not asking the compiler to convert a1 into a CB*, you tell the
compiler that it already IS a CB*.
Note, it is _always conversion_ that, you are asking the compiler to do with
"(CB)a1", but what concrete kind of conversion (reinterpret, static etc )
will be done is unknown (implicit).

_Conversion_ is not the same as "a1" _treated already IS_ a CB*, in general
case of non-POD-pointers "(CB)a1" can be not the same as "*((CB*)(&a1))".
--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Mar 19 '07 #7

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

Similar topics

8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
23
by: neoedmund | last post by:
There's a program, it's result is "unexpected aaa", i want it to be "expected aaa". how to make it work? class C1(object): def v(self, o): return "expected "+o class C2(object):
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
8
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
4
by: sip.address | last post by:
Hi there, When creating interfaces and implementations, the usual thing is doing somethign like class Interface { public: virtual void f() = 0; virtual void g() = 0; };
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.