C++ program – illestrate virtual functions

by Nideesh C on April 14, 2011 · 0 comments

in C++




A program to access the member of the derived class objects through an array of members. In this program, both the base class and the derived class member functions are preceded by the keyword.

#include<iostream.h>
#include<conio.h>
 
	class base
	{
		private: int x;
			float y;
		public:
			virtual void getdata( );
			virtual void display( );
	};
	class devb: public base
	{
		private: int roll;
			char name[20];
		public:  virtual void getdata( );
			virtual void display( );
	};
	class devc : public  base
	{
		private: float height;
			float weight;
		public : virtual void getdata( );
			 virtual void display( );
	}:
	void base :: getdata( )
	{
		cout<<” Enter any Integer”;
		cin>>x;
		cout<<”Enter a real no”;
		cin>>y;
	}
	void base ::display( )
	{
		cout<<”The no X=”<<x<<”Y=”<<y<<endl;
	}
	void devb :: display( )
	{
		cout<<”Roll of the Student is:”<<roll<<endl;
		cout<<” Name of the Student is: “<<name<<endl;
	}
	void devb :: getdata( )
	{
		cout<<”Enter the Roll of  the Student:”;
		cin>>roll;
		cout<<”Enter Name of Student :”;
		cin>>name;
	}
	void devc :: getdata( )
	{
		cout<<”Enter height and weight”;
		cin>>height>>weight;
	}
	void devc :: display( )
	{
		cout<<”Height :”<<height<<endl;
		cout<<”Weight :”<<weight<<endl;
	}
	void main( )
	{
		base *ptr[3];
		devb objb;
		devc objc;
		clrscr( );
		ptr[0] = &objb;
		ptr[1] = &objc;
		ptr[0] ->getdata( );
		ptr[1] -> getdata( );
		ptr[1] -> display( );
		ptr[1] -> display( );
		getch ( );
  	}

OUTPUT

Enter the Roll of  the Student: 101
 Enter Name of Student : Vineeth
 height and weight 170 72

Roll of  the Student is:101
 Name of  the Student is:Vineeth
 Height :170
 Weight :72

 

Not Satisfied ? Just search & get the result

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

Related posts:

  1. C++ program – implement Pure Virtual Functions
  2. C++ class program – illestrate File operations
  3. C++ program – Using class to generate mark sheet using multiple inheritance
  4. C++ program – Perform complex arithmetic using operator overloading
  5. C++ program for creation and traversal of a Binary Tree

Leave a Comment

Previous post:

Next post: