This document discusses different types of polymorphism in C++, including runtime polymorphism using virtual methods and compile-time polymorphism using templates. It provides examples of how each can be used and notes that while they have some differences, templates and inheritance can also be combined effectively in some cases. The costs associated with each approach are outlined, such as a potential performance overhead for virtual methods and potential code bloat issues with templates.
ملفات مساق البرمجة الهدفية (الشيئية) التي يتم تدريسها لطلبة بكالوريوس تكنولوجيا المعلومات وبكالوريوس تطوير نظم الحاسوب في الكلية الجامعية للعلوم والتكنولوجيا.
الملف يضم مفهوم الوراثة Inheritance
إعدادي وتدريسي
ملفات مساق البرمجة الهدفية (الشيئية) التي يتم تدريسها لطلبة بكالوريوس تكنولوجيا المعلومات وبكالوريوس تطوير نظم الحاسوب في الكلية الجامعية للعلوم والتكنولوجيا.
الملف يضم مفهوم الوراثة Polymorphism
إعدادي وتدريسي
Learn the various forms of polymorphism in Java with illustrative examples to explain method overloading(Compile-time polymorphism) and method overriding(Run-time polymorphism)
Object Oriented Programming_Chapter 3 (Two Lectures)
1- Let’s think on Inheritance
2- Let’s focus on Superclass’s Constructor
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
Object Oriented Programming_Chapter 4 (Two Lectures)
1- Let’s think on polymorphism
2- Let’s focus on Full Example
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
This presentation is about a lecture I gave within the "Software Design" course of the Computer Science bachelor program, of the Vrije Universiteit Amsterdam.
http://www.ivanomalavolta.com
This slide about Object Orientated Programing contains Fundamental of OOP, Encapsulation, Inheritance Abstract Class, Association, Polymorphism, Interface, Exceptional Handling and many more OOP language basic thing.
This presentation is about a lecture I gave within the "Software Modeling" course of the Computer Science bachelor program, of the Vrije Universiteit Amsterdam.
http://www.ivanomalavolta.com
ملفات مساق البرمجة الهدفية (الشيئية) التي يتم تدريسها لطلبة بكالوريوس تكنولوجيا المعلومات وبكالوريوس تطوير نظم الحاسوب في الكلية الجامعية للعلوم والتكنولوجيا.
الملف يضم مفهوم الوراثة Inheritance
إعدادي وتدريسي
ملفات مساق البرمجة الهدفية (الشيئية) التي يتم تدريسها لطلبة بكالوريوس تكنولوجيا المعلومات وبكالوريوس تطوير نظم الحاسوب في الكلية الجامعية للعلوم والتكنولوجيا.
الملف يضم مفهوم الوراثة Polymorphism
إعدادي وتدريسي
Learn the various forms of polymorphism in Java with illustrative examples to explain method overloading(Compile-time polymorphism) and method overriding(Run-time polymorphism)
Object Oriented Programming_Chapter 3 (Two Lectures)
1- Let’s think on Inheritance
2- Let’s focus on Superclass’s Constructor
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
Object Oriented Programming_Chapter 4 (Two Lectures)
1- Let’s think on polymorphism
2- Let’s focus on Full Example
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
This presentation is about a lecture I gave within the "Software Design" course of the Computer Science bachelor program, of the Vrije Universiteit Amsterdam.
http://www.ivanomalavolta.com
This slide about Object Orientated Programing contains Fundamental of OOP, Encapsulation, Inheritance Abstract Class, Association, Polymorphism, Interface, Exceptional Handling and many more OOP language basic thing.
This presentation is about a lecture I gave within the "Software Modeling" course of the Computer Science bachelor program, of the Vrije Universiteit Amsterdam.
http://www.ivanomalavolta.com
Some of the most common and easy-to-calculate/easy-to-measure code metrics. Understanding these can help you make your code better: avoiding code rot and writing maintainable code all starts here. Content is created for C# .net, however, the underlying principles apply to other languages/frameworks as well.
A presentation on Design Patterns. This was created for the workshop at NED University of Engineering and Technology Karachi, Pakistan CIS Department held on April 7, 2012.
Had a wonderful experience
Presentation I gave at the Houston TechFest Sept 2009. This presentation goes over Extension Methods, Lambdas, Expression Trees, and Fluent Interfaces. I also show examples from popular frameworks using these techniques.
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
Acetabularia Information For Class 9 .docxvaibhavrinwa19
Acetabularia acetabulum is a single-celled green alga that in its vegetative state is morphologically differentiated into a basal rhizoid and an axially elongated stalk, which bears whorls of branching hairs. The single diploid nucleus resides in the rhizoid.
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Palestine last event orientationfvgnh .pptxRaedMohamed3
An EFL lesson about the current events in Palestine. It is intended to be for intermediate students who wish to increase their listening skills through a short lesson in power point.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
2. Polymorphism means “Many forms”
OO Purists – “Only virtual methods”
Liberal C++ guys – Either virtual methods (Behavior), or templates (Types), or
even function overloading for that matter (Behavior)
Generally accepted – virtual method based, and template based
OVERVIEW
3. Runtime/Dynamic polymorphism (Based on virtual methods)
Compile time/Static polymorphism (Based on templates)
These 2 are largely orthogonal techniques, but many ways where one could be
replaced with the other…and that’s where the confusion
KINDS OF POLYMORPHISM
4. ANY OF THEM WORKS – EXAMPLE
// Using virtual method based polymorphism
// lock is a virtual method. SingleThreading and Multi-
threading are 2 derived classes which define lock()
differently
void func(BaseThreadingPolicy * pThreadingPolicy)
{
pThreadingPolicy->lock();
};
// Using template based polymorphism
SingleThreading and Multi-threading both implement a lock()
method, but the needn’t have any virtual methods, nor do
they need to derive from a common base class
template <class T> func(T* pThreadingPolicy)
{
pThreadingPolicy->lock();
};
5. In the previous slide we have shown that templates aren’t restricted to
polymorphism of type alone, but they also can be used for polymorphism of
behavior
In fact the template version in the previous example yields much faster code than
the virtual method equivalent
Much prevailing confusion about which technique to use in what situation
ANY OF THEM WORKS - CONTD
6. Runtime binding between abstract type and concrete type
Enforces a common interface for all derived types via the base class
Enforces an ‘Is-A’ relationship
Used extensively in OO frameworks
Templates eating into parts of its territory
RUNTIME POLYMORPHISM
7. Compile time binding between abstract type and concrete
type
Concrete types need not belong to the same inheritance
hierarchy. They just need to meet the ‘Constraints’
imposed by the generic type. So, more generic than the
virtuals
Foundation of ‘Generic programming’ or programming
based on ‘Concepts’ (Eg: STL)
A very ‘Happening area’ in C++ right now (See TR1,
Boost, C++0x...)
COMPILE TIME POLYMORPHISM
8. Templates –
Buy us generality in software, without the ‘Abstraction penalty’
Are type safe
Are non-intrusive
Allow both reference and value semantics unlike virtuals
WHY ALL THE
EXCITEMENT
ABOUT
TEMPLATES?
9. DESIGN OF REUSABLE DATA
STRUCTURES – INHERITANCE OR
TEMPLATES?
IndexIndex Inheritance basedInheritance based
approachapproach
Template based approachTemplate based approach
1 Slower (virtuals) Faster
2 Non type-safe
(Crash!)
Type safe
3 Intrusive (put an
int/char inside a class
and derive from base)
Non-Intrusive
4 Reference semantics
only (leaks, crashes..)
Value or Reference
semantics, both allowed
10. So, templates lead to ‘Faster, Safer, and Easier to reuse’ data structures than
inheritance
Using inheritance for designing generic data structures, is a common mistake in
C++
Dates back to days where compiler support was bad for templates
SURE CHOICE - TEMPLATES
11. Code and structure reuse from base classes
OO Frameworks
Framework implementation requires ‘Heterogeneous containers’
Reactor example
Often depend on polymorphic return types
Factory example
Can’t do that with templates
WHY INHERITANCE THEN?
12. Don’t overuse inheritance (Implementing data structures using inheritance is an
example of misuse of inheritance)
Evaluate if your design could be done using templates, they are faster and safer
than virtuals.
Read up on templates and be aware of its pitfalls (Another class of mine will deal
with that)
CHOOSING YOUR
POLYMORPHISM
13. Templates and Inheritance can be combined sometimes
Eg 1: Singleton pattern implementation
Eg 2: Reusable object counter
Eg 3: Reducing template code bloat
Weakness of inheritance – Base classes don’t know the
type of their derived classes
Static attributes in base class are ‘Shared’
Weakness of templates – They don’t lead to an ‘Is-A’
relationship
VANILLA OR
STRAWBERRY? I
THINK I’LL
HAVE BOTH
14. Best of both worlds
Have base class know the derived class type by using templates [So, getInstance in the
base class is able to create a derived class instance]
Enforce ‘Is-A’ relationship using inheritance [Eg: Make ctor private in the base class, so
nobody can instantiate a derived class instance as well]
SINGLETON
USING THE VAN-
BERRY FLAVOR
15. Synergy again
Base classes need to share the static attributes
Use templates to get over that issue – One base class generated for each derived class
Use inheritance for the ‘Is-A’ relationship [Whenever the class ctor is called, the base class
ctor is called, likewise for the dtor, increment and decrement operations happen
automatically]
OBJECT COUNTER
16. Each class that has a virtual method has an associated
‘vtable’
A ‘vtable’ is just an array of function pointers containing
pointers to virtual method implementations for that class
Each object of any such class, has a pointer to the
associated vtable
The compiler creates the required vtables and initializes
each object to point to the associated vtable all ‘Under the
hood’
COST OF RUNTIME
POLYMORPHISM
18. ILLUSTRATION (CONTD)
Pointer to Base::func1
Pointer to Base::func2
Pointer to Base::~Base
vtable for the Derived class
Pointer to Derived::func1
Pointer to Base::func2
Pointer to Derived::~Derived
vtable for the Base class
20. The compiler would have converted all your virtual method calls
Your call: pDerived->func2();
It’s become: (*pDerived->vptr[1])(pDerived)
As we see above, the vptr stored by the compiler in the derived object is looked
up, an offset (+1) added to get to the second virtual method in the class, and the
function is called
WHAT HAPPENS AT RUNTIME?
21. Direct cost – vtable lookup (Put at about 5-10% overhead as opposed to non-
virtual methods)
Indirect cost –
Cannot inline virtual methods (Makes a big difference)
Each objects needs to store extra pointer (An issue for fine grained objects – say 1000000
link element objects, each containing 4 bytes extra!)
SO, WHAT’S THE COST?
22. Well, this gets worse when MI (Multiple Inheritance is
used)
Now, the deal is 15-20% overhead for MI for method
defined in the 2nd
and onwards base class. There’s no
penalty for methods in the first base class we derive from
Ensure your most frequently called methods are from the
FIRST base class you derive from if you use MI, order
your base classes accordingly
IS THAT ALL?
23. ZERO runtime cost (Abstraction without abstraction penalty)
However …
Coders not aware of how templates work ‘Under the hood’ end up creating code bloat
Developer turn around time increased due to longer compiles (This could be avoided too)
COST OF
COMPILE TIME
POLYMORPHISM
24. Code bloat happens because compilers cannot do
‘Commonality and variability analysis’
If the code body is the same for a set of template
argument values, the compiler fails to see that and
generates multiple classes for each argument value
Eg: list<int *>, list<char *, list<MyClass*> all of these
may have the same underlying implementation of a
‘Container of POINTERS’, but the compiler generates 3
different class definitions for these
TEMPLATE CODE BLOAT
25. A coder who knows this, does the following
Give a partial template specialization for POINTER types
Have that derive from a void * based container which has most of the implementation in
it
This specialized class will have simple inline methods that ‘Delegate’ to the base class and
gets work done
TEMPLATE CODE BLOAT
(CONTD)
26. So, we get the following result –
The thin inline wrappers in the specialized class offer type safety
Majority of the code body is in the non-template base class and hence no duplication of it
The thin wrappers are all inline hence no performance overhead as well
This is called the ‘Hoisting idiom’
TEMPLATE CODE BLOAT
(CONTD)
27. Happens mainly because the template method definitions have to be in the
header file
They may in turn pull in other headers and hence lead to large include sizes and
hence more compile time
More work to the compiler in case one uses template meta-programming
LONGER COMPILATION TIME
28. To reduce compile times, we can use the ‘Explicit instantiation’ technique in
cases where large includes are warranted
Here basically you give the method body in a .cpp file and put only the template
class definition in the .h file, but explicitly instantiate the template in the .cpp file
where the methods are defined for all types expected
WORKAROUND