Exceptional C++

【Exceptional C++(8)】代码的复杂性(二)

2018-01-29  本文已影响6人  downdemo

问题

String EvaluateSalaryAndReturnName(Employee e)
{
    if (e.Title() == "CEO" || e.Salary() > 100000)
    {
        cout << e.First() << " " << e.Last() << " is overpaid" << endl;
    }
    return e.First() + " " + e.Last();
}

解答

String EvaluateSalaryAndReturnName(Employee e)
{
    String result = e.First() + " " + e.Last();
    if (e.Title() == "CEO" || e.Salary() > 100000)
    {
        String message = e.First() + " " + e.Last() + " is overpaid\n";
        cout << message;
    }
    return result;
}
String theName;
theName = EvaluateSalaryAndReturnName(bob);
void EvaluateSalaryAndReturnName(Employee e, String& r)
{
    String result = e.First() + " " + e.Last();
    if (e.Title() == "CEO" || e.Salary() > 100000)
    {
        String message = e.First() + " " + e.Last() + " is overpaid\n";
        cout << message;
    }
    r = result;
}
shared_ptr<String> EvaluateSalaryAndReturnName(Employee e)
{
    shared_ptr<String> result = new String(e.First() + " " + e.Last());
    if (e.Title() == "CEO" || e.Salary() > 100000)
    {
        String message = e.First() + " " + e.Last() + " is overpaid\n";
        cout << message;
    }
    return result;
}
上一篇下一篇

猜你喜欢

热点阅读