Programming

Program with Student as Abstract Class and Create Derived Classes

Create a program with student as abstract class and create derived classes – engineering, medicine and science from the base class – student. Create the objects of the derived classes, process and access them using array of pointers of type base class – student.

Algorithm

  • Start the program
  • Enter name, roll number and department
  • Link to the respective classes through pointer
  • Display the results
  • End the program

Source Code

#include<iostream.h>

#include<conio.h>

class student

{

   protected:
      char name[10];
      int roll;
      public:
      virtual void getdata()
      {
         cout<<"\n\nEnter the name : ";
         cin>>name;
         cout<<"\nEnter the roll number : ";
         cin>>roll;
      }
      virtual void showdata()
      {  cout<<"\nName : "<<name<<"\t"<<"Roll No : "<<roll;    }
      virtual void dept()=0;
};

class engineering: public student
{
   protected:
      char dep[10];
   public:
      void dept()
                  {
         getdata();
                  cout<<"\nEnter the department :           ";
                  cin>>dep;
      }
      void showdata()
      {
                  student::showdata();
         cout<<"\t"<<"Department : "<<dep;
      }
};
class medicine: public student
{
      protected:
      char dep[10];
   public:
      void dept()
                  {
         getdata();
                  cout<<"\nEnter the department :           ";
                  cin>>dep;
      }
      void showdata()
      {
                  student::showdata();
         cout<<"\t"<<"Department : "<<dep;
      }
};
class science: public student
{
      protected:
      char dep[10];
   public:
      void dept()
                  {
         getdata();
                  cout<<"\nEnter the department :           ";
                  cin>>dep;
      }
      void showdata()
      {
                  student::showdata();
         cout<<"\t"<<"Department : "<<dep;
      }
};
void main()
{
      student *baseptr;
   engineering e; medicine m; science s;
   baseptr = &e; baseptr->dept(); baseptr->showdata();
   baseptr = &m; baseptr->dept(); baseptr->showdata();
   baseptr = &s; baseptr->dept(); baseptr->showdata();
   getch();
}

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.