Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > derived class incresing the sopce of base class methods and explicit call to constructor

Reply
Thread Tools

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

 
 
Taran
Guest
Posts: n/a
 
      04-19-2006
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

 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      04-19-2006

"Taran" <> skrev i meddelandet
news: 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-19-2006
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


 
Reply With Quote
 
=?utf-8?B?5rW36aOO?=
Guest
Posts: n/a
 
      04-20-2006
I think you are a newer,you have a long way to go.

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      04-20-2006
> #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
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      04-20-2006

benben wrote in message <44470981$0$7527$>. ..
>>
>> 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-20-2006
海风 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57