100% found this document useful (1 vote)
249 views

Virtual Function in C++

The document discusses the concept of base and derived classes in C++. It defines a base class and derived class with a display() method. It then creates an object of the derived class and assigns it to a base class pointer. When calling display() through the pointer without virtual, the base class method is called. But with virtual, the derived class method is called, allowing polymorphic behavior.

Uploaded by

krunal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
249 views

Virtual Function in C++

The document discusses the concept of base and derived classes in C++. It defines a base class and derived class with a display() method. It then creates an object of the derived class and assigns it to a base class pointer. When calling display() through the pointer without virtual, the base class method is called. But with virtual, the derived class method is called, allowing polymorphic behavior.

Uploaded by

krunal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Virtual Keyword with respect to C++

Program:
Header file
/*
===========================
Author: Krunal Soni
Purpose: To explain the concept of Base class and Derive class
===========================
All rights reserved by KSONI
*/

#include <iostream>
#include <string.h>
using namespace std;

/*
=========================
Base class is the parent class
=========================
*/
class Base{

public:
Base();
~Base();
void display();
//virtual void display();

private:
char classname[40];

};
/*
=========================
Derive class has been derived from Base class
=========================
*/
class Derive: public Base{

public:
Derive();
~Derive();
void display();

private:
char classname[40];
};
Defination file

/*
===========================
Author: Krunal Soni
Purpose: To explain the concept of Base class and Derive class
===========================
All rights reserved by KSONI
*/

#include "VirtualC.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

/*
=========================
Base class component defination
=========================
*/
Base::Base(){
strcpy(classname,"I am in Base class");
}

Base::~Base(){

void Base::display(){
cout<<classname<<endl;
}
/*
=========================
Derive class component defination
=========================
*/

Derive::Derive(){
strcpy(classname,"I am in Derive class");
}

Derive::~Derive(){

void Derive::display(){
cout<<classname<<endl;
}
Application file

/*
===========================
Author: Krunal Soni
Purpose: To explain the concept of Base class and Derive class
===========================
All rights reserved by KSONI
*/
#include "VirtualC.h"
#include <iostream>
using namespace std;

/*
=========================
Master function which run the app
=========================
*/
int main(int argc, char * argv[]){
Derive OD; //derive class object
Base *OB= new Base; //base class pointer
OB=&OD; //base class pointer points the derive object
OD.display(); //function from derive will be called
OB->display();// function from base will be called w/o virtual
// function from derive will be called w/ virtual

return 0;
}
Result file:

Without virtual keyword:

With virtual keyword:

You might also like