C++ program – implement Pure Virtual Functions

by Nideesh C on April 14, 2011 · 0 comments

in C++




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.

 

#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( );
	}

OUTPUT

Enter the roll no of the student: 111
Enter the name of the student : Vijay

Name is : Vijay
 Roll no is : 111



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ program – Using class to generate mark sheet using multiple inheritance
  2. C++ class program – illestrate File operations
  3. C++ program – Perform complex arithmetic using operator overloading
  4. C++ program to implement Stack using Formula Based Representation
  5. C++ program to implement Heap sort Algorithm

Leave a Comment

Previous post:

Next post: