 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,

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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Cpt S 122 – Data Structures 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 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
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.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
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.
Object Oriented Programming
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
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
7. Inheritance and Polymorphism
Introduction to C++ Dynamic Binding and User Defined Type Conversions
Andy Wang Object Oriented Programming in C++ COP 3330
Polymorphism &Virtual Functions
Methods Attributes Method Modifiers ‘static’
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Programming Language
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
CS410 – Software Engineering Lecture #8: Advanced C++ III
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:

 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization, the this pointer, Dynamic type information

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.

The virtual keyword indicates to the compiler that  it should choose the appropriate definition of a function not by the type of reference, but by the type of object that the reference refers to.

Therefore,  a virtual function is a member function you may redefine for other derived classes,  can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class,  even if you call that function with a pointer or reference to a base class of the object. A class that declares or inherits a virtual function is called a polymorphic class.

prefix declaration with the virtual keyword redefine a virtual member function in any derived class this is called overriding  understand the contrast with overloading

overridden function must have same name and same parameter list  no need to use the virtual keyword again  return type can be different if the parameter lists are different, they are considered different  in this case, it is not overridden, but hidden  hidden methods cannot be called

 class base  {  public:  virtual void show()  {  cout<<"\n Base class show:";  }  void display()  {  cout<<"\n Base class display:" ;  }  };   class drive:public base  {  public:  void display()  {  cout<<"\n Drive class display:";  }  void show()  {  cout<<"\n Drive class show:";  }  };  void main()  {  base obj1;  base *p;  cout<<"\n\t P points to base class:\n" ;   p=&obj1;  p->display();  p->show();   cout<<"\n\n\t P points to drive class:\n";  drive obj2;  p=&obj2;  p->display();  p->show();  getch();  }

 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; } };  int main()  { B b; C c; A* pa1 = &b; A* pa2 = &c; // b.f(); pa1->f(); pa2->f(); }  Outputs:  Class A  Class C

 // ambiguous reference to base class  class Parent  {  protected:  int basedata;  };  class Child1 : public Parent  { };  class Child2 : public Parent  { };  class Grandchild : public Child1, public Child2  {  public:  int getdata()  { return basedata; } // ERROR: ambiguous  };

 An abstract class is a class that can only be derived from; no objects can be instantiated from it.  Its purpose is to define an interface and provide a common base class for derived classes.  A base class becomes an abstract class by making its constructor(s) protected or by declaring a virtual function pure: virtual void statement()=0;  Derived classes must implement all pure virtual functions. If a derived class does not implement these functions, then it becomes an abstract class as well.  Abstract classes are not required to implement their pure virtual functions.

 class A  {  public :  virtual void show()=0;  };  class B  {  public :  void disp() {cout<<"B";}  };  class C : public A  {  public :  void show() { cout<<"C";}  };  Int main()  {  C c1;  A a1; //Error  B b1;  A *arr[2];  arr[0] = &c1;  arr[1] = &b1; //Error  return 0;  }

 is a special function that can access and modify private or protected data members of a class.  is not a member function, i.e. it is not in the scope of the class to which it has been declared as friend. Since it is not in the scope of the class, it cannot be called using object of that class.  Can be invoked like a normal function, without the help of any object.  Unlike member functions, it cannot access the members directly and has to use an object and the dot operator with each member name.  It can be declared either in public or private part of a class without affecting its meaning.  Usually it has objects as arguments.  A class has to specify in its declaration, who all are its friends.

 If we want a function to operate on Objects of two different classes.  Then the function will take objects of two different classes as arguments and operate on their private data.  In this situation we can use a Friend function that can act as a bridge between two classes.

 Are special classes, whose all member functions can access and modify private/protected data members of another class.  However, a class has to specify in its declaration, who all are its friends.  Class Z  {  ………  friend class X; // all member functions of X are  friends to Z  };

 Provide a very generic functionality that doesn’t require us to instantiate the class,i.e. static member functions declared in the public part of a class declaration can be accessed without specifying an object of the class.  Can access only static members (functions or data members).  Cannot be a const member function (Inspector).  A static member function can be called using the class name (instead of its object) as follows:  class_name:: function_name;

 #include "iostream"  # include  using namespace std;  class Person  {  private:  static int count;  char name[50];  public:  void getname()  {  cout<<"name:";  cin>>name;  }  void setname()  {  cout<<"The Name is:"<<name<<endl;  count++;  }  static void getcount()  {  cout<<"count:"<<count<<endl;  }  };  int Person :: count ;  int main()  {  Person p1,p2,p3;  Person::getcount();  p1.getname();  p2.getname();  p3.getname();  p1.setname();  p2.setname();  p3.setname();  Person::getcount();  getch();  return 0;  }

 class Data  {  int i;  public:  Data() { }  Data( int x) { i = x; }  Data( Data &a) { i = a.i; }  void disp()  { cout<<i; }   Data operator = (Data &D)  {  i = D.i;  cout<<” In Asignment function “ <<endl;  return Data(i);  }  };  int main()  {  Data D(10); // this is copy initialization   Data D1;  D1=D; // invokes overloaded =  D1.disp();  Data D2 = D1; // does not invoke =, this is copy initialization   D2.disp();  return 0;  }

 It points to the current object, that is, object that invoked the method.  ‘this’ pointer is a pointer that is automatically available in all non-static member functions of a class. It allows objects to access its own address  When a member function is called, it is automatically passed an implicit argument that is a pointer to the invoking object

21  Within a member function, the this keyword is a pointer to the current object, i.e. the object through which the function was called  C++ passes a hidden this pointer whenever a member function is called  Within a member function definition, there is an implicit use of this pointer for references to data members pData nLength this Data member referenceEquivalent to pDatathis->pData nLengththis->nLength

 class Alpha  {  private:  int alpha;  public:  void tester()  {  this->alpha = 11; //same as alpha = 11;  cout alpha<<endl;  //same as cout << alpha;  }  void viewthis()  {  cout<<"the address of this:"<<this;  }  };  int main()  {  Alpha w;  w.tester();  w.viewthis();  cout << endl;  getch();  return 0;  }

 It is possible to find the information about an object’s class and even change the class of an object at runtime. The two mechanisms for the same are:  dynamic_cast operator  typeid operator  for both to work, the compiler must enable Run- Time Type Information (RTTI). Include the header file typeinfo.

 For dynamic_cast to work, the base class must be polymorphic; that is, it must have at least one virtual function.  The dynamic_cast operator syntax is dynamic_cast (expr) where expr is an expression pointing to a direct or indirect base class object and type is a derived class.  expr is converted to a pointer of the specified type if the dynamic type of the object pointed to is that type or is a type derived from that type. Otherwise, the result of the cast operation is zero.

 typeid allows to check the type of an expression: typeid (expression)  Returns a reference to an object of class type_info ▪ Contains the information about the type of its operand ▪ type_info member function name ▪ Returns a pointer-based string that contains the type name of the argument passed to typeid  Must include header file