SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie Virtual function (20)

Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
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
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Kürzlich hochgeladen (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

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