Comments

Tuesday, December 18, 2012

OOP : Polymorphism

Posted by at 3:13 AM Read our previous post
Object:
By using the following class diagram write down class definitions in c++. In the main create an aray of 50 pointers to the Base class. The program keeps on creating new obkects(student,teacher,manager,labour) as user wants and each new object is referenced by pointers. Call setdata() function as the object is created. Once all objects are created,call the display function for each of them.

Source code:
#include<iostream>
#include <string>
using namespace std;

 class person
 {
private:
    string name;
    string age;
 public:
    virtual void getdata()
    {
    cout<<"Enter Name: "<<endl;
    cin>>name;
    cout<<"Enter age: "<<endl;
    cin>>age;
    }
    virtual void setdata()
    {
    cout<<"Name: "<<name;
    cout<<"Age: "<<age;
    }
 };

 ////////////////////////////////////////////////////

  class student : public person
 {
private:
    string id;
    string gpa;
 public:
     person::getdata;
     person::setdata;
    void getdata()
    {
    cout<<"Enter id: "<<endl;
    cin>>id;
    cout<<"Enter GPA: "<<endl;
    cin>>gpa;
    }
    void setdata()
    {
    cout<<"ID: "<<id<<endl;;
    cout<<"Gpa: "<<gpa<<endl;;
    }
 };
  ///////////////////////////////////////////////
   class teacher : public person
 {
private:
    string faculty;
    string publication;
 public:

      person::getdata;
     person::setdata;

    void getdata()
    {
    cout<<"Enter faculty: "<<endl;
    cin>>faculty;
    cout<<"Enter publication: "<<endl;
    cin>>publication;
    }
    void setdata()
    {
    cout<<"faculty: "<<faculty<<endl;;
    cout<<"publication: "<<publication<<endl;;
    }
 };
///////////////////////////////////////////////
 class employee : public person
 {
private:
    string e_id;
    string salary;
 public:
      person::getdata;
     person::setdata;
    void getdata()
    {
    cout<<"Enter employee id: "<<endl;
    cin>>e_id;
    cout<<"Enter Salary: "<<endl;
    cin>>salary;
    }
    void setdata()
    {
    cout<<"Employee iD: "<<e_id<<endl;
    cout<<"salary: "<<salary<<endl;
    }
 };

 /////////////////////////////////////////////////
  class manager:public employee
 {
private:
    string designation;

 public:
     employee::getdata;
     employee::setdata;
    void getdata()
    {
    cout<<"Enter DESIGNATION: "<<endl;
    cin>>designation;
   
    }
    void setdata()
    {
   
    cout<<"Designation: "<<designation<<endl;
   
    }
 };
  ////////////////////////////////////////////////
   class labour : public employee
 {
private:
    string overtime;
   
 public:

      employee::getdata;
     employee::setdata;

    void getdata()
    {
    cout<<"Enter Overtime: "<<endl;
    cin>>overtime;
   
    }
    void setdata()
    {
   
    cout<<"Overtime: "<<overtime<<endl;
    }
 };

   void main()
   {
    person obj;
    person *ptr;

    student obj1;
    teacher obj2;
    employee obj3;
    manager obj4;
    labour obj5;



cout<<"Class Person"<<endl;
ptr=&obj;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class Student"<<endl;
ptr=&obj1;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class teacher"<<endl;

ptr=&obj2;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class Employee"<<endl;
ptr=&obj3;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class Manager"<<endl;
ptr=&obj4;
ptr->getdata();
cout<<endl;
ptr->setdata();
      
cout<<"Class Labour"<<endl;
ptr=&obj4;
ptr->getdata();
cout<<endl;
ptr->setdata();
    }


Output
  

    

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template