While I see some reasons behind your questions, I think you
need to improve the your example
you are saying that you need to return a non-null object
of type "Manager", while your define "SomeFunction(bool SomeCondition)"
to return "Employee".
if you are indeed going to return "Employee" object why bothering
initializing "Designation" while you will not be able to access it
later. For example:
cout << SomeFunction(true)->Designation(); // error !
So, I'm not sure what do mean by saying your examples work fine,
since the context is not clear.
** Comparing Implementation 1 and 2 / About dynamic casting
While both examples can improve, I think Implementation 1 is slightly
better. In both cases you do dynamic casting. However, in Implementation
1, you do an implicit upcasting in "Emp = Mng;", while in Implementation 2
you do downcasting in "Manager Mng = (Manager)Emp;".
In general you should avoid casting (especially the downcasting since it's
not that safe all the time compared with upcasting), and if you have to
you should use C++ style casting (e.g. dynamic_cast). See the
example in
https://www.tutorialcup.com/cplusplus/upcasting-downcasting.htm
A better solution is to use virtual functions in order to avoid
casting and make room to add more objects types beside "Manager".
For example, your header may look like:
class Employee
{
public:
virtual void setDesignation(const string & d) = 0;
virtual string getDesignation() = 0;
};
class Manager : public Employee
{
public:
virtual void setDesignation (const string & d) {Designation=d;}
virtual string getDesignation() {return Designation;}
private:
string Designation;
};
and your function may look like:
Employee* SomeFunction(bool SomeCondition)
{
Employee *Emp = NULL;
if (SomeCondition)
{
Emp = new Manager;;
Emp->setDesignation("BOSS");
}
return Emp;
}
then if you want to access the Designation later, you can do
cout << SomeFunction(true)->getDesignation();
** About Polymorphism
you don't use any Polymorphism in both examples. This is becuase you don't
use any function that is type-specific, and so your runtime behaviour doesn't
vary depending on the "Employee" object (you are merely using one object type
"Manager" anyways !). See the example in
http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm