SlideShare a Scribd company logo
1 of 4
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.
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: 7




VIRTUAL 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)
{
  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......";
}
getch();
 return 0;
}

** OUTPUT **

Enter a: 96
Enter b: 12
     (1) ADDITION.
     (2) SUBTRACTION.
     (3) MULTIPLICATION.
     (4) DIVISION.
Enter your choice: 4
Division is: 8

More Related Content

What's hot

C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
Jussi Pohjolainen
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 

What's hot (20)

Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Virtual function
Virtual functionVirtual function
Virtual function
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
virtual function
virtual functionvirtual function
virtual function
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
C++11
C++11C++11
C++11
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Constructor
ConstructorConstructor
Constructor
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
Compile time polymorphism
Compile time polymorphismCompile time polymorphism
Compile time polymorphism
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Viewers also liked

OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)
Kai-Feng Chou
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
Princess Sam
 

Viewers also liked (20)

pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)
 
Uid
UidUid
Uid
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance
InheritanceInheritance
Inheritance
 
Working with functions in matlab
Working with functions in matlabWorking with functions in matlab
Working with functions in matlab
 
Manipulators
ManipulatorsManipulators
Manipulators
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lec5
Lec5Lec5
Lec5
 
Roots of polynomials
Roots of polynomialsRoots of polynomials
Roots of polynomials
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Es272 ch3b
Es272 ch3bEs272 ch3b
Es272 ch3b
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Inheritance
InheritanceInheritance
Inheritance
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Parm
ParmParm
Parm
 
Array and string
Array and stringArray and string
Array and string
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 

Similar to Virtual function

(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
AakashBerlia1
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 

Similar to Virtual function (20)

Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
 
Presentation
PresentationPresentation
Presentation
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Lecture05
Lecture05Lecture05
Lecture05
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 

More from harman kaur

Program to illustrate Switch, Goto and Exit statements.
Program to illustrate Switch, Goto and  Exit statements.Program to illustrate Switch, Goto and  Exit statements.
Program to illustrate Switch, Goto and Exit statements.
harman kaur
 
Quiz on Logic Gate
Quiz on Logic GateQuiz on Logic Gate
Quiz on Logic Gate
harman kaur
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
harman kaur
 
Introduction to digital electornics
Introduction to digital electornicsIntroduction to digital electornics
Introduction to digital electornics
harman kaur
 

More from harman kaur (14)

Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
 
Creating red black tree
Creating red black treeCreating red black tree
Creating red black tree
 
c plus plus programsSlide
c plus plus programsSlidec plus plus programsSlide
c plus plus programsSlide
 
Cpp programs
Cpp programsCpp programs
Cpp programs
 
Program to illustrate Switch, Goto and Exit statements.
Program to illustrate Switch, Goto and  Exit statements.Program to illustrate Switch, Goto and  Exit statements.
Program to illustrate Switch, Goto and Exit statements.
 
Digital u1
Digital u1Digital u1
Digital u1
 
Quiz on Logic Gate
Quiz on Logic GateQuiz on Logic Gate
Quiz on Logic Gate
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
 
Msql query
Msql queryMsql query
Msql query
 
Rules of inference
Rules of inferenceRules of inference
Rules of inference
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Introduction to digital electornics
Introduction to digital electornicsIntroduction to digital electornics
Introduction to digital electornics
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 

Recently uploaded

Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
Alexander Litvinenko
 

Recently uploaded (20)

How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING IIII BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
Benefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptxBenefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptx
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 

Virtual function

  • 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. 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: 7 VIRTUAL 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. { 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. getch(); return 0; } ** OUTPUT ** Enter a: 96 Enter b: 12 (1) ADDITION. (2) SUBTRACTION. (3) MULTIPLICATION. (4) DIVISION. Enter your choice: 4 Division is: 8