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 the derived class: ClassName(list-of-parameters) : C_1(list1).The Constructor of a Derived Class Derived classes don’t inherit constructors and destructors. C_n(list_n) { // … } C++ 6 .. ...

Example #include <iostream> using namespace std. C++ 7 . void f2(). class Base { public: void f1(). }.

}.The Derived Class class Derived : public Base { public: void f1(). C++ 8 . Function f2 will be inherited from Base. Override only the f1 function.

} void Base::f2() { cout << "Base: f2\n". } C++ 9 . f1().The Member Functions of the Base Class void Base::f1() { cout << "Base: f1\n".

} C++ 10 .Member Function of the Derived Class void Derived::f1() { cout << "Derived: f1\n".

f2().The main function int main() { Derived d. d. } Output: Base: f2 Base: f1 The selection of the f1 function has been done in compile time. C++ 11 .

C++ 12 .  If function f1 is declared as virtual. }. void f2(). then the selection of the file will be done in running-time.Virtual functions class Base { public: virtual void f1().  We have to place only one virtual keyword in front of the declaration of the f1 function.  In this case all inherited f1 functions will be considered virtual. in the base class.

Output: Base: f2 Derived: f1 } C++ 13 .The main Function int main() { Derived d.f2(). d.

Virtual Base Classes In case of multiple inheritance a derived class can inherit multiple issues of a data member. Animal Domestic Dog C++ 14 Mammal .

public: Animal(char* n). class Animal { protected: char name[20]. }.The Animal Class #include <iostream> #include <cstring> using namespace std. C++ 15 .

public: Mammal(char* n.The Mammal Class class Mammal : public Animal { protected: int weight. }. int w). C++ 16 .

}. int c). C++ 17 .The Domestic Class class Domestic : public Animal { protected: int comportment. public: Domestic(char* n.

C++ 18 .The Dog Class class Dog : public Mammal. }. bool b). int c. int w. void Display(). public Domestic { protected: bool bark. public: Dog(char* n.

n). } C++ 19 .Constructor of the Animal Class Animal::Animal(char* n) { strcpy(name.

Other Constructors Mammal::Mammal(char* n. } Domestic::Domestic(char* n. } C++ 20 . int c): Animal(n) { comportment = c. int w): Animal(n) { weight = w.

w). Domestic(n. bool b): Mammal(n. c) { bark = b.Constructor of the Dog Class Dog::Dog(char* n. } C++ 21 . int c. int w.

cout << "name (domestic): " << Domestic::name << endl. C++ 22 .The Display Member Function void Dog::Display() { cout << "name (mammal): " << Mammal::name << endl.

else cout << "no barking". cout << "comportment: " << comportment << endl. if ( bark ) cout << "barking\n". } C++ 23 .The Display Member Function cout << "weight: " << weight << endl.

true). 9. } In the Display member function we can’t access the name data member simply. v.The main Function int main() { Dog v("Hungarian Vizsla".Display(). because this data member was inherited in two different way. 12. C++ 24 .

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.

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).Virtual Base Class If we would like to have only one issue of the name data member we have to use virtual base classes. C++ 26 .

The Mammal Class class Mammal : public virtual Animal { protected: int weight. public: Mammal(char* n. C++ 27 . }. int w).

C++ 28 . public: Domestic(char* n.The Domestic Class class Domestic : public virtual Animal { protected: int comportment. int c). }.

Mammal(n. int w. C++ 29 . Domestic(n.Constructor of the Dog Class Dog::Dog(char* n. bool b): Animal(n). c) { bark = b. w). int c. } Mammal and Domestic doesn’t call Animal automatically.

} C++ 30 . else cout << "no barking".The Display Member Function void Dog::Display() { cout << "name (mammal): " << name << endl. cout << "comportment: " << comportment << endl. if ( bark ) cout << "barking\n". cout << "weight: " << weight << endl.

Display(). } We can access the name data member without using the scope operator. 12. v. C++ 31 . true). 9.The main Function int main() { Dog v("Hungarian Vizsla".

Output name: Hungarian Vizsla weight: 12 comportment: 9 barking C++ 32 .

we call it a pure virtual function. but we don’t define it in the base class. C++ 33 .Abstract Classes. Pure Virtual Functions A base class can have some known features. If a virtual member function is declared in the base class. but isn’t defined. In this case we declare a virtual function. only in the derived class. but we are not able to define them.

then we name it abstract class. but the declaration ends with =0. This means. C++ 34 . No instance of an abstract class can be defined.Declaration of Pure Virtual Functions Pure virtual functions are declared in the regular way. If a class contains at least one pure virtual function. that we don’t want to define the function right now.

In other case the derived class will be also abstract.Overriding the Pure Virtual Functions We have to override all pure virtual functions in the derived class. C++ 35 .

Example Animal Dove Bear Horse C++ 36 .

public: C++ 37 .The Animal Class #include <iostream> using namespace std. class Animal { protected: double weight. double speed. double age.

} int fast() { return speed > average_speed().The Animal Class Animal( double w. virtual double average_age() = 0. double a. double s). virtual double average_speed() = 0. int fat() { return weight > average_weight(). } void display(). }. } int young() { return 2 * age < average_age(). virtual double average_weight() = 0. C++ 38 .

age = a. } C++ 39 .Constructor of the Animal Class Animal::Animal( double w. double s) { weight = w. speed = s. double a.

" : "old. " : "thin. " ).The display Member Function void Animal::display() { cout << ( fat() ? "fat. } C++ 40 . " ). cout << ( fast() ? "fast" : "slow" ) << endl. cout << ( young() ? "young.

a. } }. } double average_age() { return 6.The Dove Class class Dove : public Animal { public: Dove( double w. double s): Animal(w. } double average_speed() { return 90. s) {} double average_weight() { return 0.5. double a. C++ 41 .

s) {} double average_weight() { return 450. } }. } double average_age() { return 43.The Bear Class class Bear: public Animal { public: Bear( double w. } double average_speed() { return 40. a. double s): Animal(w. C++ 42 . double a.

s) {} double average_weight() { return 1000.The Horse Class class Horse: public Animal { public: Horse( double w. double s): Animal(w. a. C++ 43 . } double average_age() { return 36. double a. } double average_speed() { return 60. } }.

d. b.display(). 70). 1.6. } C++ 44 . 80). Bear b(500. 40. h. Horse h(900.The main function void main() { Dove d(0. 8. 46).display().display().

young. young.Output fat. fast thin. old. fast C++ 45 . slow fat.

Sign up to vote on this title
UsefulNot useful