














































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
What is Inheritance? 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 more features into itself. Classes can inherit attributes (data members) and methods (member functions) from other classes. What is the purpose of Inheritance? Specialization: Extending the functionality of an existing class Generalization: sharing commonality between two or more classes.
Typology: Slides
1 / 86
This page cannot be seen from the preview
Don't miss anything!















































































Inheritance is the process of creating new classes, called derived classes , from existing or base classes.
The derived class inherit s all the capabilities of the base class but can add more features into itself.
Classes can inherit attributes (data members) and methods (member functions) from other classes. What is the purpose of Inheritance?
Specialization : Extending the functionality of an existing class
1
Derived class or subclass or child class : A class which inherits some of its attributes and methods from another class
Base class or superclass or parent class : A class from which another class inherits
Ancestor : A class’s ancestors are those from which its own super classes inherit
Descendant : A class’s descendants are those which inherit from its sub classes.
Building Commercial Public Domestic Office block Masjid Hospital Office block Apartment Block A ‘kind of’ Building (AKO) Factory A ‘kind of’ Commercial building (AKO) Arrow in diagram means ’inherits from’
Car Vehicle
A car is ‘a kind of’ vehicle car class can inherit from vehicle class Car
A wheel isn’t ‘a kind of’ car. A wheel is ‘a part of’ a car Designing your classification hierarchy: ‘A kind of’ or ‘a part of’?
What do objects inherit?
A ‘coloured line’ is a kind of line the coloured line class inherits all the attributes and methods of the line class and adds attributes and methods of its own An object of the ‘coloured line’ class has all the attributes and methods of the ‘line’ base class as well as the attributes and methods added by the derived class
Specialisation Extending the functionality of an existing class eg a coloured line is a specialised kind of line A class is both
closed in that it has an encapsulated, private part which cannot be affected by external manipulation
and open in that it allows itself to be used as part of a larger software unit.
Generalization Helpful to place common elements (attributes and methods) in a shared base class and organize problem into an inheritance hierarchy. Cow Whale Elephant Eagle Animal Mammal Bird
Generalization Sometimes this leads to the creation of abstract classes which can’t be instantiated directly Cow Whale Elephant Eagle Animal Mammal Bird Abstract classes
Derived Class and Base Class (1/2) // inheritance with Counter class #include
Derived Class and Base Class (2/2) class CountDn: public Counter{ // derived class public: Counter operator -- () // decr count (prefix) { return Counter(--count); } }; int main(){ CountDn c1; // c1 of class CountDn // CountDn c2(100); // Error cout << "\nc1=" << c1.get_count(); // display c ++c1; ++c1; ++c1; // increment c1,3times cout << "\nc1=" << c1.get_count(); // display it --c1; --c1; //decrement c1, twice cout << "\nc1=" << c1.get_count(); // display it cout << endl; system("PAUSE"); return 0; }
Generalization in UML Class Diagrams
In the UML, inheritance is called generalization, because the parent class is a more general form of the child class.
The child is more specific version of the parent.
In figure, the direction of the arrow emphasizes that the derived class refers to functions and data in the base class, while the base class has no access to the derived class.
Counter C1; C1.count = 5; // Error C1.get_count(); // OK
Derived Class Constructors (1/2) // constructors in derived class #include