1 / 8

ENERGY 211 / CME 211

ENERGY 211 / CME 211. Lecture 22 November 10, 2008. Specializing Classes. Often, classes have similar operations or data Maintaining such classes separately is wasteful

dean
Télécharger la présentation

ENERGY 211 / CME 211

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. ENERGY 211 / CME 211 Lecture 22 November 10, 2008

  2. Specializing Classes • Often, classes have similar operations or data • Maintaining such classes separately is wasteful • Can put common functionality in a base class, and put class-specific features in derived classes that inherit from the base class • Then, further extensions in new directions can easily be made

  3. Derived Classes • The declaration classderived-class:access-specbase-class{ access-specifier1:decl-list1 access-specifier2:decl-list2 … }; allows derived-class to inherit from base-class, according to access-spec • Do not re-declare inherited members unless you intend to override them

  4. Inheritance • Normally, inheritance is public • public members of the base class are available in the derived class • private members of the base class are not available to the derived class • A public or protected member variable cannot be declared in both • A public/protected member function can be, but don't do it unless base function is virtual (coming up)

  5. Protected members • protected members are available only to derived classes, and are considered protected in the derived class as well • Using the protected specifier is the way to allow inheritance for members that would normally be private if declared in the derived class • Use private for members that are used only in their own class

  6. Constructors • Constructors for derived classes can be implemented in the same way as for base classes • They first call the default constructor for the base class (that has no arguments) • Can select a different base constructor • Form: derv::derv(derv-args): base(base-args)compound-stmt where derv is the derived class and base is the base class

  7. Virtual Functions • To have member function in derived class override that of base class, declare base class function virtual: virtual type name(args); • Always make base destructor virtual, or derived destructor may not be called! • Add =0; to end of virtual declaration to make function pure virtual; this makes base class an abstract class (can't create objects)

  8. Next Time All about templates • Template functions • Template classes • Specializing Templates

More Related