Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Polymorphism, Virtual Methods and Abstract Classes.
Inheritance, Polymorphism, and Virtual Functions
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Chapter 12: Adding Functionality to Your Classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
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.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism, Virtual Methods and Abstract Classes
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Polymorphism & Virtual Functions
Polymorphism Lec
Modern Programming Tools And Techniques-I Inheritance
Interfaces.
OOP’S Concepts in C#.Net
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Interfaces.
METHOD OVERRIDING in JAVA
Polymorphism Polymorphism
Constructors and Destructors
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
CLASSES AND OBJECTS.
Inheritance: Polymorphism and Virtual Functions
Inheritance and Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
Eighth step for Learning C++ Programming
C++ Polymorphism Reference and pointer implicit type casting
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Topic 1 11/18/2015

Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas Khan : Roll No: /18/2015

Virtual Function: 3 11/18/2015 A type of function that appears to exist in some part of a program but does not exist really is called virtual function. A virtual function is a member function that is declared within a base class (Parent Class) and redefined by a derived class (Child Class). Virtual functions are used to implement polymorphism. They enable the user to execute completely different function by the same function call. To create virtual function, precede the function’s declaration in the base class with the keyword virtual.

Example: #include Class A { int a; public: A( ) { a = 1; } virtual void show( ) { cout <<a; } }; Class B: public A { int b; public: B( ) { b = 2; } virtual void show() { cout <<b; } }; int main() { A *pA; B oB; pA = &oB; pA->show(); return 0; } Since pA points to object of B and show( ) is virtual in base class A. Output =2 4 11/18/2015

Pure Virtual Function: A type of virtual function that has no body is known as pure virtual function. A pure virtual function that has the notation "= 0" in the declaration of that function. 5 11/18/2015

Example: Simple example of a pure virtual function in C++ Class someclass { public: virtual void pure_virtual ( ) = 0; / / a pure virtual function / / note that there is no function body }; 6 11/18/2015

When should pure virtual functions be used in C++? In C++, a regular, "non-pure" virtual function provides a definition, which means that the class in which that virtual function is defined does not need to be declared abstract. The pure virtual functions are used in the classes which are not used directly. The user can inherit the class and then override the pure virtual function in the child class. 7 11/18/2015

Abstract classes: A type of class that contains any pure virtual function is called abstract class. An abstract class cannot be used direclty. It means that no object of an abstract class can be created. However, a child class can inherit an abstract class and use it by overriding its pure virtual function. It is also defined by using the keyword “virtual”. 8 11/18/2015

Characteristics of Abstract Class: Abstract classes are used to create a model class. Abstract class can have normal functions and variables along with a pure virtual function. Abstract classes are mainly used for Up casting, so that its derived classes can use its interface. 9 11/18/2015

Example: class Base //Abstract base class{ public: virtual void show() = 0; //Pure Virtual Function}; class Derived: public Base{ public: void show() { cout << "Implementation of Virtual Function in Derived class"; }}; int main(){ Base obj; //Compile Time Error Base *b; Derived d; b = &d; b->show( );} 10 11/18/2015

Virtual Base Classes A class that is inherited by child classes it is known as virtual base class. The virtual base classes are necessary in a situation where the member of base class is duplicated in multilevel inheritance. This can be achieved by preceding the base class’ name with the word virtual /18/2015

Example: #include Class Parent { protected: int n; }; class Child 1: public Parent { }; class Child 2 : public Parent { }; class Baby : public Child 1, public Child 2 { public : void set ( ) { n = 5; cout <<”n=”<<n<<endl’ } }; void main ( ) { Baby obj; Obj.set ( ); getch ( ); } Output is: “5” 12 11/18/2015

Flow chart about above program is: 13 11/18/2015

14 11/18/2015