Presentation is loading. Please wait.

Presentation is loading. Please wait.

MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.

Similar presentations


Presentation on theme: "MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance."— Presentation transcript:

1 MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance

2 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

3 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.

4 Inheritance Bear Mammal is-a Subclass/ Derived Class Superclass/ Base Class

5 Single Inheritance

6

7

8 Multiple Inheritance

9 Horse int age; int weight; int color; gallop() canter() Eagle int age; int weight; int wingspan; fly() hunt () Flying Horse int age; int weight; int color; int wingspan; gallop() fly()

10 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.

11 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

12 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

13 Inheritance: Example of Extension Counter count Counter() Counter (int) Get_Count() Operator ++() CountDn 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

14 // 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);} }

15 class CountDn : public Counter //derived class { public: Counter operator - - () { return Counter (- - count); } int main () { CountDnC1; cout << “\n C1=“ << C1.getCount(); ++C1; ++C1; ++C1; cout << “\n C1=“ << C1.getCount(); -- C1; --C1; cout << “\n C1=“ << C1.getCount(); cout << endl; return 0 } The keyword public is used for indicating inheritance CountDn is derived from the base class Counter

16 Accessibility Base Class Public Protected Private Derived Class Public Protected Private Object Base Object Derived

17 Inheritance and Accessibility Access SpecifiedAccessible from Own Class Accessible from Derived Class Accessible from Objects outside Class publicYes protectedYes No privateYesNo

18 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)  : idNum (id) { }  Student:: Student (cnst string& nm, const string& id, const string& maj, int year)  : Person (nm, id),  Major (maj),  gradYear(year) { }

19 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()

20 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

21 Example class BaseA { public: int a; void printData () { cout << "Printing Data In Base Class: " << a << endl; } void printMsg () { cout << "Printing Msg in Base Class: " << "Hello World" << endl;} }; class DerivedA : public BaseA { public: void printData (){ cout << "Printing Data In Derived Class: " << a << endl;} };

22 Example …contd int main () { BaseA obj; obj.a = 10; obj.printData(); obj.printMsg(); return 0; } Output Printing Data In Base Class: 10 Printing Msg in Base Class: Hello World

23 Example..contd. int main () { DerivedA obj; obj.a = 10; obj.printData(); obj.printMsg(); return 0; } Output Printing Data In Derived Class: 10 Printing Msg in Base Class: Hello World

24 Static Binding Vs Dynamic Binding In general, the derived class can be used where ever the base class is acceptable  Person* pp[100] //array of 100 Person pointers  Pp[0] = new Person( …)// add a Person  Pp [1] = new Student (…) // add a student  Cout getname() << ‘\n’ //okay  Pp[0]->print(); //calls Person:: print()  Pp[1]->print() //also calls Person:: print() (!!)  Pp[1]->changeMajor(“English”); // ERROR

25 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.

26 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.

27 Dynamic Binding declare the print function to be virtual. class Person { // Person (base class) virtual void print() {... } // print (details omitted) }; class Student : public Person { // Student (derived from Person) virtual void print() {... } // print (details omitted) }; Person* pp[100]; // array of 100 Person pointers pp[0] = new Person(...); // add a Person (details omitted) pp[1] = new Student(...); // add a Student (details omitted ) pp[0]−>print(); // calls Person::print() pp[1]−>print(); // calls Student::print()

28 Virtual Destructors If a base class defines any virtual functions, it should define a virtual destructor, even if it is empty.


Download ppt "MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance."

Similar presentations


Ads by Google