SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Inheritance and type casting
Inheritance
• It is a mechanism of creating a new class
from an already defined class
• The new class contains all attributes of the
old class in addition to some of its own
attributes.
• (REFER Ist UNIT PPT FOR
INHERITANCE AND POLYMORPHISM
CONCEPTS)
Virtual Functions

C++ matches a function call with the correct
function definition at compile time
− known as static binding

the compiler can match a function call with the
correct function definition at run time
− known as dynamic binding.
− declare a function with the keyword virtual if you
want the compiler to use dynamic binding for that
specific function.
Example• class A {
public:
virtual void f() { cout << "Class A" << endl; }
};
• class B: public A {
• public:
void f(int) { cout << "Class B" << endl; }
};
• class C: public B {
• public:
void f() { cout << "Class C" << endl; }
};
“Pure”ly Virtual

a virtual function declared with no definition
− base class contains no implementation at all

class containing a pure virtual function is an
abstract class
− similar to Java interfaces
− cannot instantiate from abstract classes

enforces a design through inheritance hierarchy
− inherited classes must define implementation
Example
• class A {
public:
virtual void f() = 0; // pure virtual
};
• class B: public A {
• public:
void f() { cout << "Class B" << endl; }
};
• class C: public B {
• public:
void f() { cout << "Class C" << endl; }
};
Run Time Type Information (RTTI)Run Time Type Information (RTTI)
• Always exists in OOP: a prerequisite for
dynamic binding
• Accessible to programmer?
– Not necessarily in statically typed languages
• Many things can be done without it!
– Almost always in dynamically typed
languages
• Without it, it is impossible to be sure that an
object will recognize a message!
• In LST, RTTI is the information accessible from
the instance_of pointer
RTTI in C++RTTI in C++
class typeinfo {
public:
virtual ~typeinfo(void);
bool operator==(const typeinfo&) const;
bool operator!=(const typeinfo&) const;
bool before(const typeinfo&) const;
const char *name(void) const;
private:
typeinfo(const typeinfo&);
typeinfo& operator= (const typeinfo&);
//.. Implementation dependent fields
};
class Base {
...
};
void f(Base *p)
{
const typeinfo& a = typeid(p);
// Type information for Base *
const typeinfo& a = typeid(*p);
// Actual run time type of *p
}
No RTTI in early
versions of the
language.
No feature should
incur a cost if not
• Dynamic Typing: no constraints on the
values
• stored in a variable.
• – Usually implies reference semantics
• • Run time type information: dynamic type‐
is
• associated with the value.
– –
};
Dynamic binding and casting
Dynamic casting
• Casting operator is for polymorphic object casting ,so that
it can cast from one object to another object.
• Dynamic cast is also called as safe cast.it succeeds only
when the pointer or reference being cast is an object of the
target type or derived type from it.
• The syntax is written as dynamic cast<ToobjectptrOr
ref>(FromobjectPtrOrRef)
• If we have a base class and a derived class,casting from
derived pointer to base pointer always succeeds.The
casting from base pointer to derived can be succeed only if
base is actually pointing to an object of derived one.
Rtti and templates
• If we want to test the type of the actual
variable and try to provide validations
according to the type we can use RTTI
for that.
Cross casting
• It refers to casting from derived to proper base class when there are multiple base classes in case of multiple inheritance.
• The
• dynamic_cast
• feature of C++ affords another kind of solution -- cross casting.
• Consider the following code.
• class A {public: virtual ~A();};
• class B {public: virtual ~B();};
• class C : public A, public B {};
• A* ap = new C;
• B* bp = dynamic_cast<B*>(ap);
• Notice that classes
• A
• and
• B
• are completely unrelated. Now when we create an
• instance of
• C
• we can safely upcast it to an
• A*
• . However, we can now take that pointer to
• A
• and
• cross cast
• it to a pointer to a B. This works because the
• A
• pointer ‘
• ap
• ’ really points at
• a
• C
• object; and
• C
• derives from
• B
• .
• Thus, we have cast
• accross
• the inheritance hierarchy between completely unrelated
• classes. It should be noted that this will
Down casting
rectangle::rectangle(float h, float w, int c, int l):pr(c, l)
{
height = h;
width = w;
xpos = 0;
ypos = 0;
};
void main()
{
rectangle rc(3.0, 2.0, 1, 3);
C++ statements;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Primitives in Generics
Primitives in GenericsPrimitives in Generics
Primitives in GenericsIvan Ivanov
 
Migration Objective-C to Swift
Migration Objective-C to SwiftMigration Objective-C to Swift
Migration Objective-C to SwiftNattapon Nimakul
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class StructureHadziq Fabroyir
 
Direct Acyclic Graph (DAG)
Direct Acyclic Graph (DAG)Direct Acyclic Graph (DAG)
Direct Acyclic Graph (DAG)RubyyatAbir
 
Adaptive Compilation by Jecel Mattos de Assumpção Jr
Adaptive Compilation by Jecel Mattos de Assumpção JrAdaptive Compilation by Jecel Mattos de Assumpção Jr
Adaptive Compilation by Jecel Mattos de Assumpção JrFAST
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptBrendan Eich
 
3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cflSampath Kumar S
 
(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercisesNico Ludwig
 
Rahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd YearRahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd Yeardezyneecole
 

Was ist angesagt? (20)

Primitives in Generics
Primitives in GenericsPrimitives in Generics
Primitives in Generics
 
Migration Objective-C to Swift
Migration Objective-C to SwiftMigration Objective-C to Swift
Migration Objective-C to Swift
 
Variables copy
Variables   copyVariables   copy
Variables copy
 
Pixel shaders
Pixel shadersPixel shaders
Pixel shaders
 
Tricks
TricksTricks
Tricks
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
 
Pixel shaders
Pixel shadersPixel shaders
Pixel shaders
 
Direct Acyclic Graph (DAG)
Direct Acyclic Graph (DAG)Direct Acyclic Graph (DAG)
Direct Acyclic Graph (DAG)
 
Thin Template Explained
Thin Template ExplainedThin Template Explained
Thin Template Explained
 
Adaptive Compilation by Jecel Mattos de Assumpção Jr
Adaptive Compilation by Jecel Mattos de Assumpção JrAdaptive Compilation by Jecel Mattos de Assumpção Jr
Adaptive Compilation by Jecel Mattos de Assumpção Jr
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Storage class
Storage classStorage class
Storage class
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 
C++ basic
C++ basicC++ basic
C++ basic
 
3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
 
C++
C++C++
C++
 
(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises
 
Rahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd YearRahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd Year
 

Andere mochten auch

Quý ông “treo máy” vì áp lực ngày tết
Quý ông “treo máy” vì áp lực ngày tếtQuý ông “treo máy” vì áp lực ngày tết
Quý ông “treo máy” vì áp lực ngày tếtkraig710
 
F Giordano Collins Fragmentation for Kaon
F Giordano Collins Fragmentation for KaonF Giordano Collins Fragmentation for Kaon
F Giordano Collins Fragmentation for KaonFrancesca Giordano
 
Presentation1
Presentation1Presentation1
Presentation1tyaawe
 
Published - Badovli Thermal Analysis
Published - Badovli Thermal AnalysisPublished - Badovli Thermal Analysis
Published - Badovli Thermal AnalysisHamidreza Araghian
 
Avijit KumarSingh.29.03.2015
Avijit KumarSingh.29.03.2015Avijit KumarSingh.29.03.2015
Avijit KumarSingh.29.03.2015Avijit Singh
 
Inama_Erin_PPP
Inama_Erin_PPPInama_Erin_PPP
Inama_Erin_PPPerininama
 
Startup Addventure Tep
Startup Addventure TepStartup Addventure Tep
Startup Addventure TepTep
 
Class 8 science chapter 6 lesson 1
Class 8 science chapter 6 lesson 1Class 8 science chapter 6 lesson 1
Class 8 science chapter 6 lesson 1Abdulláh Mámun
 
F Giordano Proton Spin from Sea Quarks
F Giordano Proton Spin from Sea QuarksF Giordano Proton Spin from Sea Quarks
F Giordano Proton Spin from Sea QuarksFrancesca Giordano
 
03 routing protocols-giai thua tim duong
03 routing protocols-giai thua tim duong03 routing protocols-giai thua tim duong
03 routing protocols-giai thua tim duongNông Ngọc Bảo
 

Andere mochten auch (20)

A
AA
A
 
Upstream facings in RCC dams
Upstream facings in RCC damsUpstream facings in RCC dams
Upstream facings in RCC dams
 
Quý ông “treo máy” vì áp lực ngày tết
Quý ông “treo máy” vì áp lực ngày tếtQuý ông “treo máy” vì áp lực ngày tết
Quý ông “treo máy” vì áp lực ngày tết
 
F Giordano Collins Fragmentation for Kaon
F Giordano Collins Fragmentation for KaonF Giordano Collins Fragmentation for Kaon
F Giordano Collins Fragmentation for Kaon
 
Presentation1
Presentation1Presentation1
Presentation1
 
Unit i
Unit iUnit i
Unit i
 
2014_Resume_ADouglas
2014_Resume_ADouglas2014_Resume_ADouglas
2014_Resume_ADouglas
 
Published - Badovli Thermal Analysis
Published - Badovli Thermal AnalysisPublished - Badovli Thermal Analysis
Published - Badovli Thermal Analysis
 
Chapter 10,-class-3
Chapter 10,-class-3Chapter 10,-class-3
Chapter 10,-class-3
 
Avijit KumarSingh.29.03.2015
Avijit KumarSingh.29.03.2015Avijit KumarSingh.29.03.2015
Avijit KumarSingh.29.03.2015
 
Pengantar Manajemen
Pengantar ManajemenPengantar Manajemen
Pengantar Manajemen
 
Inama_Erin_PPP
Inama_Erin_PPPInama_Erin_PPP
Inama_Erin_PPP
 
SOSIOLOGI POLITIK
SOSIOLOGI POLITIKSOSIOLOGI POLITIK
SOSIOLOGI POLITIK
 
Startup Addventure Tep
Startup Addventure TepStartup Addventure Tep
Startup Addventure Tep
 
ct0018
ct0018ct0018
ct0018
 
MOWCM AR15 final SR
MOWCM AR15 final SRMOWCM AR15 final SR
MOWCM AR15 final SR
 
Class 8 science chapter 6 lesson 1
Class 8 science chapter 6 lesson 1Class 8 science chapter 6 lesson 1
Class 8 science chapter 6 lesson 1
 
Service solahart jakarta selatan 082122541663
Service solahart jakarta selatan 082122541663 Service solahart jakarta selatan 082122541663
Service solahart jakarta selatan 082122541663
 
F Giordano Proton Spin from Sea Quarks
F Giordano Proton Spin from Sea QuarksF Giordano Proton Spin from Sea Quarks
F Giordano Proton Spin from Sea Quarks
 
03 routing protocols-giai thua tim duong
03 routing protocols-giai thua tim duong03 routing protocols-giai thua tim duong
03 routing protocols-giai thua tim duong
 

Ähnlich wie Unit iv

C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending PythonPriyank Kapadia
 
Presentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).pptPresentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).pptHarpreetKaur1382
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationGlobalLogic Ukraine
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptishan743441
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphismFALLEE31188
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
c++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfc++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfnisarmca
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxSurajgroupsvideo
 
C questions
C questionsC questions
C questionsparm112
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#ANURAG SINGH
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxWatchDog13
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxAshrithaRokkam
 

Ähnlich wie Unit iv (20)

UNIT IV (1).ppt
UNIT IV (1).pptUNIT IV (1).ppt
UNIT IV (1).ppt
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending Python
 
Presentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).pptPresentation on Polymorphism (SDS).ppt
Presentation on Polymorphism (SDS).ppt
 
Polymorphismupload
PolymorphismuploadPolymorphismupload
Polymorphismupload
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Constructor
ConstructorConstructor
Constructor
 
Basics of objective c
Basics of objective cBasics of objective c
Basics of objective c
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
c++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdfc++-language-1208539706757125-9.pdf
c++-language-1208539706757125-9.pdf
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptx
 
C questions
C questionsC questions
C questions
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 

Kürzlich hochgeladen

Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Kürzlich hochgeladen (20)

Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 

Unit iv

  • 2. Inheritance • It is a mechanism of creating a new class from an already defined class • The new class contains all attributes of the old class in addition to some of its own attributes. • (REFER Ist UNIT PPT FOR INHERITANCE AND POLYMORPHISM CONCEPTS)
  • 3. Virtual Functions  C++ matches a function call with the correct function definition at compile time − known as static binding  the compiler can match a function call with the correct function definition at run time − known as dynamic binding. − declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.
  • 4. Example• class A { public: virtual void f() { cout << "Class A" << endl; } }; • class B: public A { • public: void f(int) { cout << "Class B" << endl; } }; • class C: public B { • public: void f() { cout << "Class C" << endl; } };
  • 5. “Pure”ly Virtual  a virtual function declared with no definition − base class contains no implementation at all  class containing a pure virtual function is an abstract class − similar to Java interfaces − cannot instantiate from abstract classes  enforces a design through inheritance hierarchy − inherited classes must define implementation
  • 6. Example • class A { public: virtual void f() = 0; // pure virtual }; • class B: public A { • public: void f() { cout << "Class B" << endl; } }; • class C: public B { • public: void f() { cout << "Class C" << endl; } };
  • 7. Run Time Type Information (RTTI)Run Time Type Information (RTTI) • Always exists in OOP: a prerequisite for dynamic binding • Accessible to programmer? – Not necessarily in statically typed languages • Many things can be done without it! – Almost always in dynamically typed languages • Without it, it is impossible to be sure that an object will recognize a message! • In LST, RTTI is the information accessible from the instance_of pointer
  • 8. RTTI in C++RTTI in C++ class typeinfo { public: virtual ~typeinfo(void); bool operator==(const typeinfo&) const; bool operator!=(const typeinfo&) const; bool before(const typeinfo&) const; const char *name(void) const; private: typeinfo(const typeinfo&); typeinfo& operator= (const typeinfo&); //.. Implementation dependent fields }; class Base { ... }; void f(Base *p) { const typeinfo& a = typeid(p); // Type information for Base * const typeinfo& a = typeid(*p); // Actual run time type of *p } No RTTI in early versions of the language. No feature should incur a cost if not
  • 9. • Dynamic Typing: no constraints on the values • stored in a variable. • – Usually implies reference semantics • • Run time type information: dynamic type‐ is • associated with the value. – – }; Dynamic binding and casting
  • 10. Dynamic casting • Casting operator is for polymorphic object casting ,so that it can cast from one object to another object. • Dynamic cast is also called as safe cast.it succeeds only when the pointer or reference being cast is an object of the target type or derived type from it. • The syntax is written as dynamic cast<ToobjectptrOr ref>(FromobjectPtrOrRef) • If we have a base class and a derived class,casting from derived pointer to base pointer always succeeds.The casting from base pointer to derived can be succeed only if base is actually pointing to an object of derived one.
  • 11. Rtti and templates • If we want to test the type of the actual variable and try to provide validations according to the type we can use RTTI for that.
  • 12. Cross casting • It refers to casting from derived to proper base class when there are multiple base classes in case of multiple inheritance. • The • dynamic_cast • feature of C++ affords another kind of solution -- cross casting. • Consider the following code. • class A {public: virtual ~A();}; • class B {public: virtual ~B();}; • class C : public A, public B {}; • A* ap = new C; • B* bp = dynamic_cast<B*>(ap); • Notice that classes • A • and • B • are completely unrelated. Now when we create an • instance of • C • we can safely upcast it to an • A* • . However, we can now take that pointer to • A • and • cross cast • it to a pointer to a B. This works because the • A • pointer ‘ • ap • ’ really points at • a • C • object; and • C • derives from • B • . • Thus, we have cast • accross • the inheritance hierarchy between completely unrelated • classes. It should be noted that this will
  • 13. Down casting rectangle::rectangle(float h, float w, int c, int l):pr(c, l) { height = h; width = w; xpos = 0; ypos = 0; }; void main() { rectangle rc(3.0, 2.0, 1, 3); C++ statements; }