Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ OOPs exercise

Write a C++ program to store the details of a patient.

Create a class patient which has the fields name, id, disease and medicine.

Input the name, id and disease from the user and assign a medicine to this patient.

display all the details of the patient including the medicine prescribed.

Solution:

#include #include #include using namespace std; #define MAX 10 class patient { private: char name[30]; int PID; char disease[20]; string medicine; public: //member function to get student's details void getDetails(void); //member function to print student's details void putDetails(void); }; //member function definition, outside of the class void patient::getDetails(void){ cout << "Enter name: " ; cin >> name; cout << "Enter Patient Id: "; cin >> PID; cout << "Enter Disease: "; cin >> disease; if(disease=="pnemunia") { medicine="citrezine"; } else if(disease=="Typhoid") { medicine="Ciprofloxacin "; } else if(disease=="cholera") { medicine="Doxycycline"; } else if(disease=="dengue") { medicine="Tylenol"; } } //member function definition, outside of the class void patient::putDetails(void){ cout << "Student details:\n"<> n; for(loop=0;loop< n; loop++){ cout << "Enter details of patients " << loop+1 << ":\n"; P[loop].getDetails(); } cout << endl; for(loop=0;loop< n; loop++){ cout << "Details of patients " << (loop+1) << ":\n"; P[loop].putDetails(); } return 0; }
Output: Enter total number of patients:2 Enter details of patients 1: Enter name: mad Enter Patient Id: 12 Enter Disease: pneumonia Enter details of patients 2: Enter name: ray Enter Patient Id: 20 Enter Disease: dengue Details of patients 1: Student details: Name:mad PID:12 Disease:pneumonia medicine Details of patients 2: Student details: Name:ray PID:20 Disease:dengue medicine

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?