1 / 15

CSCI-383

CSCI-383. Object-Oriented Programming & Design Lecture 14. Inheritance in C++. Base classes do not keep track of the classes derived from them. Rather, derived classes keep track of their base class(es) Handout #3 illustrates inheritance in C++

jorgey
Télécharger la présentation

CSCI-383

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. CSCI-383 Object-Oriented Programming & Design Lecture 14

  2. Inheritance in C++ • Base classes do not keep track of the classes derived from them. Rather, derived classes keep track of their base class(es) • Handout #3 illustrates inheritance in C++ • In this example, vectoris thebase class of vector3D (i.e., vector3D is derived from vector) • Note the keyword public that appears just before the name of the base class in the derived class definition • Also note that the data members of the base class are declared with a visibility level we have not discussed before: protected

  3. Base Class Visibility • In the example code given in handout #3, the base class is specified as a public base class. However, a base class can be declared as protected or private instead • If no visibility is specified for a base class, the default visibility is private

  4. Base Class Visibility • The visibility of the inherited members is determined by these rules: • public, protected, and private members of a public base class are inherited as public, protected, and private, respectively • public, protected, and private members of a protected base class are inherited as protected, protected, and private, respectively • public, protected, and private members of a private base class are inherited as private • Thus, the visibility of the base class specifies the “minimum encapsulation” (i.e., the greatest visibility

  5. Message Passing & Inheritance • When a message is passed to an object • First check to see if a method defined in the object’s class can be used. If so, use that definition • If not, pass the message to the parent class, and so on, until either an applicable method is found or the top of the inheritance hierarchy is reached

  6. The Effects of Inheritance • Recall that the constructors of data members are executed before the constructor of the class containing them. The same can be said about the constructor of the base class • Also recall that the destructors of data members are executed after the destructor of the class containing them. This is true for the destructor of the base class as well

  7. A Day in the Life … part II • The order of events now becomes • Creation (allocation) • Base class constructors are called • Component constructors are called • Constructor is called • Instance is manipulated • Destructor is called • Component destructors are called • Base class destructors are called • Destruction (deallocation)

  8. A Day in the Life … part II • For example class lightSource { ... }; class bulb: public lightSource { string manufacturer; dateSpan expectedLife; ...}; • The order of constructor execution is • lightSource • string (for manufacturer) • dateSpan (for expectedLife) • bulb • The order of destructor execution is • bulb • dateSpan (for expectedLife) • string (for manufacturer) • lightSource for vector3D in handout #3 ?

  9. More on Inheritance • Earlier it was said that if the constructor of a class does not explicitly call a constructor for a data member, then the default constructor of that data member will be called • This also holds true for the base class constructor: if the base class has a default constructor, then there is no need to reference it in the derived class constructor • However, one can choose explicitly which constructor should be called. This is illustrated in handout #3

  10. Member Location • Methods are located in classes, not in objects • That is, there is only one copy of each method, shared by all instances of the class • Although all data members are defined in classes, they have two possible physical locations • Instance variables are located within an object (i.e., within an instance of the class) • Class variables are located within the class

  11. Member Location • Here is an example of instance and class variables • Suppose that one wants to represent a class of objects named StFXPhone • The area code is a class variable, since a single value applies to all telephones on campus • The prefix and line numbers are instance variables, since a given prefix/line number pair applies only to a particular telephone • Why use class variables? • They reduce data redundancy

  12. Class Variables in C++ • By default, data members are instance variables • To transform a data member into a class variable, one declares it with the static modifier • Though class variables are declared within the class interface, they must be initialized globally

  13. Class Variables in C++ • Here is the interface of a class that contains a class variable class computer { float speed; ... static string brand; // class variable public: ... };

  14. Class Variables in C++ • And here is the initialization of the class variable // Initialize the class variable string computer::brand = “IBM”;

  15. Class Variables in C++ • Example: Write a StFXPhone class that has 2 class variables: one for the area code and another for the number of objects (i.e., phone numbers) that have been created

More Related