0

I'm having problems to display some strings that I have in my subclasses. I'm trying to do it with a function but I'm not sure why I don't get the content of these strings.

class Employee{
    string FN, LN, JT;
    double Income;

public:
    char const *getters(){
        return FN.data(), LN.data(), JT.data(); //=========>getting the content of strings
    }
    virtual char const *getAccess()=0;
    Employee(char const *fn, char const *ln, char const *jt, double inc){

        if(fn==0) throw Exception(1, "Sorry, First Name is Null");
        if(ln==0) throw Exception(2, "Sorry, Last Name is Null");
        if(jt==0) throw Exception(3, "Sorry Job Title is Null");
        if(inc<=0) throw Exception(4, "Sorry, The Income is Null");

        FN=fn;
        LN=ln;
        JT=jt;
        Income=inc;
    }
};

class Programmer: public Employee{
public:
    Programmer(char const *fn, char const *ln, double inc):
        Employee(fn,ln,"Programmer", inc)
    {}
    char const *getAccess(){
        return "You have access to Meeting Room + Development Office";
    }
};

//=========The Main============
int main(){
    Employee *acc[3];

    try{
        acc[0]=new Programmer("Juan", "Villalobos", 60000);
        acc[1]=new Director("Jorge", "Villabuena", 70000);
        acc[2]=new ProdSupport("Pedro", "Villasmil", 80000);
        for(int i=0; i<3; i++){
            cout << acc[i]->getters() << endl;    //=============>Displaying the strings
            cout << acc[i]->getAccess() << endl;
        }
    } catch(Exception acc){
        cout << "Err:" << acc.getErrCode() << " Mess:" << acc.getErrMess() << endl;
    }

    return 0;
}

So, I'm guessing that my function is not doing what I want, which is display the first name and last name. What am I doing wrong?

6
  • 1
    This is so wrong I am not sure where to start... Commented Aug 12, 2013 at 22:37
  • I forgot to say that it only gets the last string which in this case is Job Title.. equal to JT.data() Commented Aug 12, 2013 at 22:37
  • 3
    What gave you the idea that you can return a list of strings as you are trying to do? There is only one return value from a function. Commented Aug 12, 2013 at 22:38
  • And data() is wrong for returning a string. Use c_str or there is no null termination guarantee. Commented Aug 12, 2013 at 22:40
  • 1
    And always catch exceptions as const Exception &acc otherwise it makes a copy, slices the exception class to just the base, and other weird, nasty things. Commented Aug 12, 2013 at 22:41

2 Answers 2

1

I don't get the point of mixing char* and string. Prefer the later.

This does compile

char const *getters(){
    return FN.data(), LN.data(), JT.data();
}

but what you probably want is

char const *getters(){
    return (FN + LN + JT).data();
}

I would re-write your program like this:

class Employee{
    string FN, LN, JT;
    double Income;

public:
    string getters(){
        return FN + " " + LN + " " + JT;
    }

    virtual string getAccess()=0;

    Employee(string const &fn, string const &ln, string const &jt, double inc) :
        FN(fn), LN(ln), JT(jt), Income(inc)
    {
    }
};

class Programmer: public Employee{
public:
    Programmer(string const &fn, string const &ln, double inc):
        Employee(fn,ln,"Programmer", inc)
    {}

    string getAccess(){
        return "You have access to Meeting Room + Development Office";
    }
};

//=========The Main============
int main()
{
    std::vector<Employee> acc;

    acc.push_back(Programmer("Juan", "Villalobos", 60000));
    acc.push_back(Director("Jorge", "Villabuena", 70000));
    acc.push_back(ProdSupport("Pedro", "Villasmil", 80000));

    for(size_t i=0; i<acc.size(); i++){
        cout << acc[i].getters() << endl;
        cout << acc[i].getAccess() << endl;
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I would go a step further and change the for loop to use an iterator instead of an index.
Thanks a lot man.. you really extremely helped me.. I really appreciate your help..
1

The result of the , comma operator is the right-hand value, so return FN.data(), LN.data(), JT.data(); is effectively the same as return JT.data();, which is what you are seeing.

To do what you are attempting, try this instead:

std::vector<std::string> getValues() const {
    std::vector<std::string> arr(3);
    arr.push_back(FN);
    arr.push_back(LN);
    arr.push_back(JT);
    return arr;
}

std::vector<std::string> arr = acc[i]->getValues();
for (std::vector<std::string>::const_iterator iter = arr.begin(), end = arr.end(); iter != end; ++iter) {
    cout << *iter << " ";
}
cout << endl;

Or, move the cout logic into the class itself:

void displayValues() const {
    cout << FN << " " << LN << " " << JT << endl;
    cout << getAccess() << endl;
}

for(int i=0; i<3; i++){
    acc[i]->displayValues();
}

1 Comment

Yeah, exactly but how can I make it work and get those 3 strings values from my function and return them to display the content.. ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.