C++ facts From www.geeksforgeeks.org. What all is inherited from parent class in C++? Following are the things which a derived class inherits from its.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

How C++ Compilers Implement Object Orientation Eric Powders (ejp2127)
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual.
Inheritance and Polymorphism CS351 – Programming Paradigms.
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.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
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. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
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.
Object-Oriented Programming in C++ More examples of Association.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
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.
Chapter -6 Polymorphism
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming Elhanan Borenstein Lecture #7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
A First Book of C++ Chapter 12 Extending Your Classes.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 3370 – C++ Object-oriented Programming
7. Inheritance and Polymorphism
Inheritance and Big O And your first reading assignment
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
“Under the Hood” of Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
Java Programming Language
Presentation transcript:

C++ facts From

What all is inherited from parent class in C++? Following are the things which a derived class inherits from its parent: – 1) Every data member defined in the parent class (although such members may not always be accessible in the derived class!) – 2) Every ordinary member function of the parent class (although such members may not always be accessible in the derived class!) – 3) The same initial data layout as the base class.

Following are the things which a derived class does NOT inherit from its parent : – 1) The base class’s constructors and destructor. – 2) The base class’s friends

Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction) Consider the following simple program which is an example of runtime polymorphism.(given in next slide):

#include using namespace std; class Base { public: virtual void show() { cout<<" In Base \n"; } };

class Derived: public Base { public: void show() { cout<<"In Derived \n"; } };

int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; }

Output:

In Derived

The main thing to note about the above program is, derived class function is called using a base class pointer. The idea is, virtual functions are called according to the type of object pointed or referred, not according to the type of pointer or reference.virtual functions In other words, virtual functions are resolved late, at runtime.

What is the use? Virtual functions allow us to create a list of base class pointers and call methods of any of the derived classes without even knowing kind of derived class object.

What is the use? For example, consider a employee management software for an organization, let the code has a simple base classEmployee, the class contains virtual functions like raiseSalary(), transfer(), promote(),.. etc. Different types of employees like Manager, Engineer,..etc may have their own implementations of the virtual functions present in base class Employee..

What is the use? In our complete software, we just need to pass a list of employees everywhere and call appropriate functions without even knowing the type of employee. For example, we can easily raise salary of all employees by iterating through list of employees.

What is the use? Every type of employee may have its own logic in its class, we don’t need to worry because if raiseSalary() is present for a specific employee type, only that function would be called.

class Employee { public: virtual void raiseSalary() { /* common raise salary code */ } virtual void promote() { /* common promote code */ } };

class Manager: public Employee { virtual void raiseSalary() { /* Manager specific raise salary code, may contain increment of manager specific incentives*/ } virtual void promote() { /* Manager specific promote */ } };

// Similarly, there may be other types of employees // We need a very simple function to increment salary of all //employees // Note that emp[] is an array of pointers and actual pointed //objects can // be any type of employees. This function should ideally be in a //class // like Organization, we have made it global to keep things //simple

void globalRaiseSalary(Employee *emp[], int n) { for (int i = 0; i < n; i++) emp[i]->raiseSalary(); // Polymorphic Call: Calls raiseSalary() // according to the actual object, not // according to the type of pointer }

like globalRaiseSalary(), there can be many other operations that can be appropriately done on a list of employees without even knowing the type of actual object. Virtual functions are so useful that later languages like Java keep all methods as virtual by default.Java keep all methods as virtual by default

How does compiler do this magic of late resolution? Compiler maintains two things to this magic: – vtable: A table of function pointers. It is maintained per class. vtable: – vptr: A pointer to vtable. It is maintained per object. vptr:

Compiler adds additional code at two places to maintain and use vptr. – 1) Code in every constructor. This code sets vptr of the object being created. This code sets vptr to point to vtable of the class. – 2) Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference (In the above example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.

Is this a standard way for implementation of run-time polymorphism in C++? The C++ standards do not mandate exactly how runtime polymorphism must be implemented, but compilers generally use minor variations on the same basic model.