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