Inheritance PDF
Inheritance PDF
Jussi Pohjolainen
TAMK University of Applied Sciences
Introduction to Inheritance
• Inheritance is a relationship between two or
more classes where derived class inherites
behaviour and attributes of pre-existing (base)
classes
• Intended to help reuse of existing code with
little or no modification
Inheritance
• Inheritance can be continous
– Derived class can inherit another class, which
inherits another class and so on
– When changing the base class all the derived
classes changes also
• Example:
– Mammal <– Human <– Worker <- Programmer
• Could mammal be a derived class? If so, what
would be the base class?
Picture about Inheritance
features: a,b
a
b
Class A
Class B Class C
Features: a,b,d,e,f
f
Class D
Multiple Inheritance
• In multiple inheritance a derived class has
multiple base classes
• C++ supports multiple base classes, Java don't
Driver Conductor
- license - Account number
House Boat - Year of approval
Taxi Driver
Houseboat - area
Inheritance and Capsulation
• private
– Is accessible only via the base class
• public
– Is accessible everywhere (base class, derived class,
othe classes)
• protected
– Is accessible by the base class and derived classes
Basic example
• What are Programmer's attributes and
methods?
Human
Programmer
string name
int salary
void sleep()
void implementApps()
void drink()
void beNerd()
void eat()
Overriding?
• What about now?
Human
Programmer
string name
int salary
void sleep()
void drink() void implementApps()
void eat() void beNerd()
void drink()
void eat()
Overriding
• Since programmer eats and drinks differently
than humans (only Coke and Pizza) the eat and
drink methods are overriden in Programmer!
Abstract Class
• Abstract class is a class which you cannot
instantiate (create objects)
• You can inherit abstract class and create
objects from the inherited class, if it is concrete
one
• Abstract class in C++ has abstract methods,
that do not have implementations
• These methods forces derived classes to
implement those methods
Example
<<abstract>>
Mammal
string name
Elephant
int trunkLength
makesound()
Example
<<abstract>>
Figure
int x, y
Circle Rect
}
Declaring Inheritance
class Figure
{
public:
int x, y;
};
int main()
{
Circle a;
a.x = 0;
a.y = 0;
a.radius = 10;
}
Encapsulation
class Figure
{
protected:
int x, y;
};
int main()
{
Circle a;
}
Inheritance and Constructors
• When creating a object from derived class, also
the member values of the base class must be
initialized
• Base constructor is called before the derived
classes constructor
• Destructors vice versa.
Calling the Base Classes constructor
class Figure class Circle : public Figure
{ {
public: public:
Figure() { Circle() : Figure() {
cout << "Figure cout << "Circle
Constructor\n"; Constructor\n";
} }
~Figure() { ~Circle() {
cout << "Figure cout << "Circle
Destructor\n"; Destructor\n";
} }
}; };
int main()
{
Circle a;
}
Calling the Base Classes constructor
class Figure
{
private:
int x_, y_;
public:
Figure(int x, int y) : x_(x), y_(y) {
cout << "Figure Constructor\n";
}
~Figure() {
cout << "Figure Destructor\n";
}
};
Calling the Base Classes constructor
class Circle : public Figure
{
private:
double radius_;
public:
Circle(int x, int y, int radius) : Figure(x, y),
radius_(radius)
{
cout << "Circle Constructor\n";
}
~Circle() {
cout << "Circle Destructor\n";
}
};
int main()
{
Circle a(0,0,5);
}
Abstract Class
• In C++, Abstract class is a class that has one
abstract method
• Abstract method is a method without
implementation.
• Abstract method is created by reserverd word
"virtual"
Example of Abstract class
class Figure
{
private:
int x_, y_;
public:
Figure(int x, int y) : x_(x), y_(y) {
cout << "Figure Constructor\n";
}
~Figure() {
cout << "Figure Destructor\n";
}