Programming in C++ Michal Brabec Petr Malý. Inheritance class derived-class: access-specifier base-class {} Base & Derived class Implementation inheritance.

Slides:



Advertisements
Similar presentations
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
Polymorphism, Virtual Methods and Abstract Classes.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 21 - Standard C++ Language Additions Outline 21.1Introduction 21.2 bool Data Type 21.3 static_cast.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
Inheritance, Polymorphism, and Virtual Functions
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
OOP, Virtual Functions and Inheritance
ILM Proprietary and Confidential -
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Inheritance and Composition Reusing the code and functionality Unit - 04.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
 Inheritance  Protected Members  Non-public Inheritance  Virtual Function Implementation  Virtual Destructors  Abstract Base Classes and Interfaces.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Polymorphism, Virtual Methods and Abstract Classes
Advanced Program Design with C++
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Lecture 5 Inheritance, Polymorphism, Object Memory Model, The Visitor Pattern …and gazillion other tidbits!
Polymorphism & Pointers
Chapter 19 - Inheritance Outline 19.1 Introduction
Final Exam Review Inheritance Template Functions and Classes
C++ Polymorphism Reference and pointer implicit type casting
Presentation transcript:

Programming in C++ Michal Brabec Petr Malý

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

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 = ; manager.name = "Phil"; manager.phone = ; manager.employees.push_back(&driver); driver.Hello(); // I’m Paul manager.Hello(); // I’m Phil } Employee ManagerDriver

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

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)

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 = ; manager.name = "Phil"; manager.phone = ; 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; }

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

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

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; };

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

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