C++ program to implement Pure Virtual Functions

AIM
A program to demonstrate how a pure virtual function is defined, declared and invoked from the object of derived class through the pointer of the base class.

ALGORITHM

1. Start the process
2. Invoke the class with pointer
3. Assign ptr<-&obj 4. Call the ptr->getdata()
a. Get the roll no and name of the student
5. Call the ptr->display()
a. Print the roll no and name
6. Stop the process

PROGRAM

#include<iostream.h>
	#include<conio.h>
 
	class  base 
	{
		private: int x;
			float y;
		public : virtual void getdata( );
			virtual void display( );
	};
	class dev : public base
	{
		private:  int roll;
			 char name[20];
		public : void getdata( );
			void  display( );
	};	
	void base :: getdata( )  {  }
	void base :: display( )   {  }
 
	void dev :: getdata( )
	{
		cout<<” Enter Roll of  the Student “;
		cin>> roll;
		cout<<” Enter name of  the student”;
		cin>>name;
        }
	void dev :: display( )
	{
		cout<<”Name is :<<name<<endl;
		cout<<” Roll no is :<<roll <<endl;
	}
 
	void main( )
	{
		base * ptr;
		dev obj;
		clrscr( );
		ptr = &obj;
		ptr -> getdata( );
		ptr -> display( );
		getch( );
	}

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

OUTPUT

Enter the roll no of the student: 111
Enter the name of the student : Kapil Dev

Name is : Kapil Dev
Roll no is : 111


5 Responses to “C++ program to implement Pure Virtual Functions”

  1. The program is compiled,
    but isn’t it required that a pure virtual function has to be initialised yo zero?

    Reply
  2. The program is compiled,
    but isn’t it required that a pure virtual function has to be initialised to zero?

    Reply

Leave a Reply