Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(

Slides:



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

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Inheritance, Polymorphism, and Virtual Functions
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an 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.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Cpt S 122 – Data Structures 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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
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.
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.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object-Oriented Programming in C++ More examples of Association.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Learners Support Publications Constructors and Destructors.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
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.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
Constructors and Destructors
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
C++ Polymorphism Reference and pointer implicit type casting
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar), m_uiRating(iR) {} Here the derived class takes 2 variables one is used to initialise the derived class attribute the other is passed on to the base class constructor. 1

Constructors: Access Considerations DerivedClass x = new DerivedClass( 10, aPointer, true); Destroying an object occurs in the opposite order used to construct an object. The body of the derived-class destructor is executed first, and then the base-class destructor is called. When creating an object of a derived class, the program first calls the base-class constructor and then calls the derived-class constructor. The base-class constructor is responsible for initialising the inherited data members. 2

Constructors: Access Considerations Two important relationships are a base-class pointer can point to a derived-class object without an explicit type cast a base-class reference can refer to a derived-class object without an explicit type cast: Ordinarily, C++ requires that references and pointer types match the assigned types, but this rule is relaxed for inheritance. However, the rule relaxation is just in one direction. You can’t assign base-class objects and addresses to derived-class references and pointers: 3

Constructors: Access Considerations We create an array of BaseClass pointers and assign them to null. Arrays must be of the same type. We create a mixture of DerivedClass pointers and instantiate objects of the DerivedClass. We now have a group of different DerivedClass objects and their pointers. Next we assign the BaseClass pointers in the array to the derived class pointers. We end up with an array of pointers pointing to various DerivedClass objects. 4

Constructors: Access Considerations Inheritance is a ‘is a’ relationship Containment is where one class has another class as a member of that class is ‘has a’ relationship. Beware only use inheritance when there is a ‘is a’ relationship. Lawyers are often referred to as sharks. However you should not derive Lawyer from shark, it is not a true ‘is a’ relationship or is it? 5

Polymorphic Public Inheritance Objects of the derived class use the base-class functions. However there are times where you want a function to behave differently for the derived class than it does for the base class. You want a particular function to behave depending on the object that invokes it. Polymorphic behaviour You can have multiple behaviours for a function, depending on its context. 6

Polymorphic Public Inheritance There are two key mechanisms for implementing polymorphic public inheritance: Redefining base-class functions in a derived class Using virtual functions Virtual functions Inheritance can involve a derived class having the same function as the base class. Declaring the base class function as virtual allows your program to choose at runtime which function to run. 7

Polymorphic Public Inheritance A Base Class has a member function (its name is not important) the class designer knows the Base Class won’t use the member function for itself. However Derived Class’s will have a member function of the same name (signature/ID) that will do something (remember functions are doing things). If you declare the Base function as virtual when that member function name is used it will be used in the context of the DerivedClass not the BaseClass; this is polymorphic behaviour. 8

Polymorphic Public Inheritance We declare a virtual member function by preceding the function's prototype with the keyword virtual in the base class. virtual void FunctionName(); Polymorphism for( int i = 0; i < arraySize; i++) { pBaseClassArray[i]->FunctionName(); cout << endl; } The FunctionName in the derived class is called 9

Polymorphic Public Inheritance An overridden member function in a derived class has the same signature and return type (i.e., prototype) as the member function it overrides in its base class. If we declare the base class member function as virtual, we can override that function by the derived class to enable polymorphic behaviour. Declaring the base member function as virtual makes the program choose the member function version based on object type. 10

Polymorphic Public Inheritance The fact we may have a Base Class pointer pointing to the Derived Class is not relevant; the Derived Class member function will be invoked, that is at execution time based on the object type; not the pointer type. Note: The pointer has a type, the object has a type what we are saying is they can be different for the same instance of an object when using inheritance. This is because every instance of a Derived Class has a Base Class as a part of it. 11

Polymorphic Public Inheritance Even though certain functions are implicitly virtual because of a declaration made higher in the class hierarchy, explicitly declare these functions virtual at every level of the hierarchy to promote program clarity. Note: that the keyword virtual is used just in the function prototypes in the class declaration (h file), not in the function definitions (cpp file). virtual functions are another example of dynamic binding. 12

Polymorphic Public Inheritance When using virtual functions you also need to create a virtual destructor. If the destructors are virtual, the destructor corresponding to the object type is called. 13