7-10 OOPS
7-10 OOPS
PRACTICAL-7
AIM: A hospital wants to create a database regarding its indoor patients. The information
to store includes: -
a) Name of the patient
b) Date of admission
c) Disease
d) Date of discharge
Create a structure to store the date (year, month and date as its members). Create a base class
to store the above information. The member function should include functions to enter
information and display a list of all the patients in the database. Create a derived class to
store the age of the patients. List the information about all the to store the age of the patients.
List the information about the pediatric patients (less than twelve years in age).
PROCEDURE:
SOURCE CODE:
#include<iostream>
using namespace std;
struct date
{
int
d;
int
m;
int
y;
};
class hospital
{
char name[100];
struct date d_adm;
struct date d_dis;
public:
void getdata()
{
cout<<"Enter name of the patient: ";
cin>>name;
cout<<"Enter date of admission: ";
cin>>d_adm.d>>d_adm.m>>d_adm.y;
cout<<"Enter date of discharge: ";
cin>>d_dis.d>>d_dis.m>>d_dis.y;
}
void display()
{
cout<<"Patient name: "<<name;
cout<<"Date of admission: "<<d_adm.d<<d_adm.m<<d_adm.y;
cout<<"Date of discharge: "<<d_dis.d<<d_dis.m<<d_dis.y;
}};
class age:public hospital
{
int a;
public:
void get()
{
cout<<"Enter age: ";
cin>>a;
}
void put()
{
if(a<12)
{
display();
cout<<"age: "<<a;
}
else
cout<<"age greater than 12";
}};
int main()
{
age a1;
a1.getdata();
a1.get();
a1.put();
return 0;
}
OUTPUT:
2323007
PRACTICAL-8
AIM: Create a class rational which represents a numerical value by two double values –
NUMERATOR and DENOMINATOR. Include the following public member functions:
PROCEDURE:
1. Create a class Rational to represent a rational number and add two private data members.
2. Initialize numerator to 0 and denominator to 1 (default value for rational numbers) and
accept two parameters and initialize numerator and denominator.
3. Reduce the rational number by finding the Greatest Common Divisor (GCD) of the
numerator and denominator and use a helper function to compute the GCD.
4. Add two rational numbers.
5. Create instances of the Rational class and test the constructors, addition operator, and
reduce() function.
6. Use the overloaded >> and << operators for input/output.
SOURCE CODE:
#include<iostream>
using namespace std;
class Rational{
float num,denom;
public:
Rational()
{
}
Rational(float num, float denom)
{
if(denom==0)
{
cout << "Denominator can't be zero"; exit(1);
}
this->num = num;
this->denom = denom;
}
Rational reduce()
{
Rational temp;
int h = hcf(num, denom);
temp.num = num/h;
temp.denom = denom/h; return
temp;
}
Rational operator +(Rational r)
{
Rational ans;
ans.denom = denom*r.denom;
ans.num = num*r.denom+r.num*denom; return
ans;
}
int hcf(int a, int b);
friend istream& operator >>(istream& s, Rational& r);
friend ostream& operator <<(ostream& s, Rational& r);
};
int Rational::hcf(int a, int b)
{
int r;
r = a%b;
while(r)
{
a=b;
b=r;
r=a%b;
}
return b;
}
istream& operator >>(istream& s,Rational& r)
{
int a,b;
char c;
s>>a>>c>>b;
if(c!='/')
{
cout<<"use of invalid notation";
exit(0);
}
if(b==0)
{
cout<<"denominator can't be zero.";
exit(1);
}
r.num=a;
r.denom=b;
return s;
}
ostream& operator <<(ostream& s,Rational& r)
{
if(r.denom==1)
s<<r.num;
else
{
if(r.denom==-1)
s<<-r.num;
else
s<<r.num<<'/'<<r.denom;
}
return s;
}
int main()
{
Rational r1,r2,r3;
cout<<"enter r1:";
cin>>r1;
cout<<"enter r2:";
cin>>r2;
r3=r1+r2;
cout<<r1<<" + "<<r2<<" = "<<r3<<" = ";
r3 = r3.reduce();
cout<<r3<<endl;
return 0;
}
OUTPUT:
2323007
PRACTICAL-9
AIM: Make a class Employee with a name and salary. Make a class Manager inherit from
Employee. Add an instance variable, named department, of type string. Supply a method to
String that prints the manager’s name, department and salary. Make a class Executive inherits
from Manager. Supply a method to String that prints the string “Executive” followed by the
information stored in the Manager superclass object. Supply a test program that tests these
classes and methods.
PROCEDURE:
1. Create the Employee class with name and salary as member variables. Implement a
constructor and a toString() method to return the employee’s details.
2. Create the Manager class that inherits from Employee and adds an additional
department member variable. Implement a toString() method that overrides the one in
Employee and includes department info.
3. Create the Executive class that inherits from Manager and overrides the toString()
method. It should prepend "Executive" to the manager's details.
4. Implement a test program that creates instances of Employee, Manager, and Executive
and calls their toString() method to print their details.
5. Compile and run the C++ program to verify that the class hierarchy works and the
details are correctly printed.
SOURCE CODE:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
//class Employee
class Employee
{
public:
char name[30]; int
salary;
};
public:
Executive()
{
cout<<"\nExecutive";
getData();
public:
Executive()
{
cout<<"\nExecutive";
getData();
show();
}
};
int main()
{
Executive e;
getch();
}
OUTPUT
2323007
PRACTICAL-10
AIM: Write a program that creates a binary file by reading the data for the students from the
terminal. The data of each student consist of roll no., name (a string of 30 or lesser no. of
characters) and marks.
PROCEDURE:
1. Create a student class to represent student data, including roll number, name, and marks.
2. Implement a function to open the file in binary write mode.
3. Get the student details.
4. Convert the data to a binary format and write it to the file.
5. Use a main function to control the flow and allow multiple students' data to be added.
SOURCE CODE:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
//class student
class student
{
int rollno,marks;
char nm[30];
public:
//getdata() function to take data from the user
void getdata()
{
cout<<"\n Enter roll no. : ";
cin>>rollno;
cout<<"\n Enter name of student : ";
cin>>nm;
cout<<"\n Enter marks : ";
cin>>marks;
}
};
//main() function to test student class
int main()
{
student stu[5];
for(int count=0;count<5;count++)
{
stu[count].getdata();
}
getch();
}
OUTPUT: