Simple Program for Virtual Functions Using C++ Programming
Aim
Simple Example Program for virtual functions.
Simple Program for Virtual Functions Algorithm/Steps:
- Step 1: Start the program.
- Step 2: Declare the base class base.
- Step 3: Declare and define the virtual function show().
- Step 4: Declare and define the function display().
- Step 5: Create the derived class from the base class.
- Step 6: Declare and define the functions display() and show().
- Step 7: Create the base class object and pointer variable.
- Step 8: Call the functions display() and show() using the base class object and pointer.
- Step 9: Create the derived class object and call the functions display() and show() using the derived class object and pointer.
- Step 10: Stop the program.
Simple Program for Virtual Functions
#include<iostream.h>
#include<conio.h>
class base {
public:
virtual void show() {
cout << "\n Base class show:";
}
void display() {
cout << "\n Base class display:";
}
};
class drive : public base {
public:
void display() {
cout << "\n Drive class display:";
}
void show() {
cout << "\n Drive class show:";
}
};
void main() {
clrscr();
base obj1;
base *p;
cout << "\n\t P points to base:\n";
p = &obj1;
p->display();
p->show();
cout << "\n\n\t P points to drive:\n";
drive obj2;
p = &obj2;
p->display();
p->show();
getch();
}
Sample Output
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show
Virtual Class and Functions In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming