Download presentation
Presentation is loading. Please wait.
Published byPatrick Hunt Modified over 8 years ago
1
Programming in C++ Michal Brabec Petr Malý
2
Inheritance class derived-class: access-specifier base-class {} Base & Derived class Implementation inheritance - Base class object provides its public or protected members to Derived class object Interface inheritance - Derived class objects may be handled via Base class pointers or references Multiple Inheritance Derived class can be derived from several base classes
3
Inheritance class Car { public: string manufacturer; }; class Employee { public: string name; int phone; void Hello() { cout << "I'm " << name << endl; } }; class Manager : public Employee { public: vector employees; }; class Driver : public Employee { public: Car car; }; int main() { Driver driver; Manager manager; driver.car = Car{ "Skoda" }; driver.name = "Paul"; driver.phone = 122334455; manager.name = "Phil"; manager.phone = 122334466; manager.employees.push_back(&driver); driver.Hello(); // I’m Paul manager.Hello(); // I’m Phil } Employee ManagerDriver
4
Inheritance Derived objects can be implicitly converted to base objects Pointers or references to derived objects can also be implicitly converted to base class pointers or references The opposite conversion is explicit dynamic_cast static_cast reinterpret_cast
5
Task Create a classes for complex number, integer and vector which derives from class number Create a global function which removes duplicities from array of numbers and returns a new array (deep copy)
6
Slicing class Car { public: string manufacturer; }; class Employee { public: string name; int phone; void Hello() { cout << "I'm " << name << endl; } }; class Manager : public Employee { public: vector employees; }; class Driver : public Employee { public: Car car; }; int main() { Driver driver; Manager manager; driver.car = Car{ "Skoda" }; driver.name = "Paul"; driver.phone = 122334455; manager.name = "Phil"; manager.phone = 122334466; manager.employees.push_back(&driver); Employee newEmployee = DuplicateEmployee(manager, "Patrick"); Manager* newManager = static_cast (&newEmployee); std::cout employees.size() << endl; std::cout << manager.employees.size() << endl; } Employee ManagerDriver Employee DuplicateEmployee(const Employee& oldEmployoee, string newName) { Employee newEmployee(oldEmployoee); newEmployee.name = newName; return newEmployee; }
7
Polymorphism Interface inheritance Base class defines interface by using virtual functions The behavior of derived objects accessed via pointers or references to base class is different The pointers and references must be used to avoid slicing
8
Virtual Function Which function will be called is handeled in run-time Run-time Type Information (RTTI) is added to each object which has at least one virtual function (Polymorphic type) Typically implemented by pointer to Virtual Function Table one table for each class table contains addresses of the functions, which are accessed by virtual function index
9
Virtual Function Pure Virtual Function has no implementation and forces derived class to implement it Abstract class has at least one pure virtual function Destructor needs to be virtual if the object is destroyed via pointer to base class class Base { public: virtual void VirtFnc(int arg) { } virtual void PureVirtFnc(int arg) = 0; };
10
Task Create a classes for complex number, integer and vector which derives from class number Create member function AbsoluteValue for each of those classes Create a function which computes absolute values for array of numbers and return it in another array
11
Task Use code from previous task Create a global function NumbersProduct which generates product of two numbers function has two arguments: references to numbers function return pointer to number numbers might be of different type use virtual functions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.