1 / 21

Inheritance

Inheritance. A C++ class can inherit from another (base) class. This implements the (in)famous ‘IS A’ relation. Everything (attributes and operation) is inherited, except: Constructors (including copy-constructor) Destructor Assignment operator Friends. Inheritance - constructors.

hali
Télécharger la présentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance • A C++ class can inherit from another (base) class. • This implements the (in)famous ‘IS A’ relation. • Everything (attributes and operation) is inherited, except: • Constructors (including copy-constructor) • Destructor • Assignment operator • Friends

  2. Inheritance - constructors The default constructor (when created!) is empty, when means that will call the default constructors of all ‘subparts’ (attributes and base classes). In a constructor you write yourself you should probably call a constructor for a base class. If you don not, the default constructor will be called. Why is that probably not what you want?

  3. Inheritance – destructors The destructors for base class(es) are automatically called at the end of a destructor of the subclass. Q: Why at the end, not at the beginning?

  4. Inheritance – assignment When you don’t create an assignment operator the compiler will. It will call the assignment operators of all base classes (and attributes). When you do create your own assignment operator, you must call the assignment operators of the base classes. Q: what happens if you forget?

  5. Inheritance – friends Friends are not inherited. You will have to declare any friends explicitly. They could even be your base classes’ friends.

  6. Inheritance – use • When C is a subclass of B: • Where a B object is required, a C object will do • Where a B pointer is required, a C pointer will do • Where a B reference is required, a C reference will do • Note that this includes all versions of parameter passing, including passing the parameter to the assignment operator.

  7. Inheritance – use examples D is derived from B (B is the base class). B has a void B::mb( B & b ) D has a void D::mb( D & d ) B b; D d; b = d; // OK d = b; // NOT OK B * pb = new B; D * pd = new D; pb = pd; // OK pd = pb; // NOT OK pb->mb(d); // OK pd->md(b); // NOT OK What exactly happens in each allowed case?

  8. An alarmclock class AlarmClock : public Clock { public: // Public operations. void setAlarm (int hour, int min, int sec); void tick (void); protected: // Internal representation of the alarm time. int ah, am, as; }; Add 3 attributes, override tick()

  9. An alarmclock void AlarmClock::setAlarm (int hour, int min, int sec){ ah = hour; am = min; as = sec; } void AlarmClock::tick (void){ Clock::tick (); if( ( h == ah ) && ( m == am ) && ( s == as ) ){ cout << "\a\a\a" << endl; } } Just tick() would be our own tick.

  10. Classes : Person and Employee class Person { public: // Constructor. Person (void); // Public operations. void changeName (const char * n); const char * isCalled (void) const; void writeInfo (void) const; private: char name[30]; }; class Employee : public Person { public: // Constructor. Employee (void) : salary (0) { } // Public operations. long earns (void) const; void changeSalary (long s); void writeInfo (void) const; private: long salary; };

  11. Classes : Programmer and Temporary class Programmer : public Employee { public: // Constructor. Programmer (void); // Public operations. void changeFavorite (const char * f); const char * readFavorite (void) const; void writeInfo (void) const; private: char fav[10]; }; class TempEmpl : public Employee { public: // Public operations. void changePeriod (int yf, int mf, int df, int yt, int mt, int dt); void writeInfo (void) const; private: Date from, to; }; 

  12. Implementation : Person class Person { public: // Constructor. Person (void); // Public operations. void changeName (const char * n); const char * isCalled (void) const; void writeInfo (void) const; private: char name[30]; }; Why not just strcpy() ? void Person::changeName (const char * n){ strncpy( name, n, 29 ); name[29] = '\0'; } const char * Person::isCalled (void) const { return name; } void Person::writeInfo (void) const { cout << "Name: " << name << endl; }

  13. Implementation : Employee class Employee : public Person { public: // Constructor. Employee (void) : salary (0) { } // Public operations. long earns (void) const; void changeSalary (long s); void writeInfo (void) const; private: long salary; }; void Employee::changeSalary (long s){ salary = s; } long Employee::earns (void) const { return salary; } void Employee::writeInfo (void) const { Person::writeInfo (); cout << "Salary: " << salary << endl; }

  14. Implementation : Programmer class Programmer : public Employee { public: // Constructor. Programmer (void); // Public operations. void changeFavorite (const char * f); const char * readFavorite (void) const; void writeInfo (void) const; private: char fav[10]; }; Programmer::Programmer (void){ fav[0] = '\0'; } void Programmer::changeFavorite (const char * f){ strncpy (fav, f, 9); fav[9] = '\0'; } const char * Programmer::readFavorite (void) const { return fav; } void Programmer::writeInfo (void) const { Employee::writeInfo (); cout << "Favorite language: " << fav << endl; }

  15. Implementation : TempEmpl class TempEmpl : public Employee { public: // Public operations. void changePeriod (int yf, int mf, int df, int yt, int mt, int dt); void writeInfo (void) const; private: Date from, to; }; void TempEmpl::changePeriod (intyf, int mf, intdf, intyt, intmt, intdt ){ from = Date (yf, mf, df); to = Date (yt, mt, dt); } void TempEmpl::writeInfo (void) const { Employee::writeInfo (); cout << "From " << from << " to " << to << endl; }

  16. Person again – allow long names class Person { public: // Constructors. Person (const char * n = ””); Person (const Person & p); // Destructor ~Person( void ){ delete [] name; } // Assignment operator. const Person & operator= ( const Person & rhs); // Public operation. void writeInfo (void) const; private: char * name; }; Person::Person (const char * n){ name = new char[strlen(n) + 1]; strcpy (name, n); } Person::Person (const Person & p){ name = new char[strlen(p.name) + 1]; strcpy (name, p.name); } const Person & Person::operator= ( const Person & rhs ){ if (this != &rhs){ delete [] name; name = new char[strlen(rhs.name) + 1]; strcpy (name, rhs.name); } return *this; }

  17. Employee again class Employee : public Person { public: // Constructor. Employee (const char * n = “”, long s = 0) : Person (n), salary (s) { } // Public operation. void writeInfo (void) const; private: long salary; }; Why is employee so simple (compared to person)?

  18. Programmer again – allow long names class Programmer : public Employee { public: // Constructor. Programmer ( const char * n = “”, long s = 0, const char * f = “” ); Programmer (const Programmer & p); // Destructor ~Programmer (void) { delete [] fav; } // Assignment operator. const Programmer & operator= ( const Programmer & r); // Public operation. void writeInfo (void) const; private: char * fav; }; Programmer::Programmer ( const char * n, long s, const char * f ): Employee ( n,s ) { fav = new char[strlen(f) + 1]; strcpy (fav, f); }

  19. Programmer again – allow long names class Programmer : public Employee { public: // Constructor. Programmer ( const char * n = “”, long s = 0, const char * f = “” ); Programmer (const Programmer & p); // Destructor ~Programmer (void) { delete [] fav; } // Assignment operator. const Programmer & operator= ( const Programmer & r); // Public operation. void writeInfo (void) const; private: char * fav; }; Programmer::Programmer ( const Programmer & p ) : Employee (p) { fav = new char[ strlen(p.fav) + 1]; strcpy (fav, p.fav); }

  20. Programmer again – allow long names class Programmer : public Employee { public: // Constructor. Programmer ( const char * n = “”, long s = 0, const char * f = “” ); Programmer (const Programmer & p); // Destructor ~Programmer (void) { delete [] fav; } // Assignment operator. const Programmer & operator= ( const Programmer & r); // Public operation. void writeInfo (void) const; private: char * fav; }; const Programmer & Programmer::operator= ( const Programmer & rhs ){ if (this != &rhs){ Employee::operator=(rhs); delete [] fav; fav = new char[strlen(rhs.fav) + 1]; strcpy (fav, rhs.fav); } return *this; }

More Related