Inheritance Chapter 2, Slides of Object Oriented Programming

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

2021/2022

Available from 12/03/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 86

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
Improved efficiency and greater robustness
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56

Partial preview of the text

Download Inheritance Chapter 2 and more Slides Object Oriented Programming in PDF only on Docsity!

What is Inheritance?

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

Terminology

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.

Terminology - A Classification

Hierarchy

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

Wheel ^

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?

Line

Attributes:

start position

end position

Methods:

draw

Coloured Line

Attributes:

colour

Methods:

set colour

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 using namespace std; class Counter{ // base class protected: // NOTE: not private unsigned int count; // count public: Counter() : count(0) { } // no-arg constructor Counter(int c) : count(c) { } // 1-arg constructor unsigned int get_count() const // return count { return count; } Counter operator ++ () // incr count (prefix) { return Counter(++count); } };

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 using namespace std; class Counter{ protected: // NOTE: not private unsigned int count; // count public: Counter() : count(0) {} // constructor, no args Counter(int c) : count(c) {} // constructor, one arg unsigned int get_count() const // return count { return count; } Counter operator ++ () // incr count (prefix) { return Counter(++count); } }; class CountDn : public Counter{ public: CountDn() : Counter() {} // constructor, no args