Wednesday, 6 February 2013

inheritance in c++

#include<iostream.h>
#include<conio.h>
#include<string>
using namespace std;

class person
{
protected:
    int id;
   string name;
   string address;
public:

        void input()
   {
   cout<<"terminate with $\n";
   cout<<"enter name:    ";
   getline(cin,name,'$');
   cout<<"terminate with $\n";
   cout<<"enter address:    ";
   getline(cin,address,'$');
   }

      void output()
   {
   cout<<"name:    "<<name<<endl;
   cout<<"Address:    "<<address<<endl;
   }
};


class student:public person
{
private:
    float gpa;
protected:
   string course;
   int a,g2;
    char p;
public:
   student()
   {
   gpa=4;
   }
    void input1()
   {
   person::input();
   cout<<"Enter roll no:    ";
   cin>>a;
   cout<<"Terminate with $";
   cout<<"\nEnter course title:    ";
   getline(cin,course,'$');

    cout<<"would you like to update gpa??  y/n";
   cin>>p;
   if(p=='y')
   {cout<<"enter gpa:    ";
   cin>>g2;
   gpa=g2;}


   }



       void output1()
   {
   person::output();
   cout<<"roll no:    "<<a<<endl;
   cout<<"course tittle:    "<<course<<endl;
   cout<<"gpa:    "<<gpa<<endl;
   }

};

class employee:public person
{
protected:
    string designation;
    int id,sal,ded;
public:

        void input2()
    {
    person::input();
    cout<<"enter employee id:    ";
    cin>>id;
    cout<<"terminate with $\n";
    cout<<"enter designation:    ";
    getline(cin,designation,'$');
    cout<<"enter salary:    ";
    cin>>sal;
   cout<<"enter deduction amount:    ";
   cin>>ded;
    }


       void output2()
    {
    person::output();
    cout<<"id:    "<<id<<endl;
    cout<<"designation:    "<<designation<<endl;
    cout<<"salary:    "<<sal<<endl;
   cout<<"Annual salary is:    "<<sal*12<<endl;
   cout<<"Deduction amount is:    "<<ded<<endl;
    }
};

class working_student:public student,public employee
{
protected:
    int t1,t2;
public:

        void getdata()
    {
   cout<<"enter time in hours given to studies:    ";
    cin>>t1;
    cout<<"enter time in hours given to teaching:    ";
    cin>>t2;
    }

        void showdata()
    {
    cout<<"time given to study:    "<<t1<<endl;
    cout<<"time given to teaching:    "<<t2<<endl;
   }
};

class hourly_employee:public employee
{
protected:
    int p,t;
public:
        void input2()
    {
    employee::input2();
    cout<<"enter pay per hour:    ";
    cin>>p;
    cout<<"enter timing hours per day:    ";
    cin>>t;
    }

        void output2()
    {
   employee::output2();
    cout<<"pay per hour:    "<<p<<endl;
    cout<<"pay per day:    "<<p*t<<endl;
    }
};

class contract: public employee
{
private:
   int c,ll;
    string cont_name;
public:
        void input2()
    {
    employee::input2();
    cout<<"Terminate with $\n";
    cout<<"enter contract name:    ";
    getline(cin,cont_name,'$');
    cout<<"pay for the contract:    ";
    cin>>c;
    cout<<"time period for the contract:    ";
    cin>>ll;
    }

        void output2()
    {
    employee::output2();
    cout<<"contract name:    "<<cont_name<<endl;
    cout<<"pay for the contract:    "<<c<<endl;
    cout<<"time period for the contract:    "<<ll<<endl;
    }
};

class permanent:public employee
{
protected:
    int ser;
public:
        void input2()
    {
    employee::input2();
    cout<<"Enter service years:    ";
    cin>>ser;
    }

       void output2()
    {
    employee::output2();
    cout<<"Service years are:    "<<ser<<endl;
    }
};

void main()

{
cout<<"\n\nINPUT FOR WORKING STUDENT:\n\n";
working_student w;
cout<<"\n\nstudent attributes\n\n";
    w.input1();
cout<<"\n\nteacher attributes\n\n";
    w.input2();
cout<<"\n\ncombined attributes\n\n";
   w.getdata();
cout<<"\n\nINPUT FOR HOURLY EMPLOYEE:\n\n";
hourly_employee h;
    h.input2();
cout<<"\n\nINPUT FOR CONTRACT:\n\n";
contract c;
    c.input2();
cout<<"\n\nINPUT FOR PERMANENT:\n\n";
permanent p;
    p.input2();
cout<<"\n\n\nOUTPUT IS:\n\n\n";

cout<<"\n\nOUTPUT FOR WORKING STUDENT:\n\n";
    w.output1();
    w.output2();
   w.showdata();
cout<<"\n\nOUTPUT FOR HOURLY EMPLOYEE:\n\n";
    h.output2();
cout<<"\n\nOUTPUT FOR CONTRACT:\n\n";
    c.output2();
cout<<"\n\nOUTPUT FOR PERMANENT:\n\n";
    p.output2();

getch();
}

0 comments:

Post a Comment