Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lab #4 Inheritance
LAB # 4 INHERITANCE
OBJECTIVE
To be able to create new classes by inheriting from existing classes. To understand how inheritance promotes software reusability. To understand the notions of base classes and derived classes.
THEORY
Inheritance is the process of creating new classes, called derived classes, from existing or base classes. The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own. The base class is unchanged by this process. Inheritance is an essential part of OOP. Classes create a hierarchy, which is usually shown as a tree, where base classes are higher in the tree. Inheritance can be used for: 1) Add data and code to a class without having to change the original class. 2) Reuse code 3) Change the behavior of class.
Lab #4 Inheritance
class counter //base class { protected: //Note: not private unsigned int count; public: counter( ) { count = 0; } counter(int c) { count = c; } int get_count( ) { return count; } counter operator ++ () { return counter(++count); } }; class countdown : public counter //derived class { public: countdown() : counter() // derived class constructor { } countdown(int c) : counter(c) //derived class constructor { } counter operator -() { return counter(--count); } //decrement count }; void main( ) { countdown c1; countdown c2(100); cout <<\nc1= <<c1.get_count( ); cout<<\nc2= <<c2.get_count( ); ++c1; ++c1 ; ++c1; cout<<\nc1= <<c1.get_count( ); --c2; --c2; cout<<\nc2= <<c2.get_count( ); }
The ++operator, the constructors, the get_count()function in the counter class, and the - operator in the countdown class all work with objects of type countdown SPECIFYING THE DERIVED CLASS: In the above program you can notice that following the counter class in the listing is the specification for a new class, countdown. This class incorporates a new function, operator--( ), which decrements the count. The first line of countdown specifies that it is derived from counter class.
class countdown : public counter
Lab #4 Inheritance
Notice that here we use a single colon instead of double colon used for the scope resolution operator, which is followed by the keyword public and the name of the base class counter. This line states that countdown class is derived from the base class counter. ACCESSING BASE CLASS MEMBERS: It is very important to know when a member function in a base class can be used by object of the derived class. This is called accessibility. Substituting base class member functions: The object c1 of the countdown class is accessing or calling the operator++()and get_count() functions. Now you may notice that there are no such functions in the countdown class specification. In this situation the compiler uses an appropriate function from the base class. THE PROTECTED ACCESS SPECIFIER: The data in a class we have looked so far , have used the private access specifier. This is fine if we dont use inheritance. With inheritance, however, the member functions of the derived classs need to access data member of the base class to achieve reusability. The member funcitons cannot access private members. Also we dont want to make data members public, since that would allow it to be accessed by any funciton anywhere in the program and eliminate the advantages of data hiding. That is the reason the count data member of counter class is not made private instead it is given a new specifier: protected. . A protected member can be accessed by member functions in its own class or in any clas derived from its own class. It cannot be accesed from functions outside these classes. If you are writing a class that you suspect might be used, at any point in the future, as a base class for other classes, then any data or functions that the derived classes might need to access should be made protected rather than private. This ensures that the class is inheritance ready. Base Class Unchanged: It should be remembered that even if other classes have been derived from it, the base class remains inchanged. Note also that inheritance doesnot work in reverse. The base class and its object dont know anything about derived classes. DERIVED CLASS CONSTRUCTORS The above program uses two constructors in the countdown class. These constructors have an unfamiliar feature: the colon followed by a function name.
countdown( ) : counter( ) { }
This construction causes countdown() constructors to call the counter() constructors in the base class.
Lab #4 Inheritance
The answer is: when the same function exists in both the base class and the derived class, the function in the derived class will be executed.(this is true for the object of derived class). We say that derived class function overrides the base class function. The sample program given below demonstrates the concept of overriding member function and class hierarchy: Program 2:
# include <constream.h> class employee { protected: char name[20]; int emplno; public: void getdata() { cout<<"\nEnter name of employee ";cin>>name; cout<<"\nEnter employee number ";cin>>emplno; } void showdata() { cout<<"\n\n Name "<<name; cout<<"\n No. "<<emplno; } }; class instructor:public employee { char title[20]; int nofclass; public: void getdata() { employee::getdata(); cout<<"\nEnter title ";cin>>title; cout<<"\nEnter Total number of classes ";cin>>nofclass; } void showdata() { employee::showdata(); cout<<"\n Title is "<<title; cout<<"\n No of classes he/she takes "<<nofclass; } }; void main () { instructor i1; i1.getdata(); i1.showdata(); }
Lab #4 Inheritance
SCOPE RESOLUTION WITH OVERRIDDEN FUNCTIONS: The functions getdata() and showdata() in instructor class use the scope resolution operator to access getdata() and showdata() in employee class. The statements
employee::getdata(); and employee::showdata();
specify that the getdata() and showdata() functions in employee are to be called. Without the scope resolution operator, the compiler would think the getdata() and showdata() functions in instructor class were calling themselves. ABSTRACT BASE CLASS: You may observe that we dont define any object of the base class employee. We use this as a general class whose sole purpose is to act as a base from which other classes are derived. Classes used only for deriving other classes are sometimes called Abstract Base Class, meaning that no actual objects of this class are created.
TYPES OF INHERITANCE:
C++ distinguishes two types of inheritance: public and private. As a default, classes are privately derived from each other. Consequently, we must explicitly tell the compiler to use public inheritance. The type of inheritance influences the access rights to elements of the various base classes. Using public inheritance, everything which is declared protected in a base class remains protected in a derived class. Similarly, everything which is public remains public. When using private inheritance the things are quite different. The objects of the derived class cannot access public member functions of the base class. Since objects can never access private and protected members of a class, the result is that no member of the base class is accessible to objects of the derived class.
Lab #4 Inheritance
LAB TASKS
L5_P1. Create program with the class hierarchy of employees given below. One abstract class Employee and all derived concrete classes are shown below. Exercise all of the following: a. Derived class constructor b. Protected access specifier c. Overriding Member functions d. Public and Private Inheritance Also observe the effect of all features above
Employee
Faculty
Manager
Laborer
Teachers
Data: Teachers: 1. 2. 3. 4. 5. 6. 7.
Lab Instructors
Lab Instructors: 1. ID Number 2. Name 3. Title 4. Department 5. No. of Labs per week. 6. Duty in Lab # ? 7. Salary Laborer: 1. ID Number 2. Name 3. Title 4. Duty area 5. Daily Wages
ID Number Name Title Department Number of Classes per week. Total No. of Subjects Package (rupees/hr)