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

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

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
6 2462

"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
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
I think you are a newer,you have a long way to go.

Apr 20 '06 #4
> #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

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
海风 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: Christian Engström | last post by:
When I compile the below program with Microsoft Visual C++ version 6, I get the results I expect, which is that the program should write out base() derived() base() derived(derived) When I...
3
by: Elia Karagiannis | last post by:
The static constructor of a derived class never gets called if it has no other methods and the base class is only static I have the following base class and derived class. public class MyBase...
6
by: Edward Diener | last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to specify that a property, method, event, or field's access can be changed in a derived class, ie. is protected in one class and...
10
by: benliu | last post by:
Is there an easy/special way to turn a base object into a derived object? So for example, given the following: class MyString : String { .... }
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
4
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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.