By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

derived class incresing the sopce of base class methods and explicit call to constructor

P: n/a
Hi All,

I tried something with the C++ I know and some things just seem
strange.

consider:

#include <iostream>

using namespace std;

class base
{
public:

base();
void some_func_2();
private:
void some_func();
};

///////////////////////////
base::base()
{
cout<<"base constructor"<<endl;
}
///////////////////////////

void base::some_func()
{
cout<<"base some func"<<endl;
}
///////////////////////////

void base::some_func_2()
{
cout<<"base some_func_2"<<endl;
}
///////////////////////////

class derived: public base
{
public:
void some_func();
static void some_func_2();

};
void derived::some_func()
{
cout<<"derived some func"<<endl;
}
///////////////////////////

void derived::some_func_2()
{
cout<<"derived some_func_2"<<endl;
}
///////////////////////////

void main()
{
base b;
b.some_func_2();
base::base();
derived d;
d.some_func();
derived::some_func_2();
d.some_func_2();

derived *pd;
pd = reinterpret_cast<derived*>(new base());

// derived *pd;
// pd = new base(); /// This will cause errors if
uncommented.

}

Here's the output:

base constructor
base some_func_2
base constructor
base constructor
derived some func
derived some_func_2
derived some_func_2
base constructor
My problems here are that

1. I understood that derived class cannot increase the scope of the
base class methods. In this case 'derived' increased the scope of the
base class private method 'some_func' by making it public and the scope
of base class 'some_func_2' by making it static. Is my understading
incorrect or there's something wrong?

2. The constructor for the base class can be called explicitly, even
though I cannot do anything more with that. i fail to understand why
should it be allowed. Are there any special circumstances where this is
used?

3. Base class handle can take derived class objects but not the other
way around. Casting will remove this error but why doesn't implicit
cast like the previous one work?
I was using Microsoft Visual Studio 6.0.

Thanks in advance,

Regards,
-- Taran

Apr 19 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Taran" <ta************@gmail.com> skrev i meddelandet
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi All,

I tried something with the C++ I know and some things just seem
strange.

consider:

#include <iostream>

using namespace std;

class base
{
public:

base();
void some_func_2();
private:
void some_func();
};

///////////////////////////
base::base()
{
cout<<"base constructor"<<endl;
}
///////////////////////////

void base::some_func()
{
cout<<"base some func"<<endl;
}
///////////////////////////

void base::some_func_2()
{
cout<<"base some_func_2"<<endl;
}
///////////////////////////

class derived: public base
{
public:
void some_func();
static void some_func_2();

};
void derived::some_func()
{
cout<<"derived some func"<<endl;
}
///////////////////////////

void derived::some_func_2()
{
cout<<"derived some_func_2"<<endl;
}
///////////////////////////

void main()
{
base b;
b.some_func_2();
base::base();
derived d;
d.some_func();
derived::some_func_2();
d.some_func_2();

derived *pd;
pd = reinterpret_cast<derived*>(new base());

// derived *pd;
// pd = new base(); /// This will cause errors if
uncommented.

}

Here's the output:

base constructor
base some_func_2
base constructor
base constructor
derived some func
derived some_func_2
derived some_func_2
base constructor
My problems here are that

1. I understood that derived class cannot increase the scope of the
base class methods. In this case 'derived' increased the scope of
the
base class private method 'some_func' by making it public and the
scope
of base class 'some_func_2' by making it static. Is my understading
incorrect or there's something wrong?
The functions defined in derived hides the functions in the class
base. It's a another set of functions, with the same names. It doesn't
affect the functions of the base class.

2. The constructor for the base class can be called explicitly, even
though I cannot do anything more with that. i fail to understand why
should it be allowed. Are there any special circumstances where this
is
used?
Not very useful, but also not specifically forbidden by the standard.
Why should it be?

3. Base class handle can take derived class objects but not the
other
way around.
In C++ it is called a pointer, not a handle. A base class pointer can
point to a derived class, because there is a base part in the derived
class. A derived class can act as its base, because it has inherited
(some of) its properties.
Casting will remove this error but why doesn't implicit
cast like the previous one work?


Casting doesn't remove the error, it just tells the compiler to shut
up -- "Trust me, I know what I'm doing!".

If that isn't really true, all kinds of nasty things are likely to
happen. :-)
Bo Persson
Apr 19 '06 #2

P: n/a
Taran wrote:
Hi All,

I tried something with the C++ I know and some things just seem
strange.

consider:

#include <iostream>

using namespace std;

class base
{
public:

base();
void some_func_2();
private:
void some_func();
};

///////////////////////////
base::base()
{
cout<<"base constructor"<<endl;
}
///////////////////////////

void base::some_func()
{
cout<<"base some func"<<endl;
}
///////////////////////////

void base::some_func_2()
{
cout<<"base some_func_2"<<endl;
}
///////////////////////////

class derived: public base
{
public:
void some_func();
static void some_func_2();

};
void derived::some_func()
{
cout<<"derived some func"<<endl;
}
///////////////////////////

void derived::some_func_2()
{
cout<<"derived some_func_2"<<endl;
}
///////////////////////////

void main()
{
base b;
b.some_func_2();
base::base();
derived d;
d.some_func();
derived::some_func_2();
d.some_func_2();

derived *pd;
pd = reinterpret_cast<derived*>(new base());

// derived *pd;
// pd = new base(); /// This will cause errors if
uncommented.

}

Here's the output:

base constructor
base some_func_2
base constructor
base constructor
derived some func
derived some_func_2
derived some_func_2
base constructor
My problems here are that

1. I understood that derived class cannot increase the scope of the
Not sure what you mean here by "increase the scope"...
base class methods. In this case 'derived' increased the scope of the
base class private method 'some_func' by making it public
No, it didn't. 'derived' has its own function 'some_func' that is not
related to the function named the same in the 'base' class.
and the
scope of base class 'some_func_2' by making it static. Is my
understading incorrect or there's something wrong?
Nothing changes the scope of nothing here. Two new functions introduced
by the 'derived' type. Both hide the functions with the same name from
the 'base' class.
2. The constructor for the base class can be called explicitly, even
No. The syntax you used creates a temporary object (and discards it
promptly). Yes, it causes the constructor to be called, but it doesn't
mean that you can call a construtor.
though I cannot do anything more with that. i fail to understand why
should it be allowed. Are there any special circumstances where this
is used?
Yes. If you want to create a temporary object and use it.
3. Base class handle
What's that?
can take derived class objects but not the other
way around.
You mean, a pointer to a derived class can be converted to a pointer
of a base class? Yes. A couple conditions apply, but not in this case.
Casting will remove this error but why doesn't implicit
cast like the previous one work?
Which error? Which implicit cast? Which one is "previous"?
I was using Microsoft Visual Studio 6.0.


You should get yourself VC++ Express 2005 and enjoy a much better
compiler (although it shouldn't matter here).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 19 '06 #3

P: n/a
I think you are a newer,you have a long way to go.

Apr 20 '06 #4

P: n/a
> #include <iostream>

using namespace std;

class base
{
public:

base();
void some_func_2();
private:
void some_func();
};

///////////////////////////
base::base()
{
cout<<"base constructor"<<endl;
}
///////////////////////////

void base::some_func()
{
cout<<"base some func"<<endl;
}
///////////////////////////

void base::some_func_2()
{
cout<<"base some_func_2"<<endl;
}
///////////////////////////

class derived: public base
{
public:
void some_func();
static void some_func_2();

};
void derived::some_func()
{
cout<<"derived some func"<<endl;
}
///////////////////////////

void derived::some_func_2()
{
cout<<"derived some_func_2"<<endl;
}
///////////////////////////

void main()
{
base b;
b.some_func_2();
base::base();
derived d;
d.some_func();
derived::some_func_2();
d.some_func_2();

derived *pd;
pd = reinterpret_cast<derived*>(new base());

// derived *pd;
// pd = new base(); /// This will cause errors if
uncommented.
Of course it is an error. Don't treat a fruit as an apple, because it
may not be. Treat an apple as a fruit, a derived as a base, not the
other way round.

}

Here's the output:

base constructor
base some_func_2
base constructor
base constructor
derived some func
derived some_func_2
derived some_func_2
base constructor
My problems here are that

1. I understood that derived class cannot increase the scope of the
base class methods. In this case 'derived' increased the scope of the
base class private method 'some_func' by making it public and the scope
of base class 'some_func_2' by making it static. Is my understading
incorrect or there's something wrong?
The class derive does not and cannot alter the definition of base. Hence
the "scope" of base can neither be "increased" nor "decreased".

The base class has 3 members:
* the constructor base::base
* base::some_func
* base::some_func_2

The derived class has 5 members:
* the constructor derived::derived
* static derived::some_func_2
* derived::some_func
* base::some_func
* base::some_func2

The derived::some_func simply hides base::some_func in class derived.

2. The constructor for the base class can be called explicitly, even
though I cannot do anything more with that. i fail to understand why
should it be allowed. Are there any special circumstances where this is
used?
It cannot. Detail answered in other posts.

3. Base class handle can take derived class objects but not the other
way around. Casting will remove this error but why doesn't implicit
cast like the previous one work?
If what you mean by "handle" is what we call "pointer"...

It is true that a pointer to base can take a pointer to derived. But the
one you commented was doing it the other way round, which doesn't work.

base* p = new derived; // OK
dervied* q = new base; // ERROR

derived* r = p; // ERROR, no implicit conversion supplied
derived* s = static_case<derived*>(p); //OK, explicit conversion

I was using Microsoft Visual Studio 6.0.

Thanks in advance,

Regards,
-- Taran


Regards,
Ben
Apr 20 '06 #5

P: n/a

benben wrote in message <44**********************@news.optusnet.com.au>. ..

3. Base class handle can take derived class objects but not the other
way around. Casting will remove this error but why doesn't implicit
cast like the previous one work?
If what you mean by "handle" is what we call "pointer"...

It is true that a pointer to base can take a pointer to derived. But the
one you commented was doing it the other way round, which doesn't work.

base* p = new derived; // OK


OK?? (See what I was going to post (below) and correct me or comment,
please.)
dervied* q = new base; // ERROR

derived* r = p; // ERROR, no implicit conversion supplied
derived* s = static_case<derived*>(p); //OK, explicit conversion

[ was to the OP ]
// -------------------
[ others have answered your questions, so, I'll just nit-pick a bit.]

In your main() you made the comment " // This will cause errors if
uncommented.". Let's be consistant.

// void main()
int main(){
base b;
b.some_func_2();
base::base();
// derived d;
// d.some_func();
// derived::some_func_2();
// d.some_func_2();

// derived *pd;
// pd = reinterpret_cast<derived*>(new base());

// derived *pd;
// pd = new base(); // This will cause errors if uncommented.

// delete pd;
return 0;
}

There! That's better.
Why? Try this:

class base{ public:
base();

// add this:
~base(); // test, then comment this Dtor and....
// virtual ~base(); // uncomment this Dtor

void some_func_2();
private:
void some_func();
};

///////////////////////////
base::~base(){
cout<<" ~base destructor "<<endl;
}
///////////////////////////

// add a destructor (non-virtual, with output) to your 'derived' class also.
// -- rest of your program same --

In your 'toy' program it won't hurt you much, but, in a big project it could
be a big problem.
[ 'it' being the missing 'virtual destructor' in base class. ]
// -------------------

So, am I right, or out in left field somewhere?

--
Bob R
POVrookie
Apr 20 '06 #6

P: n/a
海风 wrote:
I think you are a newer,you have a long way to go.


A newer what? And who are you talking to?
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.