Download presentation
Presentation is loading. Please wait.
Published byShawna Darcey Modified over 9 years ago
1
Derived Classes
2
C++ 2 Outline Definition Virtual functions Virtual base classes Abstract classes. Pure virtual functions.
3
C++ 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
C++ 4 Example (base class list) class ClassName : public C_1, …, public C_n { // … }; Class ClassName is derived from: C_1,..., C_n.
5
C++ 5 Access Control In the base class Base class access specifier In the derived class public protectedpublicprotected privatepublicno access publicprotected privateprotectedno access publicprivate protectedprivate no access
6
C++ 6 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) { // … }
7
C++ 7 Example #include using namespace std; class Base { public: void f1(); void f2(); };
8
C++ 8 The Derived Class class Derived : public Base { public: void f1(); }; Override only the f1 function. Function f2 will be inherited from Base.
9
C++ 9 The Member Functions of the Base Class void Base::f1() { cout << "Base: f1\n"; } void Base::f2() { cout << "Base: f2\n"; f1(); }
10
C++ 10 Member Function of the Derived Class void Derived::f1() { cout << "Derived: f1\n"; }
11
C++ 11 The main function int main() { Derived d; d.f2(); } The selection of the f1 function has been done in compile time. Base: f2 Base: f1 Output:
12
C++ 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
C++ 13 The main Function int main() { Derived d; d.f2(); } Base: f2 Derived: f1 Output:
14
C++ 14 Virtual Base Classes In case of multiple inheritance a derived class can inherit multiple issues of a data member. Animal DomesticMammal Dog
15
C++ 15 The Animal Class #include using namespace std; class Animal { protected: char name[20]; public: Animal(char* n); };
16
C++ 16 The Mammal Class class Mammal : public Animal { protected: int weight; public: Mammal(char* n, int w); };
17
C++ 17 The Domestic Class class Domestic : public Animal { protected: int comportment; public: Domestic(char* n, int c); };
18
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
C++ 19 Constructor of the Animal Class Animal::Animal(char* n) { strcpy(name, n); }
20
C++ 20 Other Constructors Mammal::Mammal(char* n, int w): Animal(n) { weight = w; } Domestic::Domestic(char* n, int c): Animal(n) { comportment = c; }
21
C++ 21 Constructor of the Dog Class Dog::Dog(char* n, int w, int c, bool b): Mammal(n, w), Domestic(n, c) { bark = b; }
22
C++ 22 The Display Member Function void Dog::Display() { cout << "name (mammal): " << Mammal::name << endl; cout << "name (domestic): " << Domestic::name << endl;
23
C++ 23 The Display Member Function cout << "weight: " << weight << endl; cout << "comportment: " << comportment << endl; if ( bark ) cout << "barking\n"; else cout << "no barking"; }
24
C++ 24 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.
25
C++ 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
C++ 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
C++ 27 The Mammal Class class Mammal : public virtual Animal { protected: int weight; public: Mammal(char* n, int w); };
28
C++ 28 The Domestic Class class Domestic : public virtual Animal { protected: int comportment; public: Domestic(char* n, int c); };
29
C++ 29 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.
30
C++ 30 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"; }
31
C++ 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
C++ 32 Output name: Hungarian Vizsla weight: 12 comportment: 9 barking
33
C++ 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 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.
34
C++ 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 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.
35
C++ 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
C++ 36 Example Animal Dove Bear Horse
37
C++ 37 The Animal Class #include using namespace std; class Animal { protected: double weight; double age; double speed; public:
38
C++ 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 > average_weight(); } int fast() { return speed > average_speed(); } int young() { return 2 * age < average_age(); } void display(); };
39
C++ 39 Constructor of the Animal Class Animal::Animal( double w, double a, double s) { weight = w; age = a; speed = s; }
40
C++ 40 The display Member Function void Animal::display() { cout << ( fat() ? "fat, " : "thin, " ); cout << ( young() ? "young, " : "old, " ); cout << ( fast() ? "fast" : "slow" ) << endl; }
41
C++ 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
C++ 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
C++ 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
C++ 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
C++ 45 Output fat, young, slow fat, old, fast thin, young, fast
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.