1Q: Explain about the 'virtual function'?
ans: When the same function name is used in both the base class and derived class, Base class function is called even thought it is referenced with derived class object pointer and the derived class function will never be called. This problem can be over comes by using virtual function.
virtual function always the takes the keyword virtual, which is used to implement runtime polymorphism.
syntax:
Need for virtual functions
2.bc function and dc function must have same name
3.virtual functions must be members of same class.
4. They are accessed using object pointer.
5. They can be friend function to another class.
6. A virtual function is the base class must be defined eventhough it is not used.
7. the class cannot have a virtual constructor, but can contain a virtual destructor.
8. virtual function can not be static members of the class.
9. to realize the potential benefits of virtual functions supporting runtime polymorphism, they should be declared in the public section of the class.
2)one or more inherited function in the derived class may be modified.
3)to achieve dynamic binding in compilers.
4)we can make different objects of same class behave differently for the same name.
empty function (or) dummy function. size is 1 byte.
(or)
#include<iostream.h>
#include<conio.h>
class india
{
public:
virtual void display()=0;
};
class ap: public india
{
public:
void display()
{
cout<<"welcome to AP \n";
}
};
class hyd:public india
{
public:
void display()
{
cout<<"welcome to hyd \n";
}
};
void main()
{
// india i; //ERROR we can not create instance(object) abstract class
india *p;
clrscr();
ap a;
hyd h;
p=&a;
p->display();
p=&h;
p->display();
getch();
}
/*
welcome to AP
welcome to hyd */
ans: When the same function name is used in both the base class and derived class, Base class function is called even thought it is referenced with derived class object pointer and the derived class function will never be called. This problem can be over comes by using virtual function.
virtual function always the takes the keyword virtual, which is used to implement runtime polymorphism.
1)
Virtual
function is a member function that is defined in the base class and can be
redefined by the derived classes. With the help of virtual function one can
implement dynamic polymorphism.
Syntax:
virtual return-type
function-name(args); syntax:
class baseclass
{
public :
virtual returntype func1()
{
----;
----;
}
virtual returntype func2()
{
------;
------;
}
};
class derivedclass : public baseclass
{
public:
returntype fun1()
{
-----;
-----;
}
returntype fun2()
{
-----;
-----;
}
};
void main()
{
baseclass *poiner ;
derivedclass object;
pointer=&object;
p->fun1();
}
/* example program for virtual function */
#include<iostream.h>
#include<conio.h>
class parent
{
public:
virtual void show( )
{
cout <<” I’m parent \n”;
}
};
class child : public parent
{
public:
void show( )
{
cout<<” I’m child \n”;
}
};
void main( )
{
parent p;
parent *p;
child c;
clrscr( );
ptr=&p;
ptr-> show( );
ptr=&c;
ptr->show( );
getch ( );
}
o/p
I’m parent
I’m child
1) Virtual functions are mandatory for implementing
Dynamic Binding:
Explanation:
By default, C++ matches a function call
with the correct function definition at compile time. This is called static binding (Early Binding). It is possible to
specify that the compiler match a function call with the correct function
definition at run time; this is called dynamic
binding (Late Binding). Declaring a function with the keyword virtual instructs
the compiler to use dynamic binding for that specific function.
The following examples demonstrate the
differences between static and dynamic binding. The first example demonstrates
static binding:
Example
for Static Binding:
#include <iostream>
using namespace std;
struct A {
void f() { cout
<< "Class A" << endl; }
};
struct B: A {
void f() { cout
<< "Class B" << endl; }
};
void g(A& arg) {
arg.f();
}
int main() {
B x;
g(x);
}
The following is the output of the above
example:
Class A
When function g() is called,
function A::f() is called, although the argument refers to an object
of type B. At compile time, the compiler knows only that the argument of
function g() will be a reference to an object derived from A; it
cannot determine whether the argument will be a reference to an object of
type A or type B. However, this can be determined at run time.
The following example is the same as the previous example, except
that A::f() is declared with the virtual keyword:
Example
for Dynamic Binding:
#include <iostream>
using namespace std;
struct A {
virtual void f() {
cout << "Class A" << endl; }
};
struct B: A {
void f() { cout
<< "Class B" << endl; }
};
void g(A& arg) {
arg.f();
}
int main() {
B x;
g(x);
}
The following is the output of the above
example:
Class B
The virtual keyword indicates to
the compiler that it should choose the appropriate definition
of f() 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, and 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.
2)
Polymorphic
Class:
A class that declares or inherits a virtual function is called a polymorphic
class.
You redefine a virtual member function,
like any member function, in any derived class. Suppose you declare a virtual
function named f in a class A, and you derive directly or
indirectly from A, a class named B. If you declare a function
named f in class B with the same name and same parameter
list as A::f, then B::f is also virtual (regardless whether or
not you declare B::f with the virtual keyword) and it overrides A::f.
However, if the parameter lists of A::f and B::f are
different, A::f and B::f are considered
different, B::f does not override A::f, and B::f is not
virtual (unless you have declared it with the virtual keyword).
Instead B::f hides A::f. The following example
demonstrates this:
#include <iostream>
using namespace std;
struct A {
virtual void f() {
cout << "Class A" << endl; }
};
struct B: A {
void f(int) { cout
<< "Class B" << endl; }
};
struct C: B {
void f() { cout
<< "Class C" << endl; }
};
int main() {
B b; C c;
A* pa1 = &b;
A* pa2 = &c;
// b.f();
pa1->f();
pa2->f();
}
The following is the output of the above
example:
Class A
Class C
3)
A
virtual function cannot be global or static because, by definition, a virtual
function is a member function of a base class and relies on a specific object
to determine which implementation of the function is called. You can declare a
virtual function to be a friend of another class.
4)
If
a function is declared virtual in its base class, you can still access it
directly using the scope resolution (::) operator. In this case, the virtual
function call mechanism is suppressed and the function implementation defined
in the base class is used. In addition, if you do not override a virtual member
function in a derived class, a call to that function uses the function
implementation defined in the base class.
5)
A
virtual function must be one of the following:
·
Defined
·
Declared pure
(The class will become an abstract class)
·
Defined and declared pure (The class will become an abstract class)
Need for virtual functions
What are the Rules for virtual function
1.virtual keyword must be declared in base class2.bc function and dc function must have same name
3.virtual functions must be members of same class.
4. They are accessed using object pointer.
5. They can be friend function to another class.
6. A virtual function is the base class must be defined eventhough it is not used.
7. the class cannot have a virtual constructor, but can contain a virtual destructor.
8. virtual function can not be static members of the class.
9. to realize the potential benefits of virtual functions supporting runtime polymorphism, they should be declared in the public section of the class.
Applications of virtual functions
1)we can create many derived classes from same base class.2)one or more inherited function in the derived class may be modified.
3)to achieve dynamic binding in compilers.
4)we can make different objects of same class behave differently for the same name.
What is pure virtual function
A
pure virtual function is a member function, which has no body, the
programmer must add the notation=0 for the declaration of the pure
virtual fuction in the base class.
syntax:
virtual void virtual function()=0;
here 0 indicates that display is pure virtual function (or)empty function (or) dummy function. size is 1 byte.
(or)
virtual void virtual function()
{
;
}
Abstract Base Class: if BASE CLASS contains at least one 'pure virtual function'
then it is known as Abstract Base class.*/
then it is known as Abstract Base class.*/
#include<iostream.h>
#include<conio.h>
class india
{
public:
virtual void display()=0;
};
class ap: public india
{
public:
void display()
{
cout<<"welcome to AP \n";
}
};
class hyd:public india
{
public:
void display()
{
cout<<"welcome to hyd \n";
}
};
void main()
{
// india i; //ERROR we can not create instance(object) abstract class
india *p;
clrscr();
ap a;
hyd h;
p=&a;
p->display();
p=&h;
p->display();
getch();
}
/*
welcome to AP
welcome to hyd */
No comments:
Post a Comment