Pages

Showing posts with label INHERITANCE. Show all posts
Showing posts with label INHERITANCE. Show all posts

Tuesday, October 30, 2012

PATIENT DATABASE USING INHERITANCE

Develop a program in C++ to create a database of the following items of the derived class.
Name of the patient, sex, age, ward number, bed number, nature of illness, date of admission.
Design a base class consisting of data members : name of the patient, sex and age ; and another base class consisting of the data members : bed number and nature of the illness. The derived class consists of the data member ,date of admission.
Program should carry out the following methods
(i) Add a new entry.
(ii) List the complete record.

#include<conio.h>
#include<iostream.h>
class A
{
  public:
  char name[20];
  char sex[10];
  int age;
  void get_data();
  void disp_data();
};
void A::get_data()
{
  cout<<"enter name:";
  cin>>name;
  cout<<"enter age:";
  cin>>age;
  cout<<"enter sex:";
  cin>>sex;
}
void A::disp_data()
{
  cout<<"name:"<<name<<'\n';
  cout<<"age:"<<age<<'\n';
  cout<<"sex:"<<sex<<'\n';
}
class B
{
  public:
  int bed_number;
  char nature_illness[40];
  void get_data();
  void disp_data();
};
void B::get_data()
{
  cout<<"enter bed number:";
  cin>>bed_number;
  cout<<"enter nature of illness:";
  cin>>nature_illness;
}
void B::disp_data()
{
  cout<<"bed number:"<<bed_number<<'\n';
  cout<<"nature of illness:"<<nature_illness<<'\n';
}
class C:public A,public B
{
  int date_admission;
  public:
  void get_data();
  void disp_data();
  void record();
};
void C::get_data()
{
  A::get_data();
  B::get_data();
  cout<<"Enter Date of Admission:-> ";
  cin>>date_admission;
}
void C::disp_data()
{
  A::disp_data();
  B::disp_data();
  cout<<"Date of Admission :"<<date_admission;
}
void main()
{
  clrscr();
  C c1;
  cout<<endl<<"Enter details to add a new record\n";
  getch();
  c1.get_data();
  cout<<endl<<"Displaying the added record to database\n";
  c1.disp_data();
  getch();
}


You might also like:

C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION.

C++ PROGRAM TO READ AND DISPLAY STUDENT DETAILS USING INHERITANCE.


OUTPUT

Monday, September 3, 2012

C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION

An electricity board charges the following rates to domestic users to discourage large consumption of energy :
        For the first 100 units − 50 P per unit
        Beyond 300 units − 60 P per unit
If the total cost is more than Rs.250.00 then an additional surcharge of 15% is added on the difference. Define a class Electricity in which the function Bill computes the cost. Define a derived class More_Electricity and override Bill to add the surcharge.



#include<iostream.h>
#include<conio.h>
class electricity
{
  protected:
  float unit;
  float cost;
  public:
  void bill()
  {
    cout<<"\n enter the no. of units"<<endl;
    cin>>unit;
    if(unit<=100)
    {
      cost=0.50*unit;
      cout<<"cost up to 100 unit is Rs."<<cost<<endl;
    }
    else
    {
      if(unit>300)
      {
        cost=0.60*unit;
        cout<<"Beyond 300 units is Rs"<<cost;
      }
    }
  }
};
class more_electricity:public electricity
{
  float surcharge,diff,total_cost;
  public:
  void bill()
  {
    electricity::bill();
    if(cost>250.00)
    {
      diff=cost-250;
      surcharge=diff*0.15;
      total_cost=cost+surcharge;
      cout<<" Bill amount with surcharge is Rs"<<total_cost;
    }
    else
    {
      cout<<"Bill amout is Rs."<<cost;
    }

  }
};
void main()
{

  clrscr();
  more_electricity me;
  me.bill();

  getch();
}



OUTPUT



Saturday, September 1, 2012

INHERITANCE-READ AND DISPLAY STUDENT MARKS.

Write a complete C++ program to do the following :
(i) ‘Student’ is a base class, having two data members: entryno and   name;

entryno is integer and name of 20 characters long. The value of
entryno is 1 for Science student and 2 for Arts student, otherwise it is an error.
(ii) ‘Science’ and ‘Arts’ are two derived classes, having respectively data items marks for Physics, Chemistry, Mathematics and marks for English, History, Economics.
(iii) Read appropriate data from the screen for 3 science and 2 arts
students.
(iv) Display entryno, name, marks for science students first and then for arts students. 



#include<iostream.h>
#include<conio.h>
class student
{
  protected:
  int entryno;
  char name[20];
  public:
  void getdata()
  {
    cout<<"enter name of the student"<<endl;
    cin>>name;
  }
  void display()
  {
    cout<<"Name of the student is"<<name<<endl;
  }
};
class science:public student
{
  int pcm[3];
  public:
  void getdata()
  {
    student::getdata();
    cout<<"Enter marks for Physics,Chemistry and Mathematics\n";
    for(int j=0;j<3;j++)
    {
      cin>>pcm[j];
    }
  }
  void display()
  {
    entryno=1;
    cout<<"entry no for Science student is"<<entryno<<endl;
    student::display();
    cout<<"Marks in Physics,Chemistry and Mathematics are"<<endl;
    for(int j=0;j<3;j++)
    {
      cout<<pcm[j]<<endl;;
    }
  }

};
class arts:public student
{
  int ehe[3];
  public:
  void getdata()
  {
    student::getdata();
    cout<<"Enter marks for English,History and Economics"<<endl;
    for(int j=0;j<3;j++)
    {
      cin>>ehe[j];
    }
  }
  void display()
  {
    entryno=2;
    cout<<"entry no for Arts student is"<<entryno<<endl;;
    student::display();
    cout<<"Marks in English,History and Economics are"<<endl;
    for(int j=0;j<3;j++)
    {
      cout<<ehe[j]<<endl;;
    }
  }
};
void main()
{
  clrscr();
  science s1[3];
  arts a1[3];
  int i,j,k,l;
  cout<<"Entry for Science students"<<endl;
  for(i=0;i<3;i++)
  {
    s1[i].getdata();
  }
  cout<<"Details of three Science students are"<<endl;
  for(j=0;j<3;j++)
  {
    s1[j].display();
  }
  cout<<"Entry for Arts students"<<endl;
  for(k=0;k<3;k++)
  {
    a1[k].getdata();
  }
  cout<<"Details of three Arts students are"<<endl;
  for(l=0;l<3;l++)
  {
    a1[l].display();
  }
  getch();
}