c++ programs examples

C++ Program to inherit two classes Student and Faculty from the base class Person

a.      In the base class two functions input( ) and display( ) to input the data members of class Person.

b.       In the derived classes two functions input( ) and display() to input the data members of classes Faculty and Student.

c.        Create the objects of the three classes and call the functions.

c++ programs examples
 /* 21. Write a program to inherit two classes Student and Faculty from the
 base class Person
 a. In the base class two functions input() and display() to input the data
    members of the class Person.
 b. In the derived classes two functions input() and display() to input the
    data members of classes Faculty and Student.
 c. Create the objects of the three classes and call all the functions */

 #include<iostream.h>
 #include<conio.h>
 #include<string.h>
 class Person
 {
  private:
    char name[20];
    char address[20];
  public:
   Person()
   {
    strcpy(name," ");
    }
   void input()
   {
    char n[20],add[20];
    cout<<endl<<"\t********** Input Record **********"<<endl;
    cout<<endl<<"Enter name"<<endl;
    cin>>n;
    cout<<"Enter address"<<endl;
    cin>>add;
    strcpy(name,n);
    strcpy(address,add);
    }
   void display()
   {
    cout<<endl<<"\t********* Displaying Record *********"<<endl<<endl;
    cout<<"Name:\t\t"<<name<<endl;
    cout<<"Address:\t"<<address<<endl;
    }
 };
 class Faculty:public Person
 {
  private:
   int facid;
   char deptname[20];
  public:
   Faculty()
   {
    facid=0;
    strcpy(deptname," ");
    }
   void input()
   {
    int fid;
    char dname[20];
    Person::input();
    cout<<"Enter Faculty Id:"<<endl;
    cin>>fid;
    cout<<"Enter Department name:"<<endl;
    cin>>dname;
    facid=fid;
    strcpy(deptname,dname);
    }
   void display()
   {
    Person::display();
    cout<<"Faculty Id:\t"<<facid<<endl;
    cout<<"Department Name:"<<deptname<<endl;
    }
  };
 class Student:public Person
 {
  private:
   int stuid;
   char course[10];
  public:
   Student()
   {
    stuid=0;
    strcpy(course," ");
    }
   void input()
   {
    int sid;
    char c[10];
    Person::input();
    cout<<"Enter Student Id:"<<endl;
    cin>>sid;
    cout<<"Enter course name:"<<endl;
    cin>>c;
    stuid=sid;
    strcpy(course,c);
    }
   void display()
   {
    Person::display();
    cout<<"Student Id:\t"<<stuid<<endl;
    cout<<"Course:\t\t"<<course<<endl;
    }
 };

 void main()
 {
  clrscr();
  Person p1;
  Faculty f1;
  Student s1;
  cout<<">>>>>>>>>>>>>> Enter value for the Class Person <<<<<<<<<<<<<<"<<endl;
  p1.input();
  cout<<">>>>>>>>>>>>>> Enter value for the Class Faculty <<<<<<<<<<<<<<"<<endl;
  f1.input();
  cout<<">>>>>>>>>>>>>> Enter value for the Class Student <<<<<<<<<<<<<<<"<<endl;
  s1.input();
  clrscr();
  cout<<">>>> Display in all the records for class Person,Faculty & Studen <<<<"<<endl;
  p1.display();
  f1.display();
  s1.display();
  getch();
  }

Leave a Reply

Your email address will not be published. Required fields are marked *