C++ program : To show the working of Virtual function




#include<iostream.h>
#include<conio.h>
class student
{
   int rollno;
   char name[50];
  public:
   student()
   {
      cout<<"Enter the Rollno : ";
      cin>>rollno;
      cout<<"Enter the Name   : ";
      cin>>name;
   }
   virtual void show()
   {
      cout<<"\tStudent Record\n Rollno : "<<rollno<<"\n Name : "<<name;
   }
};

class test : public student
{       float total;
   float sub[5];
   public:
   test();
   void show();
};
test::test()
{
   total=0;
   cout<<"\tEnter Marks\n";
   for(int i=0;i<5;i++)
   {
      cout<<"Subject "<<i<<": ";
      cin>>sub[i];
      total+=sub[i];
   }
}
void test::show()
{
   cout<<"\nMarks";
   for(int i=0;i<5;i++)
      cout<<"\nSubject "<<i<<": "<<sub[i];
   cout<<"\n   Total : "<<total;

}

int main()
{
   clrscr();
   test obj;
   obj.student::show();
   obj.show();
   getch();
   return 0;
}

Class diagram

 

0 comments:

Post a Comment