PROGRAM10 Merged Removed
PROGRAM10 Merged Removed
Create a class Employee with a name and salary. Create a class Manager
inherit from Employee. Add an instance variable, named department, of
type string. Supply a method to to String that prints the manager’s name,
department and salary. Create 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.
#include <iostream>
#include <string>
// Employee class
class Employee {
protected:
string name;
double salary;
public:
Employee(string name, double salary) : name(name), salary(salary) {}
public:
Manager(string name, double salary, string department) : Employee(name, salary),
department(department) {}
int main() {
// Create Employee object
Employee employee("Aarti ", 50000);
employee.print();
return 0;
}
PROGRAM-5
Create two classes DM and DB which store the value of distances. DM
stores distances in metres and centimetres and DB in feet and inches. Write
a program that can read values for the class objects and add one object of
DM with another object of DB. Use a friend function to carry out the
addition operation. The object that stores the results maybe a DM object or
DB objects, depending on the units in which the results are required. The
display should be in the format of feet and inches or metres and
centimetres depending on the object on display.
#include<iostream>
using namespace std;
class DB;
class DM{
float distM, distCm;
public:
void get(){
cout<<"Enter distance in meters and centimeters:";
cin>>distM>>distCm;
}
friend void add(DM,DB);
};
class DB{
float distF,distI;
public:
void get(){
cout<<"Enter distance in feet and inches:";
cin>>distF>>distI;
}
friend void add(DM,DB);
};
void add(DM d1,DB d2){
DM d3;
float ansM=d2.distF*12*0.0254;
float ansCm=d2.distI*2.54;
d3.distM=d1.distM+ansM;
d3.distCm=d1.distCm+ansCm;
cout<<"Result is:"<<d3.distM<<"m "<<d3.distCm<<"cm";
}
int main(){ DM
d1; DB d2;
d1.get();
d2.get();
add(d1,d2);
return 0;
}
PROGRAM-9
A hospital wants to create a database regarding its indoor patients. The
information to store include
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 all the pediatric patients (less than twelve years
in age).
#include <iostream>
using namespace std;
struct Date {
int year;
int month;
int day;
void inputDate() {
cout << "Enter year: ";
cin >> year;
cout << "Enter month (1-12): ";
cin >> month;
cout << "Enter day (1-31): ";
cin >> day;
}
void displayDate() {
cout << day << "/" << month << "/" << year;
}
};
class Patient {
protected:
string name;
Date admissionDate;
Date dischargeDate;
string disease;
public:
void enterInfo() {
cout << "Enter patient name: ";
cin>>name;
cout << "Enter admission date: " << endl;
admissionDate.inputDate();
cout << "Enter disease: ";
cin>> disease;
cout << "Enter discharge date: " << endl;
dischargeDate.inputDate();
}
void displayInfo() {
cout << "Name: " << name << endl;
cout << "Admission Date: ";
admissionDate.displayDate();
cout << endl;
cout << "Disease: " << disease << endl;
cout << "Discharge Date: ";
dischargeDate.displayDate();
cout << endl;
}
};
class PediatricPatient : public Patient {
private:
int age;
public:
void enterAge() {
cout << "Enter patient age: ";
cin >> age;
}
void displayAge() {
cout << "Age: " << age << endl;
}
bool isPediatric() {
return age < 12;
}
};
int main() {
int numPatients;
cout << "Enter number of patients: ";
cin >> numPatients;
PediatricPatient patients[numPatients];
class Rational {
private:
double numerator;
double denominator;
// Function to calculate the greatest common divisor (GCD)
double gcd(double a, double b) {
if (b == 0)
return a;
return gcd(b, fmod(a, b));
}
public:
// Default constructor
Rational() : numerator(0), denominator(1) {}
// Parameterized constructor
Rational(double num, double denom) : numerator(num), denominator(denom) {
reduce();
}
int main() {
Rational r1;
cout << "Default Rational number: " << r1 << endl;
return 0;
}
PROGRAM-2a
Write a C++ program that illustrates the order of execution of constructors
and destructors when new class is derived from more than one base class
#include <iostream>
using namespace std;
class Base1 {
public:
Base1() {
cout << "Base1 constructor" << endl;
}
~Base1() {
cout << "Base1 destructor" << endl;
}
};
class Base2 {
public:
Base2() {
cout << "Base2 constructor" << endl;
}
~Base2() {
cout << "Base2 destructor" << endl;
}
};
class Derived : public Base1, public Base2 {
public:
Derived() {
cout << "Derived constructor" << endl;
}
~Derived() {
cout << "Derived destructor" << endl;
}
};
int main() {
Derived d;
return 0;
}