Tuesday, 5 February 2013

function overridding in c++


//function overriding
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class person
{
protected:
int id;
    char name[20],address[50];
   public:

person()

    {
    id=0;
    name[0]='\0';
  address[0]='\0';
    }


   void getinfo()      //over ridding function

    {
    cout<<"enter id ";
    cin>>id;
    cout<<"enter name ";
    gets(name);
  cout<<"enter address ";
    gets(address);
      }


   void showinfo()

    {
    cout<<"your personal info: \n\n";
    cout<<"id: "<<id<<endl;
    cout<<"name: "<<name<<endl;
    cout<<"address: "<<address<<endl;
    }
};


class student:public person
{
   private:
    int roll,marks;
   public:

   student()

      {
      person::person();
      roll=marks=0;
      }

   void getinfo()

      {
      person::getinfo();        //we are recalling the function getinfo in the super class if we will not use this statemnt then when we recall getinfo function
                                //then only roll no and marks are entertained but in this case even the data in over-ridding function will be entertained
      cout<<"enter roll no ";
cin>>roll;
      cout<<"enter marks ";
      cin>>marks;
      }

   void showedu()

      {
      cout<<"\n\n\nyour educational info\n\n";
      cout<<"roll no "<<roll<<endl;
      cout<<"marks "<<marks<<endl;
      }

};


void main()

      {
      student * s;
      s=new student;
      s->getinfo();   //low scope function (which is easy to approach) will be called
      s->showinfo();
      s->showedu();
      getch();
      }


0 comments:

Post a Comment