Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
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 with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
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.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
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
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
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,
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
A First Book of C++ Chapter 12 Extending Your Classes.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Polymorphism Lec
Abstract Classes AKEEL AHMED.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Polymorphism Polymorphism
Constructors and Destructors
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CISC/CMPE320 - Prof. McLeod
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
Chapter 6 Polymorphism.
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:

Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment and copy initialization, the this pointer. Dynamic type information.

Pointers to Derived Classes Pointers to objects of a base class are type compatible with pointers to objects of a derived class. Example - If B is a base class and D is a derived class from B then a pointer declared as a pointer to B can also be a pointer to D. B *bptr; B b; D d; bptr = &b; We can make bptr to point to D also - bptr = &d; This s a perfectly valid argument in C++, as d is an object derived from the class B

#include using namespace std; class BaseClass { int x; public: void setx(int i) { x = i; } int getx() { return x; } }; class DerivedClass : public BaseClass { int y; public: void sety(int i) { y = i; } int gety() { return y; } }; int main() { BaseClass *p; BaseClass baseObject; DerivedClass derivedObject; p = &baseObject; p->setx(10); cout getx(); p = &derivedObject; derivedObject.sety(88); cout << derivedObject.gety(); p->setx(99); cout getx(); return 0; }

Virtual Functions A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs. Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.

#include using namespace std; class B { public: virtual void display() { cout<<"Content of base class"; } }; class D1 : public B { public: void display() { cout<<"Content of first derived class"; } } ; class D2 : public B { public: void display() { cout<<"Content of second derived class"; } }; int main() { B *b; D1 d1; D2 d2; b = &d1; b->display(); b = &d2; b->display(); return 0; }

Runtime Polymorphism C++ allows binding to be delayed till run time. When you have a function with same name, equal number of arguments and same data type in same sequence in base class as well derived class and a function call of form: base_class_type_ptr->member_function(args); will always call base class member function. The keyword virtual on a member function in base class indicates to the compiler to delay the binding till run time. Every class with atleast one virtual function has a vtable that helps in binding at run time.vtable Looking at the content of base class type pointer it will correctly call the member function of one of possible derived / base class member function.

Rules For Virtual Functions 1.The virtual functions must be members of some class. 2.They cannot be static member. 3.They are accessed by using object pointers. 4.A virtual function can be a friend of another class. 5.A virtual function in a base class must be defined, even though it may not be used. 6.The prototypes of the base class version of a virtual function and all the derived class versions must be identical. 7.We cannot have virtual constructors, but we can have virtual destructors. 8.While a base pointer can point to any type of the derived objects, the reverse is not true. 9.If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.

Pure Virtual Functions A Pure virtual function or Pure virtual method is a virtual function that is required to be implemented by a derived class. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. A subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class.subclass Pure virtual methods typically have a declaration (signature) and no definition (implementation).declaration

Abstract Classes An abstract class is a class that cannot be instantiated (object is not created). An abstract class is usually implemented as a class that has one or more pure virtual functions. Characteristics of Abstract Classes – 1.Abstract class cannot be instantiated,but pointers & references of abstract class type can be created. 2.Abstract class can have normal functions & variables along with a pure virtual function. 3.Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface. 4.Classes inheriting an abstract class must implement all pure virtual functions, or else they will become abstract too.

Virtual Base Classes When two or more classes are derived from a common base class, we can prevent multiple copies of the base class being present in an object, derived from those objects, by declaring the base class as virtual. Such a base class is known as virtual base class. This can be achieved by preceding the base class name with the word virtual.

class A { public: int i; }; class B : virtual public A { public: int j; }; class C: virtual public A { public: int k; }; class D: public B, public C { public: int sum; }; int main() { D ob; ob.i = 10; ob.j = 20; ob.k = 30; ob.sum = ob.i + ob.j + ob.k; cout << “Value of i is : ”<< ob.i; cout << “Value of j is : ”<< ob.j; cout << “Value of k is :”<< ob.k; cout << “Sum is : ”<< ob.sum; return 0; }

Static Functions We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

#include using namespace std; class gamma { private: static int total; int id; public: gamma() { total++; id = total; } ~gamma() { total--; cout << id; } int main() { gamma g1; gamma::showtotal(); gamma g2, g3; gamma::showtotal(); g1.showid(); g2.showid(); g3.showid(); return 0; } static void showtotal() { cout <<total; } void showid() { cout << id; } }; int gamma::total = 0;

This Pointer The ‘this’ pointer is passed as a hidden argument to all non- static member function calls and is available as a local variable within the body of all non-static functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

#include class max { int a; public: void getdata() { cout<<"Enter the Value :"; cin>>a; } max &greater(max &x) { if(x.a>=a) return x; else return *this; } void display() { cout<<a; } }; main() { max one,two,three; one.getdata(); two.getdata(); three=one.greater(two); three.display(); getch(); }