0% found this document useful (0 votes)
2 views

virtual function_C++ qd java

The document explains the concept of virtual functions in C++, which are member functions that can be overridden in derived classes, enabling polymorphism. It outlines the properties, rules, and differences between virtual and non-virtual functions, emphasizing dynamic binding at runtime. Additionally, it provides code examples demonstrating the implementation and behavior of virtual functions in a base and derived class context.

Uploaded by

aaammm1522
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

virtual function_C++ qd java

The document explains the concept of virtual functions in C++, which are member functions that can be overridden in derived classes, enabling polymorphism. It outlines the properties, rules, and differences between virtual and non-virtual functions, emphasizing dynamic binding at runtime. Additionally, it provides code examples demonstrating the implementation and behavior of virtual functions in a base and derived class context.

Uploaded by

aaammm1522
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Virtual Function

Manoj Pawaiya
Lect. IT Dept.
IET DAVV,INDORE
virtual function
• “In object-oriented programming, a virtual
function or virtual method is a function ormethod whose
behaviour can be overridden within an inheriting class by a
function with the same signature. This concept is a very
important part of the polymorphism portion ofobject-
oriented programming (OOP).”

• ”C++ virtual function is a member function of a class,


whose functionality can be over-ridden in its
derived classes. The whole function body can
be replaced with a new set of implementation in
the derived class. The concept of c++ virtual functions is
different from C++ Function overloading.”
C++ Virtual Function - Properties:
C++ virtual function is,
A member function of a class
Declared with virtual keyword
Usually has a different functionality in the derived class
A function call is resolved at run-time

The difference between a non-virtual c++ member function


and a virtual member function is, the non-virtual
member functionsare resolved at compile time.
This mechanism is called static binding. Where as the c++
virtual member functions are resolved during run-time.
This mechanism is known as dynamic binding.
Rules for Virtual Function
1. The virtual function must be member of some class.
2. They can not be static members.
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
through it may not be used.
6. The prototype of base class version of a virtual function
and all the derived class version must be identical. If the
function with same name have different prototype, C++
considered them as overloaded functions, and the virtual
function mechanism is ignored.
7.We can not have virtual constructors, but we can have virtual
destructors.

8. While a base pointer can point to any type of derived object ,


the
reverse is not true. This is to say, we can not use a pointer to a
derived Class to access an object of the base type.

9. When a base pointer point to a derived Class, incrementing or


decrementing it will not make it to point to the next object of the
derived Class. It is incremented or decremented only relative to
its base type. Therefore , we should not use this method to move
the pointer to the next object.

10. 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.
Virtual Function
class Base
{
Public:
void display()
{
cout<<“ \n Display base” ;
}
virtual void show()
{
cout<<“ \n Show base” ;
}
};
Class Derived : public Base
{
Public :
void display()
{
cout<<“ \n Display Derived” ;
}
void show()
{
cout<<“ \n Show Derived” ;
}
};
int main()
{
Base B ;
Derived D ; *****output******
Base *bptr ;

cout<<“\n bptr point to base \n” ;


bptr = &B; bptr point base
Display base
bptr -> display(); // calls base version Show base
bptr -> show(); // calls base version
bptr point to Derived
cout<<“\n bptr point to Derived \n” ; Display base
Show base
bptr = &D;
bptr -> display(); // calls base version
bptr -> show(); // calls Derived version
return 0; }
Run time poly morphism
class media
{
protected:
char title[50];
float price;
public:
media(char *s, float a)
{
strcpy(title,s);
price = a;
}
virtual void display() { }
};
class book: public media
{
int pages ;
public:
book(char *s, float a,int p) :media(s,a)
{
pages = p;
}
void display() { }
};
class tape: public media
{
float time ;
public:
tape(char *s, float a, float t) :media(s, a)
{
time = t;
}
void display() ;
};
void book :: display ()
{
cout<<“\n Title :”<<title ;
cout<<“\n Page :”<<page ;
cout<<“\n Price :”<<price ;
}
void tape :: display ()
{
cout<<“\n Title :”<<title ;
cout<<“\n Plat time :”<<time<<“mins” ;
cout<<“\n Price :”<<price ;
}
int main()
{
char * title = new char[30];
float price, time ;
int page;
// BOOK details
cout<<“\n Enter Book Details \n”;
cout<<“\n Title :” ; cin>>title ;
cout<<“\n Page :” ; cin>>pages ;
cout<<“\n Price :” ; cin>>price ;
}
// Tape details
cout<<“\n Enter Tape Details \n”;
cout<<“\n Title :” ; cin>>title ;
cout<<“\n Price :” ; cin>>price ;
cout<<“\n Play time(min) :” cin>>time ;
}
tape tape1(title, price,time) ;
media * list[2];
list[0] = &book ;
list[1] = &tape1 ;
cout<< “\n MEDIA DEATAILS” ;
cout<<“\n ………book…….” ;
list[0] ->display(); // BOOK details
cout<<“\n ………tape…….” ; Enter Book Details
list[1] ->display(); // Tape details Title: progaming in c
return 0; Price: 88
} Pages: 400
Enter Tape Details
Title: computing concept
Price: 90
OUTPUT Pay time(min): 55
MEDIA DEATAILS
………book…….
Title: progaming in c
Pages: 400
Price: 88

………tape…….
Title: computing concept
Pay time(min): 55
Price: 90

You might also like