Title: Derived Classes
1Derived Classes
Derived Classes
2Outline
- Definition
- Virtual functions
- Virtual base classes
- Abstract classes. Pure virtual functions.
3Definition
- 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
4Example (base class list)
- class ClassName public C_1, , public C_n
-
- //
-
- Class ClassName is derived from
- C_1, ..., C_n.
5Access Control
In the base class Base class access specifier In the derived class
public public public
protected public protected
private public no access
public protected protected
protected protected protected
private protected no access
public private private
protected private private
private private no access
6The Constructor of a Derived Class
- Derived classes dont inherit constructors and
destructors. - The constructor of the derived class ClassName(l
ist-of-parameters) - C_1(list1), ..., C_n(list_n)
-
- //
-
7Example
- include ltiostreamgt
- using namespace std
- class Base
- public
- void f1()
- void f2()
8The Derived Class
- class Derived public Base
- public
- void f1()
-
- Override only the f1 function.
- Function f2 will be inherited from Base.
9The Member Functions of the Base Class
- void Basef1()
-
- cout ltlt "Base f1\n"
-
- void Basef2()
-
- cout ltlt "Base f2\n"
- f1()
10Member Function of the Derived Class
- void Derivedf1()
-
- cout ltlt "Derived f1\n"
11The main function
- int main()
- Derived d
- d.f2()
-
- The selection of the f1 function has been done in
compile time.
Output
Base f2 Base f1
12Virtual 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.
13The main Function
- int main()
- Derived d
- d.f2()
Output
Base f2 Derived f1
14Virtual Base Classes
- In case of multiple inheritance a derived class
can inherit multiple issues of a data member.
Animal
Domestic
Mammal
Dog
15The Animal Class
- include ltiostreamgt
- include ltcstringgt
- using namespace std
- class Animal
- protected
- char name20
- public
- Animal(char n)
16The Mammal Class
- class Mammal public Animal
- protected
- int weight
- public
- Mammal(char n, int w)
17The Domestic Class
- class Domestic public Animal
- protected
- int comportment
- public
- Domestic(char n, int c)
18The Dog Class
- class Dog public Mammal, public Domestic
- protected
- bool bark
- public
- Dog(char n, int w, int c, bool b)
- void Display()
19Constructor of the Animal Class
- AnimalAnimal(char n)
-
- strcpy(name, n)
20Other Constructors
- MammalMammal(char n, int w) Animal(n)
-
- weight w
-
- DomesticDomestic(char n, int c) Animal(n)
-
- comportment c
21Constructor of the Dog Class
- DogDog(char n, int w, int c, bool b)
Mammal(n, w), Domestic(n, c) -
- bark b
22The Display Member Function
- void DogDisplay()
-
- cout ltlt "name (mammal) " ltlt Mammalname ltlt
endl - cout ltlt "name (domestic) " ltlt Domesticname ltlt
endl
23The Display Member Function
- cout ltlt "weight " ltlt weight ltlt endl
- cout ltlt "comportment " ltlt comportment ltlt endl
- if ( bark ) cout ltlt "barking\n"
- else cout ltlt "no barking"
24The main Function
- int main()
- Dog v("Hungarian Vizsla", 12, 9, true)
- v.Display()
-
- In the Display member function we cant access
the name data member simply, because this data
member was inherited in two different way.
25Output
- 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.
26Virtual 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).
27The Mammal Class
- class Mammal public virtual Animal
- protected
- int weight
- public
- Mammal(char n, int w)
28The Domestic Class
- class Domestic public virtual Animal
- protected
- int comportment
- public
- Domestic(char n, int c)
29Constructor of the Dog Class
- DogDog(char n, int w, int c, bool b)
Animal(n), Mammal(n, w), Domestic(n, c) -
- bark b
-
- Mammal and Domestic doesnt call Animal
automatically.
30The Display Member Function
- void DogDisplay()
-
- cout ltlt "name (mammal) " ltlt name ltlt endl
- cout ltlt "weight " ltlt weight ltlt endl
- cout ltlt "comportment " ltlt comportment ltlt endl
- if ( bark ) cout ltlt "barking\n"
- else cout ltlt "no barking"
31The 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.
32Output
- name Hungarian Vizsla
- weight 12
- comportment 9
- barking
33Abstract 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 dont define it in the base class. - If a virtual member function is declared in the
base class, but isnt defined, we call it a pure
virtual function.
34Declaration of Pure Virtual Functions
- Pure virtual functions are declared in the
regular way, but the declaration ends with 0.
This means, that we dont 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.
35Overriding 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.
36Example
Animal
Horse
Dove
Bear
37The Animal Class
- include ltiostreamgt
- using namespace std
- class Animal
- protected
- double weight
- double age
- double speed
- public
38The 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 gt average_weight()
- int fast() return speed gt average_speed()
- int young() return 2 age lt average_age()
- void display()
39Constructor of the Animal Class
- AnimalAnimal( double w, double a, double s)
-
- weight w
- age a
- speed s
40The display Member Function
- void Animaldisplay()
-
- cout ltlt ( fat() ? "fat, " "thin, " )
- cout ltlt ( young() ? "young, " "old, " )
- cout ltlt ( fast() ? "fast" "slow" ) ltlt endl
41The 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
42The 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
43The 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
44The 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()
45Output
- fat, young, slow
- fat, old, fast
- thin, young, fast