150 likes | 164 Views
Discover the intricacies of inheritance in C++, including base class tracking, visibility levels, constructor and destructor execution, message passing, and class variables. Unravel the effects and benefits of inheritance, order of event execution, and instance versus class variables. Dive into inheritance examples and learn the significance of encapsulation in object-oriented programming.
E N D
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++ • 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
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
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
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
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
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)
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 ?
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
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
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
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
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: ... };
Class Variables in C++ • And here is the initialization of the class variable // Initialize the class variable string computer::brand = “IBM”;
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