It\'s been a while since I have had to write C++ code and I\'m feeling kind of stupid. I\'ve written code that is similar to, but is not exactly, the code b
You can cast using Single Argument Constructor: i.e. (A parent works, A child Studies) as follows:
#include
using std::cout;
class Parent
{
public:
void goToWork()
{
cout<<"working\n"; // only parents work
}
};
class Child : public Parent
{
public:
Child(const Parent& parentAddr){}
void goToSchool()
{
cout<<"studying\n"; // only children studies
}
};
int main(void)
{
Child child(*(new Parent()));
// here's a child working
child.goToWork();
return 0;
}
you pass an child class address as parent's constructor parameter and you can use a child obj to do parent's stuff