Virtual function

1,621 views
1,428 views

Published on

VIRTUAL FUNCTIONS IN C++.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,621
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Virtual function

  1. 1. IMPORTANT POINTS RELATED TO VIRTUAL FUNCTIONS VIRTUAL FUNCTIONS ARE USED IN CASE OF FUNCTION OVER RIDING. FUNCTION OVERRIDING MEANS BASE CLASS AND DERIVED CLASS HAVING SAME FUNCTION NAME AND ARGUMENTS, BUT PERFORM DIFFERENT OPERATIONS. VIRTUAL FUNCTIONS ARE USED TO ACCESS BOTH BASE CLASS AND DERIVED CLASS FUNCTIONS. VIRTUAL FUNCTIONS DECLARATION IS SAME AS OTHER FUNCTION BUT THE VIRTUAL KEYWORD IS PRECEEDED WITH THE FUNCTIONS DECLARATION. VIRTUAL FUNCTIONS ARE ACCESSED USING POINTERS: (*ptr).functioname(); Or ptr->functionname(); Where *ptr is a pointer to base class object. When base class pointer, points to base class object’s address as ptr =&b1, where b1 is the object of base class. We can access the base class function. And when It points to derived class object’s address, we can access derived class functions.SIMPLE PROGRAM SHOWING THE EFFECT OF VIRTUAL FUNCTION. #include<iostream.h> #include<conio.h> class one //base class { public: int a,b; void get_data() { cout<<"enter a:"; cin>>a; cout<<"enter b:"; cin>>b; cout<<"n a is:"<<a; cout<<"n b is:"<<b; } virtual void cal() //virtual function over rided in derived class { cout<<"sum is"<<a+b; } }; class two :public one //two class derived from one { void cal() { cout<<"sub is"<<a-b; } }; int main() { one o1,*ptr; //created object and pointer to base class object.
  2. 2. two t1; // created object of derived class. clrscr(); cout<<"n PTR POINTS TO BASE CLASS.."; ptr=&o1; ptr->get_data(); ptr->cal(); cout<<"n PTR POINTS TO DERIVED CLASS..."; ptr =&t1; ptr->get_data(); ptr->cal(); getch(); return 0; } **OUTPUT** PTR POINTS TO BASE CLASS. Enter a: 45 Enter b: 12 Sum is: 57 PTR POINTS TO DERIVED CLASS. Enter a: 12 Enter b: 5 Sub is: 7VIRTUAL FUNCTION USED TO PERFORM VARIOUS MATHEMATICAL OPERATIONS.#include<iostream.h>#include<conio.h>class add{ public: virtual int calculate(int num1, int num2)//virtual func overloaded in der classes... { return num1+num2; }};class sub:public add{ public: int calculate (int num1, int num2) { return num1-num2; }};class mul:public sub{ public: int calculate(int num1, int num2)
  3. 3. { return num1*num2; }};class div:public mul{ public: int calculate(int num1, int num2) { return num1/num2; }};int main(){ int a,b,ch; clrscr(); cout<<"enter a:"; cin>>a; cout<<"enter b:"; cin>>b; cout<<"n(1)ADDITION n (2)SUBTRACTION n (3) MULTIPLICATION n (4) DIVISIONn Enter your choice"; cin>>ch;add a1,*ptr;sub s1;mul m1;div d1;switch(ch){ case 1: ptr =&a1; cout<<"SUM IS:"<<ptr->calculate(a,b); break; case 2: ptr =&s1; cout<<"SUBTRACTION IS:"<<ptr->calculate(a,b); break; case 3: ptr=&m1; cout<<"MULTIPLICATION IS:"<<ptr->calculate(a,b); break; case 4: ptr=&d1; cout<<"DIVISION IS:"<<ptr->calculate(a,b); break; default: cout<<"WRONG CHOICE......";}
  4. 4. getch(); return 0;}** OUTPUT **Enter a: 96Enter b: 12 (1) ADDITION. (2) SUBTRACTION. (3) MULTIPLICATION. (4) DIVISION.Enter your choice: 4Division is: 8

×