Derived Classes - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Derived Classes

Description:

Derived Classes Outline Definition Virtual functions Virtual base classes Abstract classes. Pure virtual functions. Definition class Derived : list-of-base-classes ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 46
Provided by: bscwpubit
Category:
Tags: base | classes | data | derived

less

Transcript and Presenter's Notes

Title: Derived Classes


1
Derived Classes
Derived Classes
2
Outline
  • Definition
  • Virtual functions
  • Virtual base classes
  • Abstract classes. Pure virtual functions.

3
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

4
Example (base class list)
  • class ClassName public C_1, , public C_n
  • //
  • Class ClassName is derived from
  • C_1, ..., C_n.

5
Access 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
6
The 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)
  • //

7
Example
  • include ltiostreamgt
  • using namespace std
  • class Base
  • public
  • void f1()
  • void f2()

8
The Derived Class
  • class Derived public Base
  • public
  • void f1()
  • Override only the f1 function.
  • Function f2 will be inherited from Base.

9
The Member Functions of the Base Class
  • void Basef1()
  • cout ltlt "Base f1\n"
  • void Basef2()
  • cout ltlt "Base f2\n"
  • f1()

10
Member Function of the Derived Class
  • void Derivedf1()
  • cout ltlt "Derived f1\n"

11
The 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
12
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.

13
The main Function
  • int main()
  • Derived d
  • d.f2()

Output
Base f2 Derived f1
14
Virtual Base Classes
  • In case of multiple inheritance a derived class
    can inherit multiple issues of a data member.

Animal
Domestic
Mammal
Dog
15
The Animal Class
  • include ltiostreamgt
  • include ltcstringgt
  • using namespace std
  • class Animal
  • protected
  • char name20
  • public
  • Animal(char n)

16
The Mammal Class
  • class Mammal public Animal
  • protected
  • int weight
  • public
  • Mammal(char n, int w)

17
The Domestic Class
  • class Domestic public Animal
  • protected
  • int comportment
  • public
  • Domestic(char n, int c)

18
The Dog Class
  • class Dog public Mammal, public Domestic
  • protected
  • bool bark
  • public
  • Dog(char n, int w, int c, bool b)
  • void Display()

19
Constructor of the Animal Class
  • AnimalAnimal(char n)
  • strcpy(name, n)

20
Other Constructors
  • MammalMammal(char n, int w) Animal(n)
  • weight w
  • DomesticDomestic(char n, int c) Animal(n)
  • comportment c

21
Constructor of the Dog Class
  • DogDog(char n, int w, int c, bool b)
    Mammal(n, w), Domestic(n, c)
  • bark b

22
The Display Member Function
  • void DogDisplay()
  • cout ltlt "name (mammal) " ltlt Mammalname ltlt
    endl
  • cout ltlt "name (domestic) " ltlt Domesticname ltlt
    endl

23
The 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"

24
The 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.

25
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.

26
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).

27
The Mammal Class
  • class Mammal public virtual Animal
  • protected
  • int weight
  • public
  • Mammal(char n, int w)

28
The Domestic Class
  • class Domestic public virtual Animal
  • protected
  • int comportment
  • public
  • Domestic(char n, int c)

29
Constructor 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.

30
The 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"

31
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.

32
Output
  • name Hungarian Vizsla
  • weight 12
  • comportment 9
  • barking

33
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 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.

34
Declaration 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.

35
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.

36
Example
Animal
Horse
Dove
Bear
37
The Animal Class
  • include ltiostreamgt
  • using namespace std
  • class Animal
  • protected
  • double weight
  • double age
  • double speed
  • public

38
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 gt average_weight()
  • int fast() return speed gt average_speed()
  • int young() return 2 age lt average_age()
  • void display()

39
Constructor of the Animal Class
  • AnimalAnimal( double w, double a, double s)
  • weight w
  • age a
  • speed s

40
The display Member Function
  • void Animaldisplay()
  • cout ltlt ( fat() ? "fat, " "thin, " )
  • cout ltlt ( young() ? "young, " "old, " )
  • cout ltlt ( fast() ? "fast" "slow" ) ltlt endl

41
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

42
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

43
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

44
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()

45
Output
  • fat, young, slow
  • fat, old, fast
  • thin, young, fast
Write a Comment
User Comments (0)
About PowerShow.com