Object Oriented Programming in C Hierarchy Inheritance MAITRAYEE
Object Oriented Programming in C++: Hierarchy / Inheritance MAITRAYEE MUKERJI
Hierarchy �“Hierarchy is a ranking or ordering of abstractions” (Booch et. al. ) �The two most important hierarchies in a complex system are its class structure (the “is a” hierarchy) inheritance its object structure (the “part of” hierarchy) aggregation
Inheritance �The most important “is a” hierarchy is called as inheritance a bear “is a” kind of mammal, a house “is a” kind of tangible asset, and a quick sort “is a” particular kind of sorting algorithm.
Inheritance Mammal Superclass/ Base Class is-a Bear Subclass/ Derived Class
Single Inheritance
Single Inheritance
Single Inheritance
Multiple Inheritance
Multiple Inheritance Horse Eagle int age; int weight; int color; int age; int weight; int wingspan; gallop() canter() fly() hunt () Flying Horse int int age; weight; color; wingspan; gallop() fly()
Inheritance �Defines a relationship among classes one class shares the structure or behavior defined in one or more classes the process of creating new classes, called derived classes, from existing or base classes �Represents a hierarchy of abstractions, in which a subclass inherits from one or more superclasses. a subclass augments or redefines the existing structure and behavior of its superclasses.
Inheritance �Inheritance implies a generalization/specialization hierarchy, wherein a subclass specializes the more general structure or behavior of its superclasses. �Implementation of modularity/ hierarchy / code reuse
INHERITANCE in C++ �Derived class may be larger Extension: Extend the functionality of the class � Reuse the code written for functions of the base class, but new functions are added to extend the functionality Specialization: specialize the functionality of the class � Inherits all members of the base class � Inherited functions work as it is, or correct functionality is implemented by overiding the function
Inheritance: Example of Extension Counter count Counter() Counter (int) Get_Count() Operator ++() Count. Dn Operator -- () The direction of the arrow emphasizes that the derived class refers to the functions and data in the base class, while the base class has no access to the derived class
// object represents a counter variable // demonstrating inheritance Class Counter { protected: //And not private unsigned int count; public: Counter () { count = 0 } ; Counter (int c) { count = c) } unsigned int get_count() { return count; } Counter operator ++ () { return Counter (++count); } }
class Count. Dn : public Counter { public: Counter operator - - () { return Counter (- - count); } int main () { Count. Dn //derived class The keyword public is used for indicating inheritance Count. Dn is derived from the base class Counter C 1; cout << “n C 1=“ << C 1. get. Count(); ++C 1; cout << “n C 1=“ << C 1. get. Count(); -- C 1; --C 1; cout << “n C 1=“ << C 1. get. Count(); cout << endl; return 0 }
Accessibility Base Class Public Object Base Protected Private Derived Class Public Protected Private Object Derived
Inheritance and Accessibility Access Specified Accessible from Own Class Accessible from Derived Class Accessible from Objects outside Class public Yes Yes protected Yes No private Yes No No
Inheritance: Constructors & Destructors �When a derived class is constructed, it is the responsibility of this class’s constructor to take care that the appropriate constructor is called for its base constructor. Person: : Person(const string& nm, const string& id) �: name (nm) � : id. Num (id) { } Student: : Student (cnst string& nm, const string& id, const string& maj, int year) �: Person (nm, id), � Major (maj), � grad. Year(year) { }
Inheritance: Constructors & Destructors �Classes are destroyed in the reverse order from their construction, with derived classes destroyed before base classes Person: : ~Person() {…. } Student: : ~Student() {…. } Delete s; // calls ~Student() then ~Person()
Function Overiding �When same function exists in both base class and derived class, the function in the derived class will be executed for objects of the derived class. �Objects of the base class don’t know anything about the derived class and will always use the base class functions �Derived class functions are said to overide base class functions
Example class Base. A { public: int a; void print. Data () { cout << "Printing Data In Base Class: " << a << endl; } void print. Msg () { cout << "Printing Msg in Base Class: " << "Hello World" << endl; } }; class Derived. A : public Base. A { public: void print. Data (){ cout << "Printing Data In Derived Class: " << a << endl; } };
Example …contd int main () { Base. A obj; obj. a = 10; obj. print. Data(); obj. print. Msg(); return 0; } Output Printing Data In Base Class: 10 Printing Msg in Base Class: Hello World
Example. . contd. int main () { Derived. A obj; obj. a = 10; obj. print. Data(); obj. print. Msg(); return 0; } Output Printing Data In Derived Class: 10 Printing Msg in Base Class: Hello World
Static Binding Vs Dynamic Binding �In general, the derived class can be used where ever the base class is acceptable Person* pp[100] Pp[0] = new Person( …) Pp [1] = new Student (…) //array of 100 Person pointers // add a Person // add a student Cout << pp[1] -> getname() << ‘n’ Pp[0]->print(); Pp[1]->print() Pp[1]->change. Major(“English”); //okay //calls Person: : print() //also calls Person: : print() (!!) // ERROR
Static Binding Vs Dynamic Binding �Anomalous behavior can be attributed to static binding �Used as default by C++ to determine which member function to call for a derived class �It consider an object’s declared type, not its actual type. Since pp[1] is declared to be a pointer to a Person, the members for that class are used.
Static Binding Vs Dynamic Binding �In dynamic binding, an object’s contents determine which member function is called �To specify that a member function should use dynamic binding, the keyword “virtual” is added to the function’s declaration.
Dynamic Binding declare the print function to be virtual. class Person { // Person (base class) virtual void print() {. . . } // print (details omitted) }; class Student : public Person { virtual void print() {. . . } }; Person* pp[100]; pp[0] = new Person(. . . ); pp[1] = new Student(. . . ); pp[0]−>print(); pp[1]−>print(); // Student (derived from Person) // print (details omitted) // array of 100 Person pointers // add a Person (details omitted) // add a Student (details omitted ) // calls Person: : print() // calls Student: : print()
Virtual Destructors �If a base class defines any virtual functions, it should define a virtual destructor, even if it is empty.
- Slides: 28