SlideShare a Scribd company logo
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

Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Virtual function
Virtual functionVirtual function
Virtual function
Burhan Ahmed
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismJussi Pohjolainen
 
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
Geeks Anonymes
 
C++11
C++11C++11
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
ramya marichamy
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
Open Gurukul
 
C++ Programming
C++ ProgrammingC++ 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 operatorJussi Pohjolainen
 
C++ Training
C++ TrainingC++ Training
C++ Training
SubhendraBasu5
 
(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
Nico Ludwig
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 

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

pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
rattaj
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
Docent Education
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
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
 
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...
cprogrammings
 
Working with functions in matlab
Working with functions in matlabWorking with functions in matlab
Working with functions in matlab
harman kaur
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
Online
 
Parm
ParmParm
Parm
parmsidhu
 
Array and string
Array and stringArray and string
Array and string
prashant chelani
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
Puneet Rajput
 

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

Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
ishan743441
 
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
RashidFaridChishti
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
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
Abu Saleh
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
ShashiShash2
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
Abubakar524802
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
sheibansari
 
Tut Constructor
Tut ConstructorTut Constructor
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
 
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
AakashBerlia1
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
ArjunArora78
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
Vikash Dhal
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
Mohammad Shaker
 
Presentation
PresentationPresentation
Presentation
manogallery
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
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

Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
harman kaur
 
Creating red black tree
Creating red black treeCreating red black tree
Creating red black tree
harman kaur
 
c plus plus programsSlide
c plus plus programsSlidec plus plus programsSlide
c plus plus programsSlide
harman kaur
 
Cpp programs
Cpp programsCpp programs
Cpp programs
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
 
Digital u1
Digital u1Digital u1
Digital u1
harman kaur
 
Quiz on Logic Gate
Quiz on Logic GateQuiz on Logic Gate
Quiz on Logic Gateharman kaur
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
ਜਪੁ ਜੀ ਸਾਹਿਬ (JAPJI SAHIB)
harman kaur
 
Msql query
Msql queryMsql query
Msql query
harman kaur
 
Rules of inference
Rules of inferenceRules of inference
Rules of inference
harman kaur
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
harman kaur
 
Introduction to digital electornics
Introduction to digital electornicsIntroduction to digital electornics
Introduction to digital electornicsharman kaur
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
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

The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 

Recently uploaded (20)

The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 

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