c++ 05

Derived Classes
Outline
Definition
Virtual functions
Virtual base classes
Abstract classes. Pure virtual functions.
C++
2
Definition
class Derived : list-of-base-classes {
// new data member and member functions
};
The list of base classes is formed from:
public base_class
protected base_class
private base_class
C++
3
Example (base class list)
class ClassName : public C_1, …, public C_n
{
// …
};
Class ClassName is derived from:
C_1, ..., C_n.
C++
4
Access Control
In the base class
public
protected
private
public
protected
private
public
protected
private
Base class access
specifier
public
public
public
protected
protected
protected
private
private
private
C++
In the derived class
public
protected
no access
protected
protected
no access
private
private
no access
5
The Constructor of a Derived
Class
Derived classes don’t inherit constructors
and destructors.
The constructor of the derived class:
ClassName(list-of-parameters) :
C_1(list1), ..., C_n(list_n)
{
// …
}
C++
6
Example
#include <iostream>
using namespace std;
class Base {
public:
void f1();
void f2();
};
C++
7
The Derived Class
class Derived : public Base {
public:
void f1();
};
Override only the f1 function.
Function f2 will be inherited from Base.
C++
8
The Member Functions of the
Base Class
void Base::f1()
{
cout << "Base: f1\n";
}
void Base::f2()
{
cout << "Base: f2\n";
f1();
}
C++
9
Member Function of the
Derived Class
void Derived::f1()
{
cout << "Derived: f1\n";
}
C++
10
The main function
int main() {
Derived d;
d.f2();
}
Output:
Base: f2
Base: f1
The selection of the f1 function has been done
in compile time.
C++
11
Virtual functions
class Base {
public:
virtual void f1();
void f2();
};
 If function f1 is declared as virtual, then the selection of
the file will be done in running-time.
 We have to place only one virtual keyword in front of
the declaration of the f1 function, in the base class.
 In this case all inherited f1 functions will be considered
virtual.
C++
12
The main Function
int main() {
Derived d;
d.f2();
}
Output:
Base: f2
Derived: f1
C++
13
Virtual Base Classes
In case of multiple inheritance a derived class
can inherit multiple issues of a data member.
Animal
Domestic
Mammal
Dog
C++
14
The Animal Class
#include <iostream>
#include <cstring>
using namespace std;
class Animal {
protected:
char name[20];
public:
Animal(char* n);
};
C++
15
The Mammal Class
class Mammal : public Animal {
protected:
int weight;
public:
Mammal(char* n, int w);
};
C++
16
The Domestic Class
class Domestic : public Animal {
protected:
int comportment;
public:
Domestic(char* n, int c);
};
C++
17
The Dog Class
class Dog : public Mammal, public Domestic {
protected:
bool bark;
public:
Dog(char* n, int w, int c, bool b);
void Display();
};
C++
18
Constructor of the Animal
Class
Animal::Animal(char* n)
{
strcpy(name, n);
}
C++
19
Other Constructors
Mammal::Mammal(char* n, int w): Animal(n)
{
weight = w;
}
Domestic::Domestic(char* n, int c): Animal(n)
{
comportment = c;
}
C++
20
Constructor of the Dog Class
Dog::Dog(char* n, int w, int c, bool b):
Mammal(n, w), Domestic(n, c)
{
bark = b;
}
C++
21
The Display Member Function
void Dog::Display()
{
cout << "name (mammal): " <<
Mammal::name << endl;
cout << "name (domestic): " <<
Domestic::name << endl;
C++
22
The Display Member Function
cout << "weight: " << weight << endl;
cout << "comportment: " << comportment
<< endl;
if ( bark ) cout << "barking\n";
else cout << "no barking";
}
C++
23
The main Function
int main() {
Dog v("Hungarian Vizsla", 12, 9, true);
v.Display();
}
In the Display member function we can’t access
the name data member simply, because this
data member was inherited in two different way.
C++
24
Output
name (mammal): Hungarian Vizsla
name (domestic): Hungarian Vizsla
weight: 12
comportment: 9
barking
We can access the name data member in the
Dog class only by using the scope operator.
C++
25
Virtual Base Class
If we would like to have only one issue of
the name data member we have to use
virtual base classes.
Thus, we have to place the virtual
keyword in the base class list in front of
the class (if we intend to make that base
class virtual).
C++
26
The Mammal Class
class Mammal : public virtual Animal {
protected:
int weight;
public:
Mammal(char* n, int w);
};
C++
27
The Domestic Class
class Domestic : public virtual Animal {
protected:
int comportment;
public:
Domestic(char* n, int c);
};
C++
28
Constructor of the Dog Class
Dog::Dog(char* n, int w, int c, bool b):
Animal(n), Mammal(n, w),
Domestic(n, c)
{
bark = b;
}
Mammal and Domestic doesn’t call Animal
automatically.
C++
29
The Display Member Function
void Dog::Display()
{
cout << "name (mammal): " << name << endl;
cout << "weight: " << weight << endl;
cout << "comportment: " << comportment <<
endl;
if ( bark ) cout << "barking\n";
else cout << "no barking";
}
C++
30
The main Function
int main() {
Dog v("Hungarian Vizsla", 12, 9, true);
v.Display();
}
We can access the name data member without
using the scope operator.
C++
31
Output
name: Hungarian Vizsla
weight: 12
comportment: 9
barking
C++
32
Abstract Classes.
Pure Virtual Functions
A base class can have some known
features, but we are not able to define
them, only in the derived class.
In this case we declare a virtual function,
but we don’t define it in the base class.
If a virtual member function is declared in
the base class, but isn’t defined, we call it
a pure virtual function.
C++
33
Declaration of Pure Virtual
Functions
Pure virtual functions are declared in the regular
way, but the declaration ends with =0. This
means, that we don’t want to define the
function right now.
If a class contains at least one pure virtual
function, then we name it abstract class.
No instance of an abstract class can be defined.
C++
34
Overriding the Pure Virtual
Functions
We have to override all pure virtual
functions in the derived class.
In other case the derived class will be
also abstract.
C++
35
Example
Animal
Horse
Dove
Bear
C++
36
The Animal Class
#include <iostream>
using namespace std;
class Animal {
protected:
double weight;
double age;
double speed;
public:
C++
37
The Animal Class
Animal( double w, double a, double s);
virtual double average_weight() = 0;
virtual double average_age() = 0;
virtual double average_speed() = 0;
int fat() { return weight > average_weight(); }
int fast() { return speed > average_speed(); }
int young() { return 2 * age < average_age(); }
void display();
};
C++
38
Constructor of the Animal
Class
Animal::Animal( double w, double a, double s)
{
weight = w;
age = a;
speed = s;
}
C++
39
The display Member Function
void Animal::display()
{
cout << ( fat() ? "fat, " : "thin, " );
cout << ( young() ? "young, " : "old, " );
cout << ( fast() ? "fast" : "slow" ) << endl;
}
C++
40
The Dove Class
class Dove : public Animal {
public:
Dove( double w, double a, double s):
Animal(w, a, s) {}
double average_weight() { return 0.5; }
double average_age() { return 6; }
double average_speed() { return 90; }
};
C++
41
The Bear Class
class Bear: public Animal {
public:
Bear( double w, double a, double s):
Animal(w, a, s) {}
double average_weight() { return 450; }
double average_age() { return 43; }
double average_speed() { return 40; }
};
C++
42
The Horse Class
class Horse: public Animal {
public:
Horse( double w, double a, double s):
Animal(w, a, s) {}
double average_weight() { return 1000; }
double average_age() { return 36; }
double average_speed() { return 60; }
};
C++
43
The main function
void main() {
Dove d(0.6, 1, 80);
Bear b(500, 40, 46);
Horse h(900, 8, 70);
d.display();
b.display();
h.display();
}
C++
44
Output
fat, young, slow
fat, old, fast
thin, young, fast
C++
45