SlideShare uma empresa Scribd logo
1 de 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;
}

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Último

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 

Último (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 

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