Lecture 3. Inheritance Objective: In this lecture unit, we show:
class Derived : public Base { // Base members unlisted are inherited unchanged // except for constructor, destructor, copy // constructor, and operator= public: // new public members // Base members which are to be changed private: // new private members // Base members disabled in Derived };
class Employee { public: Employee(); void get_name(string& name) const; int get_number() const; void change_name(const string& new_name); void change_number(int new_number); private: string name; int number; }; class HourlyEmployee : public Employee { public: HourlyEmployee(); double get_wage_rate() const; void change_wage_rate(double new_rate); private: double wage_rate; };
============================================= Inheritance Base Class Derived Class Type Member Access Member Access ============================================= public public public protected protected private private --------------------------------------------- public protected protected protected protected private (inaccessible) --------------------------------------------- public private private protected private private (inaccessible) ---------------------------------------------
Mark each of the following numbered statements as legal (Y) or illegal (N):
class A { public: int aPubVar; private: int aPrivVar; protected: int aProtVar; }; class B : public A { public: int bPubFunc(); int bPubVar; protected: int bProtFunc(); int bProtVar; }; class C : protected B { private: int cPrivFunc(); }; int B::bPubFunc() { ... (1) aPubVar = 0; (2) aPrivVar = 0; (3) aProtVar = 0; ... } int C::cPrivFunc() { ... (4) aPubVar = 0; (5) aPrivVar = 0; (6) aProtVar = bProtFunc(); ... } main() { ... C cObj; (7) cObj.aPubVar = cObj.bPubFunc(); (8) cObj.aProtVar = cObj.bPubFunc(); (9) cObj.bPubVar = cObj.bProtFunc(); (10) cObj.bProtVar = cObj.bProtFunc(); ... }
C++ defines in <stdexcept> the class exception. There areseveral kinds of exceptions, including bad_alloc and bad_cast. They are derived classes of exception. All exception classes have the what method which can be used to return a (primitive) string that details an error message.
Object& operator[](int index) { if (index < 0 || index >= size()) { throw BadIndex(index, size()); } return objects[index]; } class BadIndex : public exception { public: BadIndex(int idx, int sz) : index(idx), size(sz) {} int getIndex() const { return index; } int getSize() const { return sz; } private: int index; int size; }; int main() { NewVectorv(10); try { for (int i = 0; i <= v.size(); i++) v[i] = 0; } catch(const BadIndex& e) { cout << e.what() << ", index=" << e.getIndex() << ", size=" << e.getSize() << endl; } return 0; }
class Workaholic : public Worker { public: void doWork() { Worker::doWork(); drinkCoffee(); Worker::doWork(); } };
Worker w; Workaholic wh; ... w.doWork(); wh.doWork();
Worker *wptr; cin >> x; if (x != 0) wptr = new Workaholic(); else wptr = new Worker(); ... wptr->doWork(); // Which doWork to use?
class exception { public: exception(); exception(const exeception& rhs); virtual ~exception(); const exception& operator=(const exception& rhs); virtual const char *what(); private: // implementation dependent };