0% found this document useful (0 votes)
10 views16 pages

PROGRAM10 Merged Removed

Uploaded by

Rakhi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

PROGRAM10 Merged Removed

Uploaded by

Rakhi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PROGRAM-10

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>

using namespace std;

// Employee class
class Employee {
protected:
string name;
double salary;

public:
Employee(string name, double salary) : name(name), salary(salary) {}

string getName() const {


return name;
}

double getSalary() const {


return salary;
}

virtual void print() const {


cout << "Employee Name: " << name << ", Salary: " << salary << endl;
}
};

// Manager class inheriting from Employee


class Manager : public Employee {
private:
string department;

public:
Manager(string name, double salary, string department) : Employee(name, salary),
department(department) {}

void print() const override {


cout << "Manager: " << getName() << ", Department: " << department << ", Salary: "
<< getSalary() << endl;
}
};

// Executive class inheriting from Manager


class Executive : public Manager {
public:
Executive(string name, double salary, string department) : Manager(name, salary,
department) {}

void print() const override {


cout << "Executive: ";
Manager::print();
}
};

int main() {
// Create Employee object
Employee employee("Aarti ", 50000);
employee.print();

// Create Manager object


Manager manager("Rita", 100000, "HR");
manager.print();

// Create Executive object


Executive executive("Rupal", 150000, "Marketing");
executive.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];

for (int i = 0; i < numPatients; i++) {


cout << "Enter information for patient " << i + 1 << ":" << endl;
patients[i].enterInfo();
patients[i].enterAge();
}

cout << "\nPatient Database:" << endl;


for (int i = 0; i < numPatients; i++) {
cout << "Patient " << i + 1 << ":" << endl;
patients[i].displayInfo();
patients[i].displayAge();
cout << endl;
}

cout << "\nPediatric Patients:" << endl;


for (int i = 0; i < numPatients; i++) {
if (patients[i].isPediatric()) {
cout << "Patient " << i + 1 << ":" << endl;
patients[i].displayInfo();
patients[i].displayAge();
cout << endl;
}
}
return 0;
}
PROGRAM-6
Create a class rational which represents a numerical value by two double
values- NUMERATOR and DENOMINATOR. Include the following public
member Functions:
• constructor with no arguments (default).
• constructor with two arguments.
• void reduce( ) that reduces the rational number by eliminating the
highest common factor between the numerator and denominator.
• Overload + operator to add two rational number.
• Overload >> operator to enable input through cin.
• Overload << operator to enable output through cout.
Write a main ( ) to test all the functions in the class.
#include<iostream>
#include<cmath>
using namespace std;

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();
}

// Function to reduce the rational number void


int reduce() { double commonFactor =
gcd(numerator, denominator);
numerator /= commonFactor;
denominator /= commonFactor;
}

// Overload + operator to add two rational numbers


Rational operator+(const Rational& other) const {
double resultNumerator = (numerator * other.denominator) + (other.numerator *
denominator);
double resultDenominator = denominator * other.denominator;
return Rational(resultNumerator, resultDenominator);
}

friend istream& operator>>(istream& in, Rational&


rational) {
cout << "Enter Numerator: ";
in >> rational.numerator;
cout << "Enter Denominator: ";
in >> rational.denominator;
rational.reduce();
return in;
}
friend ostream& operator<<(ostream& out, const Rational&
rational) {
out << rational.numerator; if (rational.denominator != 1) {
out << "/" << rational.denominator;
}
return out;
}
};

int main() {
Rational r1;
cout << "Default Rational number: " << r1 << endl;

Rational r2(3, 4); // parameterized constructor


cout << "Rational number r2: " << r2 << endl;
Rational r3;
cout << "Enter values for a new Rational number (using cin): ";
cin >> r3;
cout << "New Rational number (after reducing): " << r3 << endl; Rational sum = r2 + r3;
cout << "Sum of r2 and r3: " << sum << 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;
}

You might also like