Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,"— Presentation transcript:

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

2 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.

3 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.

4 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.

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

6 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

7  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();  }

8  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

9

10  // 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  };

11

12  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.

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

14  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.

15  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.

16  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  };

17  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;

18  #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;  }

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

20  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 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

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

23

24  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.

25  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.

26  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


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

Similar presentations


Ads by Google