C++ Handling My own Exception class -
i'm having problems display strings have in subclasses. i'm trying function i'm not sure why don't content of these strings.
class employee{ string fn, ln, jt; double income; public: char const *getters(){ return fn.data(), ln.data(), jt.data(); //=========>getting 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 null"); if(ln==0) throw exception(2, "sorry, last name null"); if(jt==0) throw exception(3, "sorry job title null"); if(inc<=0) throw exception(4, "sorry, income 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 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 strings cout << acc[i]->getaccess() << endl; } } catch(exception acc){ cout << "err:" << acc.geterrcode() << " mess:" << acc.geterrmess() << endl; } return 0; }
so, i'm guessing function not doing want, display first name , last name. doing wrong?
i don't point of mixing char*
, string
. prefer later.
this compile
char const *getters(){ return fn.data(), ln.data(), jt.data(); }
but want is
char const *getters(){ return (fn + ln + jt).data(); }
i re-write program 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 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; }
Comments
Post a Comment